How To send bulk email (mass, multiple email) using c#

 How To send bulk email (mass, multiple emails) using c#

In this article, you can send using Parallel ForEach Loop to send Bulk (Mass) emails by making use of Multi-Threading in ASP.Net.

Parallel.ForEach(dtCustomers.AsEnumerable(), row =>
    {
        SendEmail(row["Email"].ToString(), subject, string.Format(body, row["Name"]));
    });

 

Method:

private bool SendEmail(string recipient, string subject, string body)
{
   bool isSuccess =false;
   try{
    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);
    isSuccess =true;
    }catch(Exception ex)
    {
       // your log the exception 
       
    }
    return isSuccess ; 
}

2021-10-03