Setting multiple SMTP settings in web.config?(在 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 设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 web.config 中设置多个 SMTP 设置?


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01