Refer the below sample code for your reference and implement it as per your code logic in your code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #CCE5FB;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<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" />
</form>
</body>
</html>
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();
}
}
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";
//Set path from where you need to use image
//You code as per your requirement to get the image path and pass it to GrtImageUrl Function
string imageUrl = GetImageUrl("Images/Tulips.jpg");
string imageContent = "<img src='" + imageUrl + "' alt='Alternate Text' />";
string body = "Hello {0},<br /><br />Welcome to ASPSnippets<br /><br />" + imageContent + "<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;
}
// Your code from where you can set imageurl
private string GetImageUrl(string imagePath)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(imagePath));
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Png);
Byte[] bytes = new Byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(bytes, 0, (int)bytes.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
string imageUrl = "data:image/png;base64," + base64String;
return imageUrl;
}
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
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
'Set path from where you need to use image
'You code as per your requirement to get the image path and pass it to GrtImageUrl Function
Dim imageUrl As String = GetImageUrl("Images/Tulips.jpg")
Dim imageContent As String = "<img src='" + imageUrl + "' alt='Alternate Text' />"
Dim subject As String = "Welcome Email"
Dim body As String = "Hello {0},<br /><br />Welcome to ASPSnippets<br /><br />" + imageContent + "<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
'Your code from where you can set imageurl
Private Function GetImageUrl(ByVal imagePath As String) As String
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(imagePath))
Dim memoryStream As MemoryStream = New MemoryStream()
image.Save(memoryStream, ImageFormat.Png)
Dim bytes As Byte() = New Byte(memoryStream.Length - 1) {}
memoryStream.Position = 0
memoryStream.Read(bytes, 0, CInt(bytes.Length))
Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)
Dim imageUrl As String = "data:image/png;base64," & base64String
Return imageUrl
End Function
Screenshot
