ner*_*mik 59 c# smtp sendmail web-config
这是我的web.config邮件设置:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="smthg@smthg.net">
<network defaultCredentials="true" host="localhost" port="587" userName="smthg@smthg.net" password="123456"/>
</smtp>
</mailSettings>
</system.net>
Run Code Online (Sandbox Code Playgroud)
这是我如何尝试从中读取值 web.config
var smtp = new System.Net.Mail.SmtpClient();
var credential = new System.Net.Configuration.SmtpSection().Network;
string strHost = smtp.Host;
int port = smtp.Port;
string strUserName = credential.UserName;
string strFromPass = credential.Password;
Run Code Online (Sandbox Code Playgroud)
但凭据始终为空.我如何访问这些值?
Dar*_*ith 104
由于没有接受任何答案,其他人都没有为我工作:
using System.Configuration;
using System.Net.Configuration;
// snip...
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string username = smtpSection.Network.UserName;
Run Code Online (Sandbox Code Playgroud)
ber*_*rtl 37
没有必要ConfigurationManager手动使用和获取值.简单地实例化SmtpClient就足够了.
SmtpClient client = new SmtpClient();
Run Code Online (Sandbox Code Playgroud)
这就是MSDN所说的:
这个构造函数通过在应用程序或设备配置文件中的设置初始化主机,凭证和港口新SmtpClient属性.
斯科特格思里不久前写了一篇小帖子.
通过使用配置,以下行:
var smtp = new System.Net.Mail.SmtpClient();
Run Code Online (Sandbox Code Playgroud)
将使用配置的值 - 您无需再次访问和分配它们.
至于null值 - 您正在尝试错误地访问配置值.您只是创建一个空SmtpSection而不是从配置中读取它.
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("<the section name>");
var credentials == smtpSection.Network;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53652 次 |
| 最近记录: |