c# - 在 web.config 中设置多个 SMTP 设置?

标签 c# web-config smtp mailsettings

我正在构建一个应用程序,它需要在发送电子邮件时动态/以编程方式了解和使用不同的 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/

相关文章:

asp.net - web.config 转换未显示在 VS2010 中

asp.net - 通过 smtppickupfolder 使用 MS 企业库日志记录应用程序 block 发送电子邮件?

iis-7 - 如何配置 TFS 以与 GMail 一起发送 TFS 警报?

go - 我如何检测 golang 中的 net smtp 验证错误?

android - 利用 Google 帐户凭据在 Android 中发送电子邮件

c# - 使用 wshttpbinding 和用户名身份验证保护服务时,WCF 服务显示异常

c# - 使用sql server express为c#.net winform应用程序创建多个用户

c# - string.substring 与 string.take

javascript - Nodemailer 给出错误

c# - 实现搜索和突出显示 - 当突出显示缓慢时如何避免延迟?