In this article I will explain with an example, how to send email to multiple Users (Recipients) in
ASP.Net using C# and VB.Net.
HTML Markup
The following HTML Markup consists of:
GridView – For displaying data.
The GridView consists of two BoundField columns and two TemplateField columns.
The TemplateField columns consists of a CheckBox and Hyperlink.
Button – For sending bulk Email.
The Button has been assigned with OnClick event handler.
<asp:GridView ID="gvRecepients" 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 Multiple Email" runat="server" OnClick="SendMultipleEmail" />
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.Net.Configuration;
VB.Net
Imports System.Data
Imports System.Net
Imports System.Net.Mail
Imports System.Configuration
Imports System.Net.Configuration
Populating the GridView
Inside the
Page_Load event handler, the
GridView is populated with dynamic
DataTable with some dummy data.
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");
gvRecepients.DataSource = dt;
gvRecepients.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = 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")
gvRecepients.DataSource = dt
gvRecepients.DataBind()
End If
End Sub
Sending Bulk (Mass) email using C# and VB.Net
When Send Multiple Email button is clicked, the email setting is read from SmtpSection section of Web.Config file.
Then, an object of MailMessage class is created and the Subject, Body and From address are set into its respective properties.
Next, 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 to the
To (recipient) address.
Finally, an object of the
SmtpClient class is created and the values of
Host and
Port are fetched from the
SmtpSection of the
Web.Config file and the Email is sent using
Sent method and a success message is displayed in
JavaScript Alert Message Box using
RegisterStartupScript method.
C#
protected void SendMultipleEmail(object sender, EventArgs e)
{
string subject = "Welcome Email";
string body = "Hello,<br /><br />Welcome to ASPSnippets<br /><br />Thanks.";
//Read SMTP section from Web.Config.
SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
using (MailMessage mm = new MailMessage())
{
//Setting Subject, Body andFrom.
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
mm.From = new MailAddress(smtpSection.From);
//Looping and fetching multiplerecepient's email address.
foreach (GridViewRow row in gvRecepients.Rows)
{
if ((row.FindControl("chkSelect") as CheckBox).Checked)
{
mm.To.Add((row.FindControl("lnkEmail") as HyperLink).Text);
}
}
//Sending email to allrecpients.
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = smtpSection.Network.Host;
smtp.EnableSsl = smtpSection.Network.EnableSsl;
NetworkCredential networkCred = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials;
smtp.Credentials = networkCred;
smtp.Port = smtpSection.Network.Port;
smtp.Send(mm);
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email sent.');", true);
}
}
}
VB.Net
Protected Sub SendMultipleEmail(sender As Object, e As EventArgs)
Dim subject As String = "Welcome Email"
Dim body As String = "Hello,<br /><br />Welcome to ASPSnippets<br /><br />Thanks."
'Read SMTP section from Web.Config.
Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
Using mm As MailMessage = New MailMessage()
'Setting Subject, Body andFrom.
mm.Subject = subject
mm.Body = body
mm.IsBodyHtml = True
mm.From = New MailAddress(smtpSection.From)
'Looping and fetching multiplerecepient's email address.
For Each row As GridViewRow In gvRecepients.Rows
If (TryCast(row.FindControl("chkSelect"), CheckBox)).Checked Then
mm.To.Add((TryCast(row.FindControl("lnkEmail"), HyperLink)).Text)
End If
Next
'Sending email to allrecpients.
Using smtp As SmtpClient = New SmtpClient()
smtp.Host = smtpSection.Network.Host
smtp.EnableSsl = smtpSection.Network.EnableSsl
Dim networkCred As NetworkCredential = New NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
smtp.Credentials = networkCred
smtp.Port = smtpSection.Network.Port
smtp.Send(mm)
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Email sent.');", True)
End Using
End Using
End Sub
Screenshot
Downloads