You can send an email to multiple recipients using MailMessage
in C#. Here's an example of how to do it:
// Create a new MailMessage object MailMessage message = new MailMessage(); // Add the sender email address message.From = new MailAddress("[email protected]"); // Add the recipient email addresses message.To.Add("[email protected]"); message.To.Add("[email protected]"); message.To.Add("[email protected]"); // Set the subject and body of the email message.Subject = "Test Email"; message.Body = "This is a test email."; // Create a new SmtpClient object SmtpClient client = new SmtpClient(); // Set the SMTP server details client.Host = "smtp.example.com"; client.Port = 587; client.EnableSsl = true; // Set the credentials for the SMTP server (if required) client.Credentials = new NetworkCredential("username", "password"); // Send the email client.Send(message);
In this example, we create a MailMessage
object and set the sender email address using the From
property. We then add the recipient email addresses using the To
property, which is a collection of MailAddress
objects. We add each recipient email address using the Add()
method of the To
collection.
We then set the subject and body of the email using the Subject
and Body
properties of the MailMessage
object.
Next, we create a SmtpClient
object and set the details for the SMTP server, including the host name, port number, and SSL settings. If the SMTP server requires authentication, we also set the credentials using the Credentials
property of the SmtpClient
object.
Finally, we send the email using the Send()
method of the SmtpClient
object.
Note that there are limits to the number of recipients you can add to a single email message. Depending on the email service provider and email client being used, this limit can vary. It's always a good practice to check the limits set by your email service provider and break down your recipients into multiple emails if necessary.
To send an email to multiple recipients using System.Net.Mail
in C#, you can simply add multiple email addresses to the MailMessage.To
property. Separate each email address with a comma (",") if you want to send the email to multiple recipients.
Here's an example of how to send an email to multiple recipients using System.Net.Mail
:
using System; using System.Net; using System.Net.Mail; public class Program { public static void Main() { // SMTP server settings string smtpHost = "your_smtp_host"; int smtpPort = 587; // Replace with the appropriate SMTP port for your server string smtpUsername = "your_smtp_username"; string smtpPassword = "your_smtp_password"; // Create a MailMessage MailMessage mailMessage = new MailMessage(); // Sender email address mailMessage.From = new MailAddress("[email protected]", "Sender Name"); // Multiple recipient email addresses (separated by comma) mailMessage.To.Add("[email protected]"); mailMessage.To.Add("[email protected]"); mailMessage.To.Add("[email protected]"); // Email subject and body mailMessage.Subject = "Test Email"; mailMessage.Body = "This is a test email sent to multiple recipients."; // SMTP client configuration SmtpClient smtpClient = new SmtpClient(smtpHost, smtpPort); smtpClient.EnableSsl = true; smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(smtpUsername, smtpPassword); try { // Send the email smtpClient.Send(mailMessage); Console.WriteLine("Email sent successfully."); } catch (Exception ex) { Console.WriteLine("Error sending email: " + ex.Message); } } }
In this example, we create a MailMessage
object and add multiple recipient email addresses using the To
property. We separate each email address with a comma (",") as shown in the mailMessage.To.Add(...)
lines.
The SmtpClient
is used to send the email. Make sure to replace the placeholders (e.g., your_smtp_host
, your_smtp_username
, your_smtp_password
, etc.) with your actual SMTP server settings.
When you run this code, the email will be sent to all the specified recipients in the To
property.
In ASP.NET, you can send an email with an attachment using the System.Net.Mail
namespace. Here's an example of how to send an email with an attachment:
// Create a MailMessage object MailMessage message = new MailMessage(); message.From = new MailAddress("[email protected]"); message.To.Add(new MailAddress("[email protected]")); message.Subject = "Test Email with Attachment"; message.Body = "This is a test email with an attachment."; // Add an attachment to the email Attachment attachment = new Attachment(@"C:\path\to\file.txt"); message.Attachments.Add(attachment); // Create a SmtpClient object and send the email SmtpClient client = new SmtpClient("smtp.example.com"); client.Port = 587; client.Credentials = new NetworkCredential("username", "password"); client.EnableSsl = true; client.Send(message); // Dispose the attachment attachment.Dispose();
In this example, we create a MailMessage
object with a sender address, a recipient address, a subject, and a body. We then add an attachment to the email by creating an Attachment
object with the path to the file to attach and adding it to the Attachments
collection of the MailMessage
object.
We then create a SmtpClient
object with the SMTP server address, port, and credentials, and send the email using the Send
method of the SmtpClient
object. Finally, we dispose the attachment to free up any resources used by the attachment.
Note that the SMTP server address, port, username, and password used in this example are for demonstration purposes only, and you will need to replace them with the values for your own SMTP server.