使用配置文件的 MailSettings 进行 SMTP 身份验证

SMTP Authentication with config file#39;s MailSettings(使用配置文件的 MailSettings 进行 SMTP 身份验证)

本文介绍了使用配置文件的 MailSettings 进行 SMTP 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 MailSettings 存储在 web.config 中,但是当我发送邮件时,我的 SMTP 服务器报告我需要使用身份验证.我在配置文件中有我的用户名/密码,但仍然失败.

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

System.Net.Mail.SmtpException

超出存储分配.这服务器响应是:请使用 smtp验证.看http://www.myISP.com/support/smtp-authentication.aspx

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

超出的存储分配"让我们困惑了很长时间,我们现在忽略它.使用 smtp 身份验证"似乎很重要.

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

推荐答案

编码方式和web.config only方式的区别在于后者有defaultCredentials="true" 设置.那是防止用户名和密码被用于验证,用这种方法.我认为可以通过将其设置为false"(或完全删除它,因为false"是默认值)来解决问题.

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

这篇关于使用配置文件的 MailSettings 进行 SMTP 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用配置文件的 MailSettings 进行 SMTP 身份验证