﻿# Enabling SMTP authentication with CDOSYS (System.Web.Mail)

<datetime class="hidden">2004-02-20T00:00</datetime>
<!-- category -- mostlylucidcouk, Imported, ASP.NET, C# -->

One of the most common problems with using [CDOSYS](http://msdn.microsoft.com/library/en-us/cdosys/html/_cdosys_about_cdo_for_windows_2000.asp)  (which is the underlying class used by [System.Web.Mail)](http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebmail.asp) is the lack of authentication for sending mail via SMTP, [Darren Jefford](http://blogs.gotdotnet.com/darrenj/) [posted a method](http://blogs.gotdotnet.com/darrenj/permalink.aspx/abb9e979-98dd-4172-b07c-2c4919ed94bf) to do this, I've reproduced it below since it's currently hosted on GotDOtNwet and so may disappear at some point:


### System.Web.Mail and Authentication

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](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_techsel_tech_1.asp)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 = "* *someone@domain.com* *";* 

*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: 

*System.Runtime.InteropServices.COMException (0x8004020E): The server rejected the sender address. The server response was: 454 5.7.3 Client does not have permission to submit mail to this server.* 

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](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration_smtpauthenticate.asp)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](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebmailmailmessageclassfieldstopic.asp)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: 

msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",2); 

Where the 2 specifies NTLM, 1 for basic, 0 for none (the default) 

You can of course also configure the [myriad](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration.asp)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! :-)