ASP.NET: Sending Emails

Introduction

Sending email currently is important for applications for attracting people to use applications/websites. Sending email through websites is a better way to not allow any third party into your application. Some features like Feedback, Newsletter, Messages, Contact Us etc. are similar to sending email. That is because these features in a website sends email to the specified person.

There is also the need to send emails in ASP.Net.

Requirement

To integrate a Send email feature in your website, the most important requirement is for Simple Mail Transfer Protocol (SMTP). The other side requires a good design to show whatever you want to send in the email and code to do it.

The following namespace is required to add Simple Mail Transfer Protocol (SMTP) :
  1. using System.Net.Mail
Source code

Add the following code in the source code page:

 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6.     <style type="text/css">  
  7.         .auto-style1 {  
  8.             color: #A52A2A;  
  9.         }  
  10.     </style>  
  11. </head>  
  12. <body>  
  13.     <form id="form1" runat="server">  
  14.     <div>  
  15. <table align=center>  
  16. <tr>  
  17. <td><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Brown;">To:</span></td>  
  18. <td><asp:textbox id="txtName" Width="241" Runat="server"></asp:textbox></td>  
  19. </tr>  
  20. <tr>  
  21. <td><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Brown;">CC:</span></td>  
  22. <td><asp:textbox id="txtEmail" Width="241" Runat="server"></asp:textbox></td>  
  23. </tr>  
  24. <tr>  
  25. <td class="auto-style1"><strong>Subject </strong></td>  
  26.                                  <td>  
  27. <asp:TextBox ID="TextBox1" runat="server" Width="241"></asp:TextBox>  
  28.                     </td>  
  29.                            </tr>  
  30.                            <tr>  
  31.                                  <td colSpan="2" ><span style="font-family:Verdana; font-size:12px; font-weight:bold; color:Brown;">Message:</span></td>  
  32.                            </tr>  
  33.                            <tr>  
  34.                                  <td align="center" colSpan="2" width=100%><asp:textbox id="txtMessage" Width="100%" Runat="server" Height="99" TextMode="MultiLine" MaxLength="400"></asp:textbox></td>  
  35.                            </tr>  
  36.                            <tr>  
  37.                                  <td colSpan="2"> </td>  
  38.                            </tr>  
  39.                            <tr>  
  40.                                  <td align=center>  
  41.                         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" />  
  42.                     </td>  
  43.                                  <td align=center> </td>  
  44.                            </tr>  
  45.                            <tr>  
  46.                                  <td colSpan="2"><asp:label id="lblStatus" Runat="server" EnableViewState="False"></asp:label></td>  
  47.                            </tr>  
  48.                     </table>  
  49.     </div>  
  50.     </form>  
  51. </body>  
  52. </html>

Design

The Page will look as in the following image:

ASP1.jpg

Code behind

Add the following code in the code behind page in the C# language on the Button Click event:  

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.UI;    
  6. using System.Web.UI.WebControls;    
  7. using System.Net.Mail;    
  8. public partial class Default2 : System.Web.UI.Page    
  9. {    
  10.     protected void Page_Load(object sender, EventArgs e)    
  11.     {    
  12.     }    
  13.     protected void Button1_Click(object sender, EventArgs e)    
  14.     {    
  15.         MailMessage mail = new MailMessage();    
  16.         mail.To.Add(txtName.Text);    
  17.        mail.To.Add(txtEmail.Text);    
  18.         mail.From = new MailAddress("email address from where you want to send emails");    
  19.         mail.Subject = "testing";    
  20.         string Body = txtMessage.Text;    
  21.         mail.Body = Body;    
  22.         mail.IsBodyHtml = true;    
  23.         SmtpClient smtp = new SmtpClient();    
  24.         smtp.Host = "smtp.gmail.com"//Or Your SMTP Server Address    
  25.         smtp.Credentials = new System.Net.NetworkCredential    
  26.              ("add email address""password");    
  27.         //Or your Smtp Email ID and Password    
  28.         smtp.EnableSsl = true;    
  29.         smtp.Send(mail);    
  30.     }    
  31. }   

Run

Now save all your work or press Ctrl+S and hit "F5" to run the page, the page will open in the default browser and will look as in the following image:

ASP2.jpg

Enter the required details and hit the button/send mail, to send to the specified person in "To" and "CC".


Similar Articles