Send Email Using ASP.NET In GoDaddy Server

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.   
  4. <head runat="server">  
  5.     <title></title>  
  6.     <link href="css/bootstrap.min.css" rel="stylesheet" />  
  7. </head>  
  8.   
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div class="container">  
  12.             <div class="row">  
  13.                 <div class="col-md-4 col-md-offset-4">  
  14.                     <div class="panel panel-primary">  
  15.                         <div class="panel-heading">  
  16.                             <h3 class="panel-title">E-mail</h3>  
  17.                         </div>  
  18.                         <div class="panel-body">  
  19.                             <label>Name</label>  
  20.                             <asp:TextBox ID="txtname" runat="server" CssClass="form-control" placeholder="Name"></asp:TextBox>  
  21.                             <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Provide Name" ControlToValidate="txtname" ForeColor="Red"></asp:RequiredFieldValidator><br />  
  22.                             <label>Subject</label>  
  23.                             <asp:TextBox ID="txtsubj" runat="server" CssClass="form-control" placeholder="Subject"></asp:TextBox>  
  24.                             <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Provide Subject" ControlToValidate="txtsubj" ForeColor="Red"></asp:RequiredFieldValidator><br />  
  25.                             <label>Mobile</label>  
  26.                             <asp:TextBox ID="txtmob" runat="server" CssClass="form-control" placeholder="Mobile Number" MaxLength="12"></asp:TextBox>  
  27.                             <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please Provide Mobile Number" ControlToValidate="txtmob" ForeColor="Red"></asp:RequiredFieldValidator><br />  
  28.                             <label>Email</label>  
  29.                             <asp:TextBox ID="txtemail" runat="server" CssClass="form-control" placeholder="Email ex([email protected])"></asp:TextBox>  
  30.                             <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please Provide Email-ID" ControlToValidate="txtemail" ForeColor="Red"></asp:RequiredFieldValidator><br />  
  31.                             <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 />  
  32.                             <label>Address</label>  
  33.                             <asp:TextBox ID="txtaddress" runat="server" CssClass="form-control" placeholder="Address"></asp:TextBox>  
  34.                             <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Please Provide Address" ControlToValidate="txtaddress" ForeColor="Red"></asp:RequiredFieldValidator><br />  
  35.                             <asp:Button ID="Button1" runat="server" Text="Send Me" CssClass="btn btn-block btn-primary" OnClick="Button1_Click" />  
  36.                         </div>  
  37.                     </div>  
  38.                 </div>  
  39.             </div>  
  40.         </div>  
  41.     </form>  
  42. </body>  
  43.   
  44. </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.
Ebook Download
View all
Learn
View all