用配置文件的MailSettings SMTP认证 [英] SMTP Authentication with config file's MailSettings
问题描述
我存储我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文件
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
验证。看到
<一href=\"http://www.myISP.com/support/smtp-authentication.aspx\">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.
推荐答案
在codeD方法和 web.config中只有的做法是,后者有 =的DefaultCredentials真实
集。那是$ P $被用来进行身份验证,这种做法pventing用户名和密码。我认为这个问题会通过设置为假来解决(或者完全删除,因为假是默认值)。
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认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!