Sending an email in C# 2.0 is easier than ever before.
The example below takes advantage of the new "using" statement, which handles all the "tear down" work involved in closing connections and disposing of objects no longer required. In addition, the exception handling could be expanded to catch specific exceptions that are generated by the SmtpClient class.
using (System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage())
{
mailMessage.From = "youremail@yourdomain.com";
mailMessage.To = "receiversEmail@receiversDomain.com";
mailMessage.Subject = "Your subject";
mailMessage.Body = "Your body";
try
{
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("your server address");
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
//handle exception
}
}
Be the first to rate this post
- Currently 0/5 Stars.
- 1
- 2
- 3
- 4
- 5