Mailmessage c# multiple recipients
In C#, you can send a mail message 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 multiple recipients to the email
mail.To.Add("[email protected]");
mail.To.Add("[email protected]");
mail.To.Add("[email protected]");
// Set the body of the email
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_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 multiple 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 add multiple recipients to the blind carbon copy (BCC) list. The Bcc
property is used to send a copy of the email to multiple recipients without including their email addresses in the To
or CC
fields.
mail.Bcc.Add("[email protected]");
mail.Bcc.Add("[email protected]");
mail.Bcc.Add("[email protected]");
Note that when you use the Bcc
property, the email addresses of the recipients will not be visible to each other.
You can also use the CC
property to add multiple recipients to the carbon copy (CC) list.
mail.CC.Add("[email protected]");
mail.CC.Add("[email protected]");
mail.CC.Add("[email protected]");
The CC
property is used to send a copy of the email to multiple recipients, and their email addresses will be visible to each other.
It's worth noting that when sending emails to multiple recipients, you should be careful not to exceed the maximum number of recipients allowed by the SMTP server or the email service provider.