得到“来自” web.config 中 asp.net smtp 客户端配置的属性值

发布于 2024-10-24 20:19:37 字数 439 浏览 0 评论 0原文

如何从 web.config 文件中的 asp.net SMTP 客户端配置获取“from”属性值?

<mailSettings>
  <smtp deliveryMethod="Network" from="[email protected]">
    <network host="somehosting" userName="someusername" password="somepassword"/>
  </smtp>
</mailSettings>

How to get the "from" attribute value from the asp.net SMTP client configuration in the web.config file?

<mailSettings>
  <smtp deliveryMethod="Network" from="[email protected]">
    <network host="somehosting" userName="someusername" password="somepassword"/>
  </smtp>
</mailSettings>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

临风闻羌笛 2024-10-31 20:19:37

像这样:

var mailSettings = (MailSettingsSectionGroup)WebConfigurationManager.GetSection("system.net/mailSettings");
string from = mailSettings.From;

Like this:

var mailSettings = (MailSettingsSectionGroup)WebConfigurationManager.GetSection("system.net/mailSettings");
string from = mailSettings.From;
远山浅 2024-10-31 20:19:37

另一种方法是使用命名部分:

应用程序配置:

<configuration>
  <configSections>
   <sectionGroup name="mailSettings">
    <section name="DefaultSmtpProvider" type="System.Net.Configuration.SmtpSection"/>
   </sectionGroup>
  </configSections>

 <mailSettings>
  <DefaultSmtpProvider from="[email protected]">
   <network host="@host" userName="@userName" password="@password" defaultCredentials ="false" />
  </DefaultSmtpProvider>
 </mailSettings>
</configuration>

初始化代码:

SmtpSection smtpSettings = (SmtpSection)ConfigurationManager.GetSection("mailSettings/DefaultSmtpProvider");
var message= new MailMessage(smtpSettings.From, recipientAddress};

这还允许您在一个配置中拥有多个 smtp 设置,以防您需要切换 smtpClients。我忘了声明,如果使用这种方法,您将必须手动构建 smtp 客户端:

new SmtpClient
        {
            DeliveryMethod = smtpSettings.DeliveryMethod,
            Host = smtpSettings.Network.Host,
            UseDefaultCredentials = smtpSettings.Network.DefaultCredentials,
            Credentials = new NetworkCredential(smtpSettings.Network.UserName, smtpSettings.Network.Password)
        };

Another approach is to use named sections:

Application config:

<configuration>
  <configSections>
   <sectionGroup name="mailSettings">
    <section name="DefaultSmtpProvider" type="System.Net.Configuration.SmtpSection"/>
   </sectionGroup>
  </configSections>

 <mailSettings>
  <DefaultSmtpProvider from="[email protected]">
   <network host="@host" userName="@userName" password="@password" defaultCredentials ="false" />
  </DefaultSmtpProvider>
 </mailSettings>
</configuration>

Init Code:

SmtpSection smtpSettings = (SmtpSection)ConfigurationManager.GetSection("mailSettings/DefaultSmtpProvider");
var message= new MailMessage(smtpSettings.From, recipientAddress};

This will also allow you to have multiple smtp settings within one config in case you ever need to switch smtpClients. I forgot to state, you will have to build up the smtp client manually if this approach is used:

new SmtpClient
        {
            DeliveryMethod = smtpSettings.DeliveryMethod,
            Host = smtpSettings.Network.Host,
            UseDefaultCredentials = smtpSettings.Network.DefaultCredentials,
            Credentials = new NetworkCredential(smtpSettings.Network.UserName, smtpSettings.Network.Password)
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文