How to send mail to multiple email addresses in c#
In C#, you can send an email to multiple recipients using the MailMessage
class and the SmtpClient
class. Here's an example:
using System.Net.Mail;
// Create a new MailMessage object
MailMessage mail = new MailMessage();
// Set the sender's email address
mail.From = new MailAddress("[email protected]");
// Set the subject of the email
mail.Subject = "Test email with multiple recipients";
// Add the recipients to the email
mail.To.Add("[email protected]");
mail.To.Add("[email protected]");
mail.To.Add("[email protected]");
// Add the email body
mail.Body = "This is a test email sent to multiple recipients";
// Create a new SmtpClient object
SmtpClient client = new SmtpClient();
// Set the SMTP server settings
client.Host = "your_smtp_server";
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("[email protected]", "your_email_password");
// Send the email
client.Send(mail);
In this example, we create a new MailMessage
object and set the sender's email address, subject, and body. We then add the recipients to the To
collection using the Add
method. Finally, we create a new SmtpClient
object and set the SMTP server settings, and then send the email using the Send
method.
You can also use the Bcc
property to send the email to multiple recipients in blind carbon copy (BCC) mode, where the recipients' email addresses are not visible to each other:
mail.Bcc.Add("[email protected]");
mail.Bcc.Add("[email protected]");
mail.Bcc.Add("[email protected]");
Alternatively, you can use the CC
property to send the email to multiple recipients in carbon copy (CC) mode, where the recipients' email addresses are visible to each other:
mail.CC.Add("[email protected]");
mail.CC.Add("[email protected]");
mail.CC.Add("[email protected]");
Note that you should replace the placeholders ([email protected]
, your_smtp_server
, your_email_password
, etc.) with your actual email address, SMTP server, and email password.