简体   繁体   中英

SMTP Authentication with config file's MailSettings

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.

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM