Sdílet prostřednictvím


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("https://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

  • Anonymous
    October 22, 2003
    Darren, the link to your RSS feed returns a "404" error.Eric
  • Anonymous
    November 01, 2003
    Fixed! - Thanks for pointing it out!
  • Anonymous
    May 14, 2004
    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.
  • Anonymous
    June 11, 2004
    You wonderful man.... Thanks
  • Anonymous
    June 14, 2004
    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
  • Anonymous
    June 14, 2004
    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
  • Anonymous
    July 03, 2004
    but i dont have the fields property in .net. isn't any way out for me in the current version???
  • Anonymous
    January 22, 2009
    PingBack from http://www.hilpers.fr/962469-smtp-err-550-framework-2-a