Hi smile,
Refer below code.
HTML
<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>
<table border="0">
<tr>
<td>File Attachments:</td>
<td>
<asp:FileUpload ID="fuAttachment" runat="server" AllowMultiple="true" /></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button Text="Send Bulk Email" runat="server" OnClick="SendBulkEmail" /></td>
</tr>
</table>
Code
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";
string body = "Hello {0},<br /><br />Welcome to ASPSnippets<br /><br />Thanks.";
List<Attachment> attachments = new List<Attachment>();
foreach (HttpPostedFile file in fuAttachment.PostedFiles)
{
string fileName = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath("~/Uploads/") + fileName);
attachments.Add(new Attachment(file.InputStream, fileName));
}
//Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dtCustomers.AsEnumerable(), row =>
{
SendEmail(row["Email"].ToString(), subject, string.Format(body, row["Name"]), attachments);
});
}
private bool SendEmail(string recipient, string subject, string body, List<Attachment> attachments)
{
MailMessage mm = new MailMessage("sender@gmail.com", recipient);
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
if (attachments.Count > 0)
{
foreach (Attachment attachment in attachments)
{
mm.Attachments.Add(attachment);
}
}
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;
}
Refer given article too.
Send email with multiple attachments using Multiple FileUpload control in ASP.Net 4.5