How to Send Bulk Email Using ASP.Net

Introduction

This article explains how to send bulk email using ASP.Net 4.5.

Using

  • ASP .NET web page
  • Grid View
  • HTML Email Templates
  • Gmail SMTP Mail Server

Create a new project using "File" -> "New" -> "Project..." then select web "ASP .Net Web Forms Application". Name it " Bulk Email".



Now in the Design page “Default.aspx” design the web page as in the following screen:

ASP.Net Web Forms Application1

In the Design Source (Default.aspx) write the code as:

Default.aspx.cs

  1. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"  
  2. CodeBehind="Default.aspx.cs" Inherits="BulkEmail._Default" %>  
  3.   
  4. <asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">  
  5.     <section class="featured">  
  6.         <div class="content-wrapper">  
  7.             <hgroup class="title">               
  8.                 <h2>Send Bulk email using asp.net</h2>  
  9.             </hgroup>  
  10.             <p>  
  11.                 To learn more about ASP.NET  
  12.             </p>  
  13.         </div>  
  14.     </section>  
  15. </asp:Content>  
  16. <asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">  
  17.        <h3>We suggest the following:</h3>  
  18.     <asp:Panel ID="Panel1" runat="server" Width="100%" ScrollBars="Horizontal">  
  19.     <p>  
  20.        <asp:Button ID="btnBind" runat="server" Text="View" OnClick="btnBind_Click" /> <asp:Label  
  21. ID="Label1" runat="server" Font-Bold="true" ForeColor="Green" Text="Total Customers:">    
  22. </asp:Label><asp:Label ID="lbltotalcount" runat="server" ForeColor="Red" Font  
  23. Size="Larger"></asp:Label> </p>  
  24.         <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />  
  25.         <asp:GridView ID="grvCustomers" runat="server"></asp:GridView>  
  26.     </asp:Panel>  
  27. </asp:Content>
In the Web.config file create the connection string as:

Web.config

  1. <connectionStrings>  
  2. <add name="ConnectionString"connectionString="Server=localhost;userid=root;password=;Database=  
  3. orthwind"providerName="MySql.Data.MySqlClient"/>        
  4. </connectionStrings>

In the code behind file (Default.aspx.cs) write the code as:

  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.   
  8. //Using namespaces  
  9. using MySql.Data.MySqlClient;  
  10. using System.Configuration;  
  11. using System.Text;  
  12. using System.Net;  
  13. using System.Net.Mail;  
  14. using System.Data;  
  15.   
  16. namespace BulkEmail  
  17. {  
  18.     public partial class _Default : Page  
  19.     {  
  20.         #region MySqlConnection Connection  
  21.         MySqlConnection conn = new  
  22. MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);  
  23.   
  24.         protected void Page_Load(object sender, EventArgs e)  
  25.         {  
  26.             Try  
  27.             {  
  28.                 if (!Page.IsPostBack)  
  29.                 {  
  30.                 }  
  31.             }  
  32.             catch (Exception ex)  
  33.             {  
  34.                 ShowMessage(ex.Message);  
  35.             }  
  36.         }  
  37.         #endregion  
  38.         #region show message  
  39.         /// <summary>  
  40.         /// This function is used for show message.  
  41.         /// </summary>  
  42.         /// <param name="msg"></param>  
  43.         void ShowMessage(string msg)  
  44.         {  
  45.             ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script  
  46. language='javascript'>alert('" + msg + "');</script>");  
  47.         }        
  48.         #endregion  
  49.         #region Bind Data  
  50.         /// <summary>  
  51.         /// This display the data fetched from the table using MySQLCommand,DataSet and  
  52. MySqlDataAdapter  
  53.         /// </summary>  
  54.         /// <param name="sender"></param>  
  55.         /// <param name="e"></param>  
  56.         protected void btnBind_Click(object sender, EventArgs e)  
  57.         {  
  58.             Try  
  59.             {  
  60.                 conn.Open();  
  61.                 MySqlCommand cmd = new MySqlCommand("Select  
  62. CustomerID,ContactName,Address,Phone,Email from customers", conn);  
  63.                 MySqlDataAdapter adp = new MySqlDataAdapter(cmd);  
  64.                 DataSet ds = new DataSet();  
  65.                 adp.Fill(ds);  
  66.                 grvCustomers.DataSource = ds;  
  67.                 grvCustomers.DataBind();  
  68.                 lbltotalcount.Text = grvCustomers.Rows.Count.ToString();  
  69.             }  
  70.             catch (MySqlException ex)  
  71.             {  
  72.                 ShowMessage(ex.Message);  
  73.             }  
  74.             Finally  
  75.             {  
  76.                 conn.Close();  
  77.             }  
  78.             btnBind.Visible = false;  
  79.         }  
  80.         #endregion  
  81.         #region Bulk email,GridView,gmail  
  82.         /// <summary>  
  83.         /// this code used to Send Bulk email  
  84.         /// </summary>  
  85.         /// <param name="sender"></param>  
  86.         /// <param name="e"></param>  
  87.         protected void btnSend_Click(object sender, EventArgs e)  
  88.         {  
  89.             Try  
  90.             {  
  91.                 lbltotalcount.Text = string.Empty;  
  92.                 foreach (GridViewRow grow in grvCustomers.Rows)  
  93.                {  
  94.                     string strCustomerID = grow.Cells[0].Text.Trim();  
  95.                     string strContactName = grow.Cells[1].Text.Trim();  
  96.                     string strAddress = grow.Cells[2].Text.Trim();  
  97.                     string strPhone = grow.Cells[3].Text.Trim();  
  98.                     string strEmail = grow.Cells[4].Text.Trim();                 
  99.                     string filename = Server.MapPath("~/Event.html");  
  100.                     string mailbody = System.IO.File.ReadAllText(filename);  
  101.                     mailbody = mailbody.Replace("##NAME##", strContactName);  
  102.                     string to = strEmail;  
  103.                     string from = "[email protected]";  
  104.                     MailMessage message = new MailMessage(from, to);  
  105.                     message.Subject = "Auto Response Email";  
  106.                     message.Body = mailbody;  
  107.                     message.BodyEncoding = Encoding.UTF8;  
  108.                     message.IsBodyHtml = true;  
  109.                     SmtpClient client = new SmtpClient("smtp.gmail.com", 587);  
  110.                     System.Net.NetworkCredential basicCredential = new  
  111. System.Net.NetworkCredential("[email protected]""Password");  
  112.                     client.EnableSsl = true;  
  113.                     client.UseDefaultCredentials = true;  
  114.                     client.Credentials = basicCredential;  
  115.                     try  
  116.                     {  
  117.                         client.Send(message);  
  118.                         ShowMessage("Email Sending successfully...!" + strContactName + "  ");                                             
  119.                     }  
  120.                     catch (Exception ex)  
  121.                     {  
  122.                         ShowMessage(ex.Message);  
  123.                     }  
  124.                 }   
  125.             }  
  126.             catch (MySqlException ex)  
  127.             {  
  128.                 ShowMessage(ex.Message);  
  129.             }  
  130.             Finally  
  131.             {  
  132.                 conn.Close();  
  133.             }  
  134.         }  
  135.         #endregion  
  136.     }  
  137. }  

See the following screen for the Default.aspx: Total Customers data bind.

Default aspx

Now, see the following screen for the Default.aspx: send email waiting.

Default aspx1

Now, show in the Message box “Email sending successfully”.

Default aspx2

Now "Email Account Open" - > "View Email".

Email Account Open

Now, see the following screen: 93 mails receive in the inbox and the Total Customers Email ID: 93

Email Account Open1

93 messages were received in the inbox. Bulk Sending an E-Mail Using ASP.NET 4.5 with HTML Email Templates and Gmail SMTP Mail Server. I hope this article is useful. If you have any other questions then please provide your comments below.


Similar Articles