我将MailSettings存储在web.config中,但是,当我发送邮件时,我的SMTP服务器会报告我需要使用身份验证。我在配置文件中输入了用户名/密码,但是仍然失败。
如果执行以下操作,它会起作用,但这似乎是一个额外的步骤。它不应该只是从配置文件中获取并自动使用身份验证吗?
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
超出存储分配。的
服务器响应为:请使用smtp
身份验证。看到
http://www.myISP.com/support/smtp-authentication.aspx
“超出存储分配”使我们困惑了很长时间,现在我们将其忽略。似乎很重要的是“使用smtp身份验证”。
最佳答案
编码方法与纯Web.config方法之间的区别在于,后者设置了defaultCredentials="true"
。这样可以防止用户名和密码用于身份验证。我认为可以通过将其设置为“ false”(或将其完全删除,因为默认为“ false”)来解决。
关于asp.net - 带有配置文件的MailSettings的SMTP身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3621169/