How to send mail from c#

There are several ways to send mail from C#. Here are a few options:

Option 1: Using the System.Net.Mail namespace

You can use the System.Net.Mail namespace to send mail using the SmtpClient class. Here's an example:

using System.Net.Mail;

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

// Set the SMTP server and port
client.Host = "smtp.gmail.com";
client.Port = 587;

// Set the email address and password
client.EnableSsl = true;
client.Credentials = new NetworkCredential("your_email_address", "your_email_password");

// Create a new MailMessage object
MailMessage message = new MailMessage();
message.From = new MailAddress("your_email_address");
message.To.Add("recipient_email_address");
message.Subject = "Test email";
message.Body = "This is a test email";

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

Option 2: Using a third-party library

There are several third-party libraries available that make it easier to send mail from C#. Some popular options include:

Here's an example using MailKit:

using MailKit.Net.Smtp;

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

// Set the SMTP server and port
client.Connect("smtp.gmail.com", 587, true);

// Set the email address and password
client.Authenticate("your_email_address", "your_email_password");

// Create a new MailMessage object
MailMessage message = new MailMessage();
message.From = new MailAddress("your_email_address");
message.To.Add("recipient_email_address");
message.Subject = "Test email";
message.Body = "This is a test email";

// Send the email
client.Send(message);
client.Disconnect(true);

Option 3: Using a mail service API

Some mail services, such as SendGrid and Mailgun, provide APIs that allow you to send mail programmatically. Here's an example using SendGrid's API:

using SendGrid;

// Create a new SendGrid client object
SendGridClient client = new SendGridClient("your_sendgrid_api_key");

// Create a new Mail object
Mail message = new Mail();
message.From = new Email("your_email_address");
message.To.Add(new Email("recipient_email_address"));
message.Subject = "Test email";
message.Text = "This is a test email";

// Send the email
client.SendEmailAsync(message);

Note that you'll need to replace the placeholders (your_email_address, your_email_password, etc.) with your actual email credentials and settings. Additionally, you may need to configure your email client or server to allow outgoing mail connections.