Home > Software design >  Send email through smtp in c# through more than one from email addresses and to more than one receiv
Send email through smtp in c# through more than one from email addresses and to more than one receiv

Time:01-09

I have project related school management system in c# using asp.net as framework and sql server as database.

I need to send email with same email body and subject (like: wish you a very happy new year) but i have different from addresses and obviously different to addresses.

For example: i need to send email to all teachers with [email protected] email and to all students with [email protected] email address and to management staff with [email protected]

How can i perform this task in c# and asp.net with efficient way?

CodePudding user response:

You can create SmtpClient and MailMessage object in order to send email.

I think you should call your mail sending method with 3 times with different parameters (from address and receivers list) Your methos looks like

SendMail("[email protected]", teacherList);
SendMail("[email protected]", studentList);
SendMail("[email protected]", staffList);


public static void SendMail(string fromAddress, List<string> emailAddresses)
{
  //make this smtpClient global. Because you will use next time.
  SmtpClient smtpClient = new SmtpClient();
  smtpClient.Credentials = new System.Net.NetworkCredential("smtpUserName", "smtpPassword");
  smtpClient.Host = "mail.mailhost.com";//set your smtp host. Generally mail.domain.com
  smtpClient.Port = 587;//set your smtp port
  smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
  smtpClient.EnableSsl = false;//you can change this based on your settings

  MailMessage mail = new MailMessage();
  mail.From = new MailAddress(fromAddress, "Our School Name");
  for (int i = 0; i < emaillAddresses.Count; i  )
  {
     mail.Bcc.Add(new MailAddress(emaillAddresses[i]));
  }
  mail.Subject = "Wish you a very happy new year";
  mail.IsBodyHtml = true;
  mail.BodyEncoding = System.Text.Encoding.UTF8;
  smtpClient.Send(mail);
}

CodePudding user response:

Try creating a collection of emails. Then use foreach to loop through and send the emails

  •  Tags:  
  • Related