Library code snippets

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("xavier@devel.oping.net", "Xavier Larrea");

// The .To property is a generic collection,
// so we can add as many recipients as we like.
oMsg.To.Add(new MailAddress("fox@foxcorp.org","John Doe"));

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

SmtpClient oSmtp = new SmtpClient("smtp.myserver.com");

//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
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!

Comments

  1. 26 Jun 2009 at 07:40

    wonderful, Just what i needed.

    Thanks Xavier

  2. 27 Jan 2009 at 03:47
    hello...everyone im going to make a authentication & authorization page for our university.n i want to add some constraint like session generate & want to fetch his/her ipadress who want to access or trying to access this resources and if somebody trying to access illigly thn some mail or sms comes to the admin which notify the ipadress or userid which is attempting for access. can u give me some idea or techinques which helps me .
  3. 18 Apr 2007 at 09:38

    Hello friend,thanks for your codes at the first.  But a problem that if i want to use it in the Internet,

    just like the situation in your codes "smtp.myserver.com".  How could I do it?

    thank  you so much!!Smiley Face [:)]

  4. 13 Aug 2006 at 00:45
    yes you will require a particular mail server which will be your smtp server. if you are in india and a vsnl user you can use the vsnl smtp server.

    regards

    David Xavier
  5. 27 Jun 2006 at 13:55

    Hello Friends,

    I am working on a site(asp.net1.1 with c#), in which I have to send an automated mail to a particular user of the site based on some calculations on dates. That is if a user doesn't updates his profile every six months, the system must send an automated mail. Any one have some ideas??

    Thanks in advance

    Pramod.







  6. 13 Apr 2006 at 15:08

    do we require a mail server fro sending mail through this program

    ravi

  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Sending Authenticated Emails in .NET 2.0.

Leave a comment

Sign in or Join us (it's free).

Xavier Larrea

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

We'd love to hear what you think! Submit ideas or give us feedback