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!
Related articles
Related discussion
-
Buy cheap Xanax overnight. Cheap Xanax. Overnight delivery of Xanax in US no prescription needed. Cheapest Xanax.
by asleymar (0 replies)
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by asleymar (0 replies)
-
Cheap online order Fioricet. Cheap discount Fioricet. Offshore Fioricet online. How to buy Fioricet online without a prescription.
by asleymar (0 replies)
-
Buy Ambien no visa without prescription. Not expensive Ambien prescriptions. Ambien no rx. Cod delivery Ambien.
by asleymar (0 replies)
-
Tramadol without doctor rx. Buy Tramadol over the counter cod overnight. Cheap Tramadol cod delivery. Buy Tramadol from mexico online.
by asleymar (0 replies)
Related podcasts
-
Writing FaceBook Applications with .NET - Interview with Mel Sampat, author of Outsync
In this episode, Scott talks with Mel Sampat, a Program Manager at Microsoft who's written OutSync, an application that syncs faces between Outlook, Facebook, and indirectly Windows SmartPhones. They chat about what it takes to write your own FaceBook application using ASP.NET or WinForms.
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.
wonderful, Just what i needed.
Thanks Xavier
!--removed tag-->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 [:)]](/emoticons/emotion-1a.gif)
regards
David Xavier
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.
do we require a mail server fro sending mail through this program
ravi
This thread is for discussions of Sending Authenticated Emails in .NET 2.0.