在 web.config 中设置多个 SMTP 设置?

Setting multiple SMTP settings in web.config?(在 web.config 中设置多个 SMTP 设置?)
本文介绍了在 web.config 中设置多个 SMTP 设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序需要在发送电子邮件时以动态/编程方式了解并使用不同的 SMTP 设置.

I am building an app that needs to dynamically/programatically know of and use different SMTP settings when sending email.

我习惯于使用 system.net/mailSettings 方法,但据我了解,它一次只允许一个 SMTP 连接定义,由 SmtpClient() 使用.

I'm used to using the system.net/mailSettings approach, but as I understand it, that only allows one SMTP connection definition at a time, used by SmtpClient().

但是,我需要更多类似 connectionStrings 的方法,我可以在其中根据键/名称提取一组设置.

However, I need more of a connectionStrings-like approach, where I can pull a set of settings based on a key/name.

有什么建议吗?我愿意跳过传统的 SmtpClient/mailSettings 方法,我认为必须...

Any recommendations? I'm open to skipping the tradintional SmtpClient/mailSettings approach, and I think will have to...

推荐答案

我需要在 web.config 中有不同的 smtp 配置,具体取决于环境:dev、staging 和 production.

I needed to have different smtp configurations in the web.config depending on the environment: dev, staging and production.

这是我最终使用的:

在 web.config 中:

In web.config:

<configuration>
  <configSections>
    <sectionGroup name="mailSettings">
      <section name="smtp_1" type="System.Net.Configuration.SmtpSection"/>
      <section name="smtp_2" type="System.Net.Configuration.SmtpSection"/>
      <section name="smtp_3" type="System.Net.Configuration.SmtpSection"/>
    </sectionGroup>
  </configSections>
  <mailSettings>
    <smtp_1 deliveryMethod="Network" from="mail1@temp.uri">
      <network host="..." defaultCredentials="false"/>
    </smtp_1>
    <smtp_2 deliveryMethod="Network" from="mail2@temp.uri">
      <network host="1..." defaultCredentials="false"/>
    </smtp_2>
    <smtp_3 deliveryMethod="Network" from="mail3@temp.uri">
      <network host="..." defaultCredentials="false"/>
    </smtp_3>
  </mailSettings>
</configuration>

然后在代码中:

return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_1");
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_2");
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_3");

这篇关于在 web.config 中设置多个 SMTP 设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)