Introduction

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

Using

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

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.