ASP.NET (C#) Sending Bulk Email

last edited on: 19-Jan-2018; tagged: c#,smtp


Here is a real word example of sending bulk email using SSL and gmail SMTP server. 

using System.Net.Mail;
MailMessage mail = new MailMessage();
String FromEmail = yourEmail@example.com;
mail.From = new MailAddress(FromEmail, "From Name");
mail.Subject = “Your Subject”;
string replytoEmail = “reply@example.com”;
mail.Headers.Add("Reply-To", replytoEmail);
mail.Body = “Body of the Email”;
mail.IsBodyHtml = true; // setting true if the email is in HTML format
// DataSet DS holding the data that we have retrived from database. Example: client table with Email address or student table with Email address
if(DS.Tables[0].Rows.Count>0)
{
for(int row=0;row<DS.Tables[0].Rows.Count;row++)
{
String ToEmail = DS.Tables[0].Rows[row][“Email”].ToString();
mail.To.Add(new MailAddress(ToEmail));
}
// Email addressess will show into the Email To filed as comma separated
}
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;   // 465 or 587
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("username", "password");  // Your gmail user name and password
smtp.Host = "smtp.gmail.com";  // SMTP server
smtp.Send(mail);

Related

C# DataTable

tagged: c#,datatable

C# Sending Email with SSL 465 or 587

tagged: c#,smtp