A question came up recently on how to send email from .NET, System.Web.Mail offers a nice MailMessage and SmtpMail class that does the trick.

The classes are a wrapper over the CDOSYS functionality that's been around for a bit, and are much nicer than the rather clunky CDOSYS interface :)

It's really straight forward to send a mail:

System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();    
msg.Subject = "Testing";
msg.Body = "Hello World";
msg.From = "yourname@domain.com
msg.To = "

System.Web.Mail.SmtpMail.SmtpServer = "YOUREXCHANGESERVER";
System.Web.Mail.SmtpMail.Send( msg );

However when I was testing it out against an Exchange Server it refused to send, returning this error:


By default (and wisely) Exchange doesn't allow unauthenticated users to send mail via SMTP to prevent against spammers, etc.   To cut a very long story shot the SmtpMail class does not handle authentication to the Exchange server meaning that you can't use the class in most secure scenarios.

After a lot of digging I found that CDOSYS does have authentication code as you'd expect, however the MailMessage or SmtpMail class does not expose any way to turn it on.  After a bit more digging I found that the Everett (.NET Framework 1.1) team realised this and added a new property called Fields, which as you can see is missing some documentation ;-)

So, after some further investigation I've found that you can authenticate against a server, if you have Version 1.1 of the framework, and this is the line you have to add to the above code:


Where the 2 specifies NTLM, 1 for basic, 0 for none (the default)

You can of course also configure the myriad of other configuration options via this Fields collection

I feel a MSDN HOWTO article coming on, and perhaps polietly asking for some MSDN documentation to be uploaded! :-)