Ramakant Yadav the Internet edition

Random Ramblings Of Ramakant Yadav

11 Jun 2011



Things have certainly changed for a lot since the older CDONTS, CDOSYS. I remember messing around with CDONTS/CDOSYS on older systems. It was a beast. In the modern world, however, System.Net is the new and easy way to send email from any sort of windows system.

Here is how to send email from a SMTP client in google.

One has to use port 587 which is meant for TLS/STARTTLS. If one decides to enable ssl, the server has to advertise STARTTLS in response to ELHO. If it does not, then one has to do a lot of tinkering around with SmtpClient code. One also can kiss goodbye to any schedules previously set up.

Port 465 is used for SMTP over SSL and is not supported in SmtpClient until .netFramework 4.0. So if one needs to support SMTP over SSL, one will have to extend SmtpClient.

Of course, one has to use SendAsync with a callback method in the real world. This is just a sample. The signature for the SendAsync is also similar to Send, with the usual object as the second parameter.



///******************************************************

using (var SMTPClient = new SmtpClient("smtp.gmail.com", 587))

{

using (var MailMessage = BuildMailMessage())

{

SMTPClient.UseDefaultCredentials = false;

SMTPClient.Credentials = new NetworkCredential("username", "password");

SMTPClient.EnableSsl = true;

SMTPClient.Send(MailMessage);

}

}

///**********************************************************



blog comments powered by Disqus


The Archives | Contact me | Home


All Rights Reserved ramakantyadav.com