如何从 Web.config 读取 system.net/mailSettings/smtp [英] How to read system.net/mailSettings/smtp from Web.config

查看:60
本文介绍了如何从 Web.config 读取 system.net/mailSettings/smtp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 web.config 邮件设置:

This is my web.config mail settings:

<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>

这是我尝试从 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;

但凭据始终为空.我如何访问这些值?

But credentials are always null. How can i access these values?

推荐答案

因为没有人接受任何答案,其他人都没有为我工作:

Since no answer has been accepted, and none of the others worked for me:

using System.Configuration;
using System.Net.Configuration;
// snip...
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string username = smtpSection.Network.UserName;

这篇关于如何从 Web.config 读取 system.net/mailSettings/smtp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆