Send email using smtp c#
Here is an example of how to send an email using SMTP in C#:
using System.Net.Mail;
using System.Net;
// 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";
// Set the body of the email
mail.Body = "This is a test email sent using 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 credentials (if required)
client.Credentials = new NetworkCredential("your_smtp_username", "your_smtp_password");
// Send the email
client.Send(mail);
You can also use the MailMessage
class to send emails with attachments, HTML content, and more.
Here is an example of how to send an email with an attachment:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Email with Attachment";
mail.Body = "This is a test email sent using C# with an attachment";
// Add an attachment to the email
Attachment attachment = new Attachment("path/to/attachment.txt", "text/plain");
mail.Attachments.Add(attachment);
SmtpClient client = new SmtpClient();
client.Host = "smtp.example.com";
client.Port = 587;
client.Credentials = new NetworkCredential("your_smtp_username", "your_smtp_password");
client.Send(mail);
And here is an example of how to send an email with HTML content:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Email with HTML Content";
mail.IsBodyHtml = true;
// Set the HTML content of the email
mail.Body = "<html><body>This is a test email sent using C# with HTML content</body></html>";
SmtpClient client = new SmtpClient();
client.Host = "smtp.example.com";
client.Port = 587;
client.Credentials = new NetworkCredential("your_smtp_username", "your_smtp_password");
client.Send(mail);
Make sure to replace the placeholders ([email protected]
, [email protected]
, smtp.example.com
, your_smtp_username
, and your_smtp_password
) with your actual email addresses and SMTP server credentials.
Also, you can use System.Net.Mail.SmtpClient
class to send email, it's a more simple way to send email.
SmtpClient client = new SmtpClient();
client.Host = "smtp.example.com";
client.Port = 587;
client.Credentials = new NetworkCredential("your_smtp_username", "your_smtp_password");
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Email";
mail.Body = "This is a test email sent using C#";
client.Send(mail);
You can also use System.Net.Mail.MailMessage
class to send email, it's a more flexible way to send email.
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Email";
mail.Body = "This is a test email sent using C#";
SmtpClient client = new SmtpClient();
client.Host = "smtp.example.com";
client.Port = 587;
client.Credentials = new NetworkCredential("your_smtp_username", "your_smtp_password");
client.Send(mail);
You can also use System.Net.Mail.SmtpClient
class to send email with attachments, HTML content, and more.
SmtpClient client = new SmtpClient();
client.Host = "smtp.example.com";
client.Port = 587;
client.Credentials = new NetworkCredential("your_smtp_username", "your_smtp_password");
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Email with Attachment";
mail.Body = "This is a test email sent using C# with an attachment";
Attachment attachment = new Attachment("path/to/attachment.txt", "text/plain");
mail.Attachments.Add(attachment);
client.Send(mail);
You can also use System.Net.Mail.MailMessage
class to send email with attachments, HTML content, and more.
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Email with Attachment";
mail.Body = "This is a test email sent using C# with an attachment";
Attachment attachment = new Attachment("path/to/attachment.txt", "text/plain");
mail.Attachments.Add(attachment);
SmtpClient client = new SmtpClient();
client.Host = "smtp.example.com";
client.Port = 587;
client.Credentials = new NetworkCredential("your_smtp_username", "your_smtp_password");
client.Send(mail);
Make sure to replace the placeholders ([email protected]
, [email protected]
, smtp.example.com
, your_smtp_username
, and your_smtp_password
) with your actual email addresses and SMTP server credentials.