In this article I will explain how to send Bulk (Mass) email in ASP.Net website using C# and VB.Net.
In order to improve the performance using Parallel ForEach Loop to send Bulk (Mass) emails by making use of Multi-Threading in ASP.Net.
In order to illustrate the working of the Bulk (Mass) email in ASP.Net, I am making use of a GridView control which will be populated with some records of users with their Name and Email Addresses.
One can select the users to whom the email has to be sent using CheckBox and when the Button is clicked, Bulk (Mass) email will be sent to all selected users using Parallel ForEach Loop.
HTML Markup
The HTML Markup consists of an ASP.Net GridView and a Button.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:HyperLink ID="lnkEmail" runat="server" Text='<%# Eval("Email") %>' NavigateUrl='<%# Eval("Email", "mailto:{0}") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Send Bulk Email" runat="server" OnClick = "SendBulkEmail" />
Namespaces
You will need to import the following namespaces.
C#
using System.Net;
using System.Net.Mail;
using System.Data;
using System.Threading.Tasks;
VB.Net
Imports System.Net
Imports System.Net.Mail
Imports System.Data
Imports System.Threading.Tasks
Populating the GridView
I have created a dynamic DataTable with some dummy data and it has been bound to the GridView control in Page Load event.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Email",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "john.hammond@test.com");
dt.Rows.Add(2, "Mudassar Khan", "mudassar.khan@test.com");
dt.Rows.Add(3, "Suzanne Mathews", "suzzane.mathews@test.com");
dt.Rows.Add(4, "Robert Schidner", "robert.schidner@test.com");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), _
New DataColumn("Name", GetType(String)), _
New DataColumn("Email", GetType(String))})
dt.Rows.Add(1, "John Hammond", "john.hammond@test.com")
dt.Rows.Add(2, "Mudassar Khan", "mudassar.khan@test.com")
dt.Rows.Add(3, "Suzanne Mathews", "suzzane.mathews@test.com")
dt.Rows.Add(4, "Robert Schidner", "robert.schidner@test.com")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Sending Bulk (Mass) email to multiple users in ASP.Net
The following event handler is executed when the Button is clicked. The very first thing is to create a DataTable with two columns i.e. Name and Email.
Then a loop is executed over the GridView Rows and the name and email of all the records in GridView for which the CheckBox is checked are fetched and added as a Row to the DataTable.
Finally the Parallel ForEach Loop is executed and the SendEmail function is called inside it so that it gets called for each row of the DataTable.
C#
protected void SendBulkEmail(object sender, EventArgs e)
{
//Create a temporary DataTable
DataTable dtCustomers = new DataTable();
dtCustomers.Columns.AddRange(new DataColumn[2] { new DataColumn("Name", typeof(string)),
new DataColumn("Email",typeof(string)) });
//Copy the Checked Rows to DataTable
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("chkSelect") as CheckBox).Checked)
{
dtCustomers.Rows.Add(row.Cells[2].Text, (row.FindControl("lnkEmail") as HyperLink).Text);
}
}
string subject = "Welcome Email";
string body = "Hello {0},<br /><br />Welcome to ASPSnippets<br /><br />Thanks.";
//Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dtCustomers.AsEnumerable(), row =>
{
SendEmail(row["Email"].ToString(), subject, string.Format(body, row["Name"]));
});
}
private bool SendEmail(string recipient, string subject, string body)
{
MailMessage mm = new MailMessage("sender@gmail.com", recipient);
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
return true;
}
VB.Net
Protected Sub SendBulkEmail(sender As Object, e As EventArgs)
'Create a temporary DataTable
Dim dtCustomers As New DataTable()
dtCustomers.Columns.AddRange(New DataColumn(1) {New DataColumn("Name", GetType(String)), New DataColumn("Email", GetType(String))})
'Copy the Checked Rows to DataTable
For Each row As GridViewRow In GridView1.Rows
If TryCast(row.FindControl("chkSelect"), CheckBox).Checked Then
dtCustomers.Rows.Add(row.Cells(2).Text, TryCast(row.FindControl("lnkEmail"), HyperLink).Text)
End If
Next
Dim subject As String = "Welcome Email"
Dim body As String = "Hello {0},<br /><br />Welcome to ASPSnippets<br /><br />Thanks."
'Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dtCustomers.AsEnumerable(), _
Function(row)
Return SendEmail(row("Email").ToString(), subject, String.Format(body, row("Name")))
End Function)
End Sub
Private Function SendEmail(recipient As String, subject As String, body As String) As Boolean
Dim mm As New MailMessage("sender@gmail.com", recipient)
mm.Subject = subject
mm.Body = body
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As New NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "<password>"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
Return True
End Function
Following the screenshot of the email received from the application
Downloads