Provides properties and methods for constructing an e-mail message.
Provides properties and methods for constructing an e-mail attachment.
Provides properties and methods for sending messages.
private void sendMail()
MailMessage mailMessage = new MailMessage();
//Set sender and receiver(s)
mailMessage.From = "email@example.com";
mailMessage.To = "FirstName.LastName@gmail.com";
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");
//Set SMTP-Server : name or IP-address
SmtpMail.SmtpServer = "localhost";
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.