Use below modified code.
VB.Net
Private conn As String = ConfigurationManager.ConnectionStrings("***").ConnectionString
Protected Sub EmailfromTable()
Using sqlCon As New SqlConnection(conn)
Using cmd As New SqlCommand()
cmd.CommandText = "Select Username, Email from users"
cmd.Connection = sqlCon
sqlCon.Open()
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
sqlCon.Close()
Dim subject As String = ESubject.Text.Trim
Dim body As String = "Hello {0}, <br /><br />" & EBody.Text.Trim
Dim fileName As String = ""
Dim bytes As Byte() = New Byte() {}
If fuAttachment.HasFile Then
fileName = Path.GetFileName(fuAttachment.PostedFile.FileName)
Dim binaryReader = New BinaryReader(fuAttachment.PostedFile.InputStream)
bytes = binaryReader.ReadBytes(fuAttachment.PostedFile.ContentLength)
End If
'Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dt.AsEnumerable(),
Function(row)
Return SendEmail(row("Email").ToString(), subject, String.Format(body, row("UserName")), bytes, fileName)
End Function)
End Using
End Using
ClientScript.RegisterStartupScript(Me.GetType, "alert", "alert('Email sent.');", True)
End Sub
Private Function SendEmail(recipient As String, subject As String, body As String, bytes As Byte(), fileName As String) As Boolean
Dim mm As New MailMessage("*****", recipient)
mm.Subject = subject
mm.Body = body
mm.IsBodyHtml = True
If Not String.IsNullOrEmpty(fileName) Then
mm.Attachments.Add(New Attachment(New MemoryStream(bytes), fileName))
End If
Dim smtp As New SmtpClient()
smtp.Host = "*********"
smtp.EnableSsl = True
Dim NetworkCred As New NetworkCredential()
NetworkCred.UserName = "*********"
NetworkCred.Password = "******"
smtp.UseDefaultCredentials = False
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
Return True
End Function
Protected Sub SendBulkEmail(sender As Object, e As EventArgs) Handles Button1.Click
EmailfromTable()
End Sub
C#
private string conn = ConfigurationManager.ConnectionStrings("***").ConnectionString;
protected void EmailfromTable()
{
using (SqlConnection sqlCon = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "Select Username, Email from users";
cmd.Connection = sqlCon;
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
sqlCon.Close();
string subject = ESubject.Text.Trim;
string body = "Hello {0}, <br /><br />" + EBody.Text.Trim;
string fileName = "";
byte[] bytes = new byte[] { };
if (fuAttachment.HasFile)
{
fileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
var binaryReader = new BinaryReader(fuAttachment.PostedFile.InputStream);
bytes = binaryReader.ReadBytes(fuAttachment.PostedFile.ContentLength);
}
// Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dt.AsEnumerable(), row =>
{
return SendEmail(row("Email").ToString(), subject, string.Format(body, row("UserName")), bytes, fileName);
});
}
}
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Email sent.');", true);
}
private bool SendEmail(string recipient, string subject, string body, byte[] bytes, string fileName)
{
MailMessage mm = new MailMessage("*****", recipient);
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
if (!string.IsNullOrEmpty(fileName))
{
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), fileName));
}
SmtpClient smtp = new SmtpClient();
smtp.Host = "*********";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "*********";
NetworkCred.Password = "******";
smtp.UseDefaultCredentials = false;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
return true;
}
protected void SendBulkEmail(object sender, EventArgs e)
{
EmailfromTable();
}