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

编程入门 行业动态 更新时间:2024-10-10 10:27:45
本文介绍了使用配置文件的 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/mailSettings"); SmtpClient client = new SmtpClient(); client.Credentials = new NetworkCredential( settings.Smtp.Network.UserName, settings.Smtp.Network.Password);

Web.config

<system> <mailSettings> <smtp from="me@xyz" deliveryMethod="Network"> <network host="mail.xyz" defaultCredentials="true" userName="me@xyzcom" password="abc123" /> </smtp> </mailSettings> </system>

System.Net.Mail.SmtpException

System.Net.Mail.SmtpException

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

Exceeded storage allocation. The server response was: Please use smtp authentication. See www.myISP/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 身份验证

本文发布于:2023-05-31 14:01:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/393047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:配置文件   身份验证   MailSettings   SMTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!