Hi shal,
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox runat="server" ID="txtemailfrom" />
<asp:TextBox runat="server" ID="txtsubject" Text="Test Subject" />
<asp:TextBox runat="server" ID="txtbody" Text="Test Body" />
<asp:FileUpload ID="fileUploader" runat="server" />
<asp:Button Text="Save" runat="server" OnClick="OnSave" />
Namepsces
C#
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
VB.Net
Imports System.Data
Imports System.IO
Imports System.Linq
Imports System.Net
Imports System.Net.Mail
Imports System.Threading.Tasks
Code
C#
protected void OnSave(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("PERSONAL_EMAILID"), new DataColumn("ENAME"), new DataColumn("OFFICIAL_EMAILID") });
dt.Rows.Add("test1@gmail.com", "Test 1", "test2@gmail.com");
dt.Rows.Add("test3@gmail.com", "Test 2", "test4@gmail.com");
Stream fs = fileUploader.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string fileName = Path.GetFileName(fileUploader.PostedFile.FileName);
Parallel.ForEach(dt.AsEnumerable(), row =>
{
SendEmail(row["PERSONAL_EMAILID"].ToString(), txtsubject.Text, string.Format("{0}", row["ENAME"]), fileName, bytes);
SendEmail(row["OFFICIAL_EMAILID"].ToString(), txtsubject.Text, string.Format("{0}", row["ENAME"]), fileName, bytes);
});
}
private void SendEmail(string recipient, string subject, string body, string fileName, byte[] bytes)
{
string from = txtemailfrom.Text;
using (MailMessage mail = new MailMessage(from, recipient))
{
mail.Subject = subject;
mail.Body = body;
if (bytes.Length > 0)
{
mail.Attachments.Add(new Attachment(new MemoryStream(bytes), fileName));
}
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential networkCredential = new NetworkCredential(from, "password");
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Port = 25;
smtp.Send(mail);
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Message has been sent successfully.');window.location ='SendBulkMail.aspx';", true);
}
}
VB.Net
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("PERSONAL_EMAILID"), New DataColumn("ENAME"), New DataColumn("OFFICIAL_EMAILID")})
dt.Rows.Add("test1@gmail.com", "Test 1", "test2@gmail.com")
dt.Rows.Add("test3@gmail.com", "Test 2", "test4@gmail.com")
Dim fs As Stream = fileUploader.PostedFile.InputStream
Dim br As BinaryReader = New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Int32))
Dim fileName As String = Path.GetFileName(fileUploader.PostedFile.FileName)
Parallel.ForEach(dt.AsEnumerable(), Function(row)
SendEmail(row("PERSONAL_EMAILID").ToString(), txtsubject.Text, String.Format("{0}", row("ENAME")), fileName, bytes)
SendEmail(row("OFFICIAL_EMAILID").ToString(), txtsubject.Text, String.Format("{0}", row("ENAME")), fileName, bytes)
End Function)
End Sub
Private Sub SendEmail(ByVal recipient As String, ByVal subject As String, ByVal body As String, ByVal fileName As String, ByVal bytes As Byte())
Dim from As String = txtemailfrom.Text
Using mail As MailMessage = New MailMessage(from, recipient)
mail.Subject = subject
mail.Body = body
If bytes.Length > 0 Then
mail.Attachments.Add(New Attachment(New MemoryStream(bytes), fileName))
End If
mail.IsBodyHtml = False
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim networkCredential As NetworkCredential = New NetworkCredential(from, "password")
smtp.UseDefaultCredentials = True
smtp.Credentials = networkCredential
smtp.Port = 25
smtp.Send(mail)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alert", "alert('Message has been sent successfully.');window.location ='SendBulkMail.aspx';", True)
End Using
End Sub