Nowadays, sending an email is a small kind of task. We can send email in ASP.NET using a Local Server easily. But on the GoDaddy Server, a little bit of code should be changed, like Port Number, Hosting Server, and user credentials etc.

Following are the steps.
Note - This code will work only on GoDaddy Server, not on a Local Server.
Step 1 Create a new project.

Step 2 Add bootstrap CSS in the CSS folder.

Step 3 Create a webform aspx page.

Step 4 Design the form like below.
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4. <title></title>
  5. <link href="css/bootstrap.min.css" rel="stylesheet" />
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div class="container">
  10. <div class="row">
  11. <div class="col-md-4 col-md-offset-4">
  12. <div class="panel panel-primary">
  13. <div class="panel-heading">
  14. <h3 class="panel-title">E-mail</h3>
  15. </div>
  16. <div class="panel-body">
  17. <label>Name</label>
  18. <asp:TextBox ID="txtname" runat="server" CssClass="form-control" placeholder="Name"></asp:TextBox>
  19. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Provide Name" ControlToValidate="txtname" ForeColor="Red"></asp:RequiredFieldValidator><br />
  20. <label>Subject</label>
  21. <asp:TextBox ID="txtsubj" runat="server" CssClass="form-control" placeholder="Subject"></asp:TextBox>
  22. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Provide Subject" ControlToValidate="txtsubj" ForeColor="Red"></asp:RequiredFieldValidator><br />
  23. <label>Mobile</label>
  24. <asp:TextBox ID="txtmob" runat="server" CssClass="form-control" placeholder="Mobile Number" MaxLength="12"></asp:TextBox>
  25. <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please Provide Mobile Number" ControlToValidate="txtmob" ForeColor="Red"></asp:RequiredFieldValidator><br />
  26. <label>Email</label>
  27. <asp:TextBox ID="txtemail" runat="server" CssClass="form-control" placeholder="Email ex([email protected])"></asp:TextBox>
  28. <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please Provide Email-ID" ControlToValidate="txtemail" ForeColor="Red"></asp:RequiredFieldValidator><br />
  29. <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Please Proide Valid Email-ID" ControlToValidate="txtemail" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator><br />
  30. <label>Address</label>
  31. <asp:TextBox ID="txtaddress" runat="server" CssClass="form-control" placeholder="Address"></asp:TextBox>
  32. <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Please Provide Address" ControlToValidate="txtaddress" ForeColor="Red"></asp:RequiredFieldValidator><br />
  33. <asp:Button ID="Button1" runat="server" Text="Send Me" CssClass="btn btn-block btn-primary" OnClick="Button1_Click" />
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. </form>
  40. </body>
  41. </html>
Step 5

Add Server-side validation for every field. Use RequiredFieldValidator for checking the empty fields.

Step 6

Each FieldValidator needs to set the ControlToValidate in the properties.

Step 7

The Email Validation requires another validator like RegularExpressionValidator to check a valid Email-ID. Using ValidationExpression, we can check easily.
Step 8

Double click on "Send Me" button and add the below code.
  1. try {
  2. MailMessage msgs = new MailMessage();
  3. msgs.To.Add(txtemail.Text.Trim());
  4. MailAddress address = new MailAddress("[email protected]");
  5. msgs.From = address;
  6. msgs.Subject = "Contact";
  7. string htmlBody = @ " <
  8. !DOCTYPE html >
  9. <
  10. html >
  11. <
  12. head >
  13. <
  14. title > Email < /title> <
  15. /head> <
  16. body >
  17. <
  18. h1 > Hi welcome < /h1> <
  19. p > Thank you
  20. for register < /p> <
  21. /body> <
  22. /html>
  23. ";
  24. msgs.Body = htmlBody;
  25. msgs.IsBodyHtml = true;
  26. SmtpClient client = new SmtpClient();
  27. client.Host = "relay-hosting.secureserver.net";
  28. client.Port = 25;
  29. client.UseDefaultCredentials = false;
  30. client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
  31. //Send the msgs
  32. client.Send(msgs);
  33. ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Your Details Sent Successfull.');", true);
  34. } catch (Exception ex) {}
Step 9

Run the application. You will get an error like the following.

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

Step 10

So, add the below code in web.config.
  1. <appSettings>
  2. <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  3. </appSettings>
Step 11

Run the application again. Now, you will get a form like the following.

Step 12

Just click the "Send Me" button. You will get an error message like below.

Step 13

Fill in all the fields and provide a valid Email-ID.

The mail is sent successfully.

That's it. Thank you for reading my blog.