问题描述
我建立,需要动态/编程知道和发送电子邮件时使用不同的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.
任何建议?我打开跳过tradintional SmtpClient / mailSettings的做法,我认为将不得不...
Any recommendations? I'm open to skipping the tradintional SmtpClient/mailSettings approach, and I think will have to...
推荐答案
我需要有在web.config不同的SMTP配置根据不同的环境:开发,分期和生产
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>
然后在code:
Then in code:
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_1");
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_2");
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_3");
这篇关于在web.config中设置多个SMTP设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!