Sending Authenticated Emails in .NET 2.0

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();

// Set the message sender
oMsg.From = new MailAddress("[email protected]", "Xavier Larrea");

// The .To property is a generic collection,
// so we can add as many recipients as we like.
oMsg.To.Add(new MailAddress("[email protected]","John Doe"));

// Set the content
oMsg.Subject = "My First .NET email";
oMsg.Body = "Test body - .NET Rocks!";
oMsg.IsBodyHtml = true;

SmtpClient oSmtp = new SmtpClient("");

//You can choose several delivery methods.
//Here we will use direct network delivery.
oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;

//Some SMTP server will require that you first
//authenticate against the server.

NetworkCredential oCredential = new NetworkCredential("myusername","mypassword");
oSmtp.UseDefaultCredentials = false;
oSmtp.Credentials = oCredential;

//Let's send it already

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!

You might also like...


Xavier Larrea


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In theory, theory and practice are the same. In practice, they're not.”