Smtp send mail c#

Here is an example of how to send an email using SMTP in C#:

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 recipient's email address
mail.To.Add("[email protected]");

// Set the subject of the email
mail.Subject = "Test email from C#";

// Set the body of the email
mail.Body = "This is a test email sent from C#";

// Create a new SmtpClient object
SmtpClient client = new SmtpClient();

// Set the SMTP server's address and port
client.Host = "smtp.example.com";
client.Port = 587;

// Set the SMTP server's username and password
client.Credentials = new NetworkCredential("your_smtp_username", "your_smtp_password");

// Send the email
client.Send(mail);

You'll need to replace the placeholders with your own values:

You can also specify additional settings, such as:

Here is an example of sending an email with HTML body:

mail.IsBodyHtml = true;
mail.Body = "<html><body>This is a test email sent from C#</body></html>";

And here is an example of sending an email with attachments:

Attachment attachment = new Attachment("path/to/attachment.txt");
mail.Attachments.Add(attachment);

You can also use the MailMessage class to send emails asynchronously using the SendAsync method:

client.SendAsync(mail, CancellationToken.None);

Make sure to handle any exceptions that may occur during the sending process, such as network errors or authentication failures.