Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Saturday, April 16

System.Web.Mail

The .NET namespace used to send email in .NET applications (web-applications and Windows Forms applications) is System.Web.Mail. This namespace contains three classes :
  • MailMessage
    Provides properties and methods for constructing an e-mail message.
  • MailAttachment
    Provides properties and methods for constructing an e-mail attachment.
  • SmtpMail
    Provides properties and methods for sending messages.
Constructing an e-mail message is very simple :

        private void sendMail()

        {

            //Create MailMessage-object

            MailMessage mailMessage = new MailMessage();

 

            //Set sender and receiver(s)

            mailMessage.From = "test@test.com";

            mailMessage.To = "FirstName.LastName@gmail.com";

 

            //mail-options

            mailMessage.Priority = MailPriority.Low;

            mailMessage.BodyFormat = MailFormat.Html;

 

            //Set subject and body

            mailMessage.Subject = "testmail";

            mailMessage.Body = "<b>Hello World</b>";

 

            //Add attachment to message

            MailAttachment attachment = new MailAttachment(@"c:\mailAttachment.doc");

            mailMessage.Attachments.Add(attachment);

 

            //Set SMTP-Server : name or IP-address

            SmtpMail.SmtpServer = "localhost";

 

            //Send Mail

            SmtpMail.Send(mailMessage);

        }



The first time I tried this to send an e-mail on my machine (localhost), I was confronted with the following error : Could not access 'CDO.Message' ojbect ...



After some googling, I found out that I had to change the "relay restrictions" on my Virtual SMTP Server. By default, the radio-button "Only the list below" is selected and you must add your ip-address to the list or you must select "All except the list below" to be able to send e-mail on your local machine.



Behind the scenes, the Collaboration Data Objects message component (CDOSYS : CDO.Message) is used for sending the e-mail messages. The cdosys.dll is installed by default on Windows 2003 and on Windows XP. So, System.Web.Mail is not a full .NET native implementation of the SMTP protocol ...

I remember that in the past (Windows NT 4) we had to use the cdonts.dll and this dll was only installed by the NT4 Option Pack or with the Personal Webserver (PWS). The mail message is delivered either through the SMTP mail service built into Windows or through an arbitrary SMTP server.

Out of curiosity, I used Reflector to see how the Send-method (SmtpMail class) was implemented and I was surprised to see that the cdonts.dll is still used to send e-mail messages, but only if your OS-version is smaller than or equals 4 (NT). If you look a little bit closer at the SmtpMail class, you can find the helper-classes for CdoNts and CdoSys. CdoNts creates a "CDONTS.NewMail" object and CdoSys creates a "CDO.Message" object.


2 Comments:

  • At 7:13 AM, Anonymous Anonymous said…

    The programming code is helpful but i still get following error messages like MailMessage class is obsolete

     
  • At 7:16 AM, Anonymous Anonymous said…

    i want to how a class becomes obsolete

     

Post a Comment

<< Home