问题描述
我正在构建一个应用程序,该应用程序需要在发送电子邮件时以动态/编程方式了解并使用不同的 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 设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!