Welcome to MSDN Blogs Sign in | Join | Help

Darren Jefford

Solution Architect, Microsoft Consulting Services, Microsoft UK
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! :-)

Posted: Wednesday, October 22, 2003 9:04 AM by darrenj
Filed under:

Comments

Eric said:

Darren, the link to your RSS feed returns a "404" error. Eric
# October 22, 2003 6:56 PM

Darren said:

Fixed! - Thanks for pointing it out!
# November 1, 2003 12:57 PM

Jitender Bahri said:

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.
# May 14, 2004 9:15 AM

Diesel said:

You wonderful man.... Thanks
# June 11, 2004 1:56 PM

Peter Aveyard said:

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
# June 14, 2004 9:30 AM

Darren Jefford said:

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
# June 14, 2004 11:22 AM

Qaiser said:

but i dont have the fields property in .net. isn't any way out for me in the current version???
# July 3, 2004 12:36 PM
Anonymous comments are disabled
Page view tracker