Sending e-mails in the .NET Framework 2.0 is about the same as in
version 1.x. There are just a couple of variations. First, all the
functionality is within the new System.Net.Mail namespace. The System.Web.Mail namespace, wich was used in the 1.x frameworks is now considered obsolete.
Lets get right to the code. It's really straight forward and self explanatory:
MailMessage oMsg = new MailMessage();
oMsg.From = new MailAddress("xavier@devel.oping.net", "Xavier Larrea");
oMsg.To.Add(new MailAddress("fox@foxcorp.org","John Doe"));
oMsg.Subject = "My First .NET email";
oMsg.Body = "Test body - .NET Rocks!";
oMsg.IsBodyHtml = true;
SmtpClient oSmtp = new SmtpClient("smtp.myserver.com");
oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential oCredential = new NetworkCredential("myusername","mypassword");
oSmtp.UseDefaultCredentials = false;
oSmtp.Credentials = oCredential;
oSmtp.Send(oMsg);
Very easy, right? Remember always to use the Try-Catch
block when sending emails because lot of things can cause an exception:
bad email addresses, authentication errors, network failure, etc.
I hope you find this code useful. Happy coding!