SMTP Authentication with config file's MailSettings

40.7k views Asked by At

I'm storing my MailSettings in a web.config, however when I send the message, my SMTP server reports back that I need to use authentication. I've got my username/password in the config file, but it still fails.

It works if I do the following, but it seems like an extra step. Shouldn't it just take it from the config file and use authentication automatically?

System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(
    HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings =
    (MailSettingsSectionGroup) config.GetSectionGroup("system.net/mailSettings");

SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential(
   settings.Smtp.Network.UserName, settings.Smtp.Network.Password);

Web.config

<system.net>
    <mailSettings>
        <smtp from="[email protected]" deliveryMethod="Network">
            <network host="mail.xyz.com" defaultCredentials="true"
                userName="me@xyzcom" password="abc123" />
        </smtp>
    </mailSettings>
 </system.net>

System.Net.Mail.SmtpException

Exceeded storage allocation. The server response was: Please use smtp authentication. See http://www.myISP.com/support/smtp-authentication.aspx

The "Exceeded storage allocation" confused us for quite awhile, we now ignore it. It's the "use smtp authentication" that seems to be important.

2

There are 2 answers

0
Peter Mourfield On

The SmtpClient class should use the auth parameters without you needing to explicitly read the username or password out of the config. See http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

Can you post the System.Net segment from your config? Also, can you post the exact error that you're receiving from the SMTP server?

1
Mike Green On

The difference between the coded approach and the web.config only approach is that the latter has defaultCredentials="true" set. That is preventing the username and password from being used to authenticate, with that approach. I think the problem would be solved by setting that to “false” (or removing it completely, because “false” is the default).