我正在构建一个应用程序,它需要在发送电子邮件时动态/以编程方式了解和使用不同的 SMTP 设置。
我习惯于使用 system.net/mailSettings 方法,但据我了解,它一次只允许一个 SMTP 连接定义,由 SmtpClient() 使用。
但是,我需要更多类似 connectionStrings 的方法,我可以在其中根据键/名称提取一组设置。
有什么建议吗?我愿意跳过传统的 SmtpClient/mailSettings 方法,我认为必须...
最佳答案
我需要根据环境在 web.config 中使用不同的 smtp 配置:开发、暂存和生产。
这是我最终使用的:
在 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");
关于c# - 在 web.config 中设置多个 SMTP 设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4363038/