Email Send Using ASP.NET With C#

Introduction

 
In this article, you will learn how to send email using ASP.NET C# through the various SMTP servers. In this article, I also explained the way of sending emails from the GoDaddy server.
 
Below is the step to send an email using ASP.NET C#,
 
Step 1
 
Create a new website.
 
Step 2
 
Create a webform aspx page.
 
Step 3
 
Add CSS file as attached in source code.
 
Step 4
 
Add reference of AjaxToolKit as attached in source code.
 
Step 5
 
Design the webpage like below,
  1. <!DOCTYPE html>  
  2. <html  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head id="Head1" runat="server">  
  5.         <title>Email Send Sample</title>  
  6.         <link href="bootstrap.min.css" rel="stylesheet" />  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div class="container">  
  11.                 <div class="row">  
  12.                     <div class="col-md-4 col-md-offset-4">  
  13.                         <div class="panel panel-primary">  
  14.                             <div class="panel-heading">  
  15.                                 <h3 class="panel-title">Send Email Sample</h3>  
  16.                             </div>  
  17.                             <div class="panel-body">  
  18.                                 <label>Name</label>  
  19.                                 <asp:TextBox ID="txtname" runat="server" CssClass="form-control" placeholder="Name"></asp:TextBox>  
  20.                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Provide Name" ControlToValidate="txtname" ForeColor="Red"></asp:RequiredFieldValidator>  
  21.                                 <br />  
  22.                                 <label>Subject</label>  
  23.                                 <asp:TextBox ID="txtbody" runat="server" CssClass="form-control" placeholder="Subject"></asp:TextBox>  
  24.                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Provide Subject" ControlToValidate="txtbody" ForeColor="Red"></asp:RequiredFieldValidator>  
  25.                                 <br />  
  26.                                 <label>Email([email protected]</label>  
  27.                                 <asp:TextBox ID="txtemail" runat="server" CssClass="form-control" placeholder="Email"></asp:TextBox>  
  28.                                 <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Please Provide Email-ID" ControlToValidate="txtemail" ForeColor="Red"></asp:RequiredFieldValidator>  
  29.                                 <br />  
  30.                                 <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>  
  31.                                 <br />  
  32.                                 <asp:Button ID="Button1" runat="server" Text="Send Email" CssClass="btn btn-block btn-primary" OnClick="Button1_Click" />  
  33.                                 <asp:Button ID="Button2" runat="server" Text="Send Email from Godaddy" CssClass="btn btn-block btn-primary" OnClick="Button2_Click" />  
  34.                                 <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>  
  35.                             </div>  
  36.                         </div>  
  37.                     </div>  
  38.                 </div>  
  39.             </div>  
  40.         </form>  
  41.     </body>  
  42. </html>  
Step 6

Add Server-side validation for every field as name validation, email validation as written in the above webpage source code.
 
Step 7

Add Server-side RegularExpressionValidator to valid the correct email id.
 
Step 8
 
Added below namespace in .aspx.cs page,
  1. using System.Net;  
  2. using System.Net.Mail;  
Step 9
 
Add the below source code on button click event of the webpage.
 
Send email from local server,
  1. string to = txtemail.Text; //To address  
  2. string from = "[email protected]"//From address  
  3. MailMessage message = new MailMessage(from, to);  
  4. string mailbody = message.Subject = "This article will help you on how to send email using asp.net c# code";  
  5. message.Body = mailbody;  
  6. message.IsBodyHtml = false;  
  7. SmtpClient client = new SmtpClient();  
  8. client.Host = "smtp.gmail.com";  
  9. client.Port = 587;  
  10. System.Net.NetworkCredential basicCredential1 = new  
  11. System.Net.NetworkCredential("[email protected]""password");  
  12. client.EnableSsl = false;  
  13. client.UseDefaultCredentials = false;  
  14. // client.Credentials = basicCredential1;  
  15. try {  
  16.     client.Send(message);  
  17.     lblmsg.Text = "Email Send Successfully";  
  18. catch (Exception ex) {}  
Send email from GoDaddy Server,
  1. string to = txtemail.Text; //To address  
  2. string from = "[email protected]"//From address  
  3. MailMessage message = new MailMessage(from, to);  
  4. string mailbody = "This article will help you on how to send email using asp.net c# code";  
  5. message.Subject = txtbody.Text;  
  6. message.Body = mailbody;  
  7. message.IsBodyHtml = false;  
  8. SmtpClient client = new SmtpClient();  
  9. client.Host = "relay-hosting.secureserver.net"//This is code SMTP Host server name  
  10. client.Port = 25; //This is default port  
  11. client.EnableSsl = false;  
  12. client.UseDefaultCredentials = false;  
  13. try {  
  14.     client.Send(message);  
  15.     lblmsg.Text = "Email Send Successfully";  
  16. catch (Exception ex) {}  
Step 10

Run the application.
 
If you get the below error then follow step 11.
 
WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).
 
Step 11

Add the below code in web.config,
  1. <appSettings>  
  2.    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />  
  3. </appSettings>  
Step 12
 
Run the website and you will get the below screen. Fill in all the details and click on the button Send Email for Local server email like Gmail and Click on Send Email from Godaddy to send email from GoDaddy server that will not work from local work only on the GoDaddy server.
 
Email Send Using ASP.Net With C#
 
Step 13
 
Once you click on the button after filling in the data you get the message "Email Send Successfully".
 
Some of SMTP Class Properties,
  • Host
    SMTP Server( Here you need to give the SMTP server name from where the email going to send).

  • Enable SSL
    Check your host accepted SSL Connections Value is either True or False.

  • Port
    SMTP Server Port (Here you need to give SMTP server port from where the email going to send).

  • Credentials
    SMTP servers login credentials such as Email and Password.

  • UseDefaultCredentials
    When we set to True in UseDefaultCredentials then that specifies to allow authentication based on the credentials of the account used to send emails.

Summary

 
In this article, we learned how to send email using ASP.NET C# through the SMTP server. I hope this article is useful for those who want to send email using ASP.NET C#.


Similar Articles