SMTP Authentication with config file#39;s MailSettings(使用配置文件的 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 身份验证
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01