Introduction In ASP.Net 2.0 we should use System.Net.Mail class to send mails instead of System.Web.Mail class. System.Net.Mail is the namespace used to send email if we are using the 2.0 (or higher) .NET Framework.
Unlike System.Web.Mail, which was introduced in the 1.0 Framework, it is not built upon the CDO/CDOSYS libraries. Instead it is written from the ground up without any interop. Thus, the class System.Net.Mail, is not dependant upon other COM libraries. It introduces brand new classes for creating and sending email. Although some functionality has been removed, the new System.Net.Mail namespace is much more versatile than the older CDO dependant System.Web.Mail.
Coding Differences for sending mails between .NET 1.1 & 2.0 versions:
Paragraph Heading 1 Below is the old way of sending mails in .NET1.1:
MailMessage msgMain = new MailMessage(); msgMain.To = "toAddress@server.com"; msgMain.From = "fromAddress@server.com"; msgMain.Subject = "Hello subject"; msgMain.Body = "Hello body";
SmtpMail.SmtpServer = "172.16.9.14"; SmtpMail.Send(msgMain);
//We can set the relay server in code like this: //SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["SmtpServer"];
Paragraph Heading N In .NET2.0, all we have to do to convert is change our using statement from System.Web.Mail to System.Net.Mail and use and instance of SmtpClient class rather than the static SmtpMail class. Since, "To" property of MailMessage instance is read-only in .NET2.0, we can set "To" address through Add method as below. Also, we cannot assign "From" property directly to string-typed address, instead we should assign to instance of MailAddress class containing the "From" Address.
<system.net> <mailSettings> <smtp deliveryMethod="Network"> <network host="mail.mydomain.com" port="25" /> </smtp> <!-- Use this setting for development <smtp deliveryMethod="SpecifiedPickupDirectory"> <specifiedPickupDirectory pickupDirectoryLocation="C:\Temp" /> </smtp> --> </mailSettings> </system.net>
As in above web.config code, we can have it drop the messages in a temp folder when we are developing and have the production web.config relay the email through the main email server.
It’s also worth noting that the old System.Web.Mail.SmtpMail was single threaded. If we needed to do a lot of email processing it would get totally stuck. Take a look at what the Send Method on the old one did:
public static void Send(MailMessage message) { lock (SmtpMail._lockObject) { if (Environment.OSVersion.Platform != PlatformID.Win32NT) { throw new PlatformNotSupportedException(SR.GetString("RequiresNT")); } if (Environment.OSVersion.Version.Major <= 4) { SmtpMail.CdoNtsHelper.Send(message); } else { SmtpMail.CdoSysHelper.Send(message); } } }
As opposed to: public class SmtpClient { // Events public event SendCompletedEventHandler SendCompleted;
// Methods
public void Send(MailMessage message); public void Send(string from, string recipients, string subject, string body); public void SendAsync(MailMessage message, object userToken); public void SendAsync(string from, string recipients, string subject, string body, object userToken); public void SendAsyncCancel(); }
SmtpClient is not a static class like SmtpMail was. So we can have many email sends going at the same time.
Summary
|
No responses found. Be the first to respond and make money from revenue sharing program.
|