18

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="me@xyz.com" 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.

Owen Blacker
  • 4,117
  • 2
  • 33
  • 70
Eric
  • 1,882
  • 3
  • 16
  • 21

2 Answers2

30

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).

Mike Green
  • 2,031
  • 2
  • 18
  • 16
  • More on using the above setting at: https://www.smarterasp.net/support/kb/a380/mailsettings-defaultcredentials-setup-in-web_config.aspx ;) – Zeek2 Jan 08 '21 at 12:01
1

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?

Peter Mourfield
  • 1,885
  • 1
  • 19
  • 38