Sending mail with System.Web.Mail (.Net 1.1)


If you thought that it was a serious shortcoming that .Net framework 1.1 wasn’t able to send email through SMTP servers that used authentication, you are in grave ignorance. On the surface of it, yes, you cannot send an email with the standard System.Web.Mail.MailMessage class; but you can of course play tricks on the Exchange server (yeah, that’s what I get out of it that this things works only on an Exchange server) to make it authenticate you.

There is a namespace called http://schemas.microsoft.com/cdo/configuration that does those funny (and highly interesting) tricks. First we would check how to use a simple SMTP server (no authentication), and then we will use authentication to send a message.

//using System.Web;
//using System.Web.Mail;

//The simple SMTP Server
MailMessage message = new MailMessage();
message.From = “src@someone.com”;
message.To = “dest@someother.com”;
message.BodyFormat = MailFormat.Html;
message.Subject = “This is a spooky mail!”;
message.Body = “Heheh, just kidding!”;

SmtpMail.SmtpServer = “your.server.address”
SmtpMail.Send(message);

Now lets check out a server that uses authentication. Microsoft provides the CDOSYS object to achieve just that. You wouldn’t think they were so dumb that they didnt incorporate some lousy authentication mechanism! Add the following lines before the SmtpMail lines.


message.Fields[
"http://schemas.microsoft.com/cdo/configuration/smtpserver”

] = “your.server.address”;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/smtpserverport”%5D = 25;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/sendusing”%5D = 2;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”%5D = 1;

//Most of the times the @domain is necessary with the username
message.Fields[“http://schemas.microsoft.com/cdo/configuration/sendusername”%5D = “username@domain.com”;
message.Fields[
http://schemas.microsoft.com/cdo/configuration/sendpassword”%5D = “urpassword”;

//If only the server uses SSL, in this case, the smtpserverport field would be different too
message.Fields[“http://schemas.microsoft.com/cdo/configuration/smtpusessl”%5D = true;

And that would do just fine. There you go, I just saved your time and money!

[P.S. To those whom these lines look like Greek, I must say that this post appears as a record!]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: