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 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 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:

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 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! 🙂

Comments (8)

  1. Eric says:

    Darren, the link to your RSS feed returns a "404" error.

    Eric

  2. Darren says:

    Fixed! – Thanks for pointing it out!

  3. Jitender Bahri says:

    Still doesn’t help….Error message : The server response was: 454 5.7.3 Client does not have permission to submit mail to this server.

    is still coming. 🙁

    After reading this article I thought I have got the solution.

  4. Diesel says:

    You wonderful man…. Thanks

  5. Peter Aveyard says:

    No, I still got the same error:

    Could not access ‘CDO.Message’ object.

    System.Runtime.InteropServices.COMException (0x8004020E): The server rejected the sender address. The server response was: 505 Authentication required

  6. Darren Jefford says:

    This is unusual – Are you sure the account your code is running under has permission to send messages on that Email server? (i.e. Relaying)

    You can test this by following theses steps:

    http://support.microsoft.com/?id=286421

    You could also try changing your smtp server to just the name rather than the fully qualified domain name, e.g. mymailserver instead of mymailserver.myserver.com

  7. Qaiser says:

    but i dont have the fields property in .net. isn’t any way out for me in the current version???