在web.config中设置多个SMTP设置?

alp*_*ogg 37 c# smtp web-config mailsettings

我正在构建一个应用程序,需要动态/编程地知道并在发送电子邮件时使用不同的SMTP设置.

我习惯使用system.net/mailSettings方法,但据我所知,它只允许一次一个SMTP连接定义,由SmtpClient()使用.

但是,我需要更多类似connectionStrings的方法,我可以根据键/名称提取一组设置.

有什么建议?我愿意跳过传统的SmtpClient/mailSettings方法,我认为必须......

小智 64

我需要在web.config中有不同的smtp配置,具体取决于环境:dev,staging和production.

这是我最终使用的内容:

在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>
Run Code Online (Sandbox Code Playgroud)

然后在代码中:

return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_1");
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_2");
return (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_3");
Run Code Online (Sandbox Code Playgroud)

  • @Mikko你的代码没有得到很好的解释.如何使用返回的SmtpSection? (20认同)

小智 25

SmtpSection smtpSection =  (SmtpSection)ConfigurationManager.GetSection("mailSettings/smtp_1");

SmtpClient smtpClient = new SmtpClient(smtpSection.Network.Host, smtpSection.Network.Port);
smtpClient.Credentials = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
Run Code Online (Sandbox Code Playgroud)


ITm*_*eze 10

这是我如何使用它,它对我来说很好(设置类似于Mikko答案):

  1. 首先设置配置部分:

    <configuration>
      <configSections>
        <sectionGroup name="mailSettings">
          <section name="default" type="System.Net.Configuration.SmtpSection" />
          <section name="mailings" type="System.Net.Configuration.SmtpSection" />
          <section name="partners" type="System.Net.Configuration.SmtpSection" />
        </sectionGroup>
      </configSections>
    <mailSettings>
      <default deliveryMethod="Network">
        <network host="smtp1.test.org" port="587" enableSsl="true"
               userName="test" password="test"/>
      </default>
      <mailings deliveryMethod="Network">
        <network host="smtp2.test.org" port="587" enableSsl="true"
               userName="test" password="test"/>
      </mailings>
    <partners deliveryMethod="Network">
      <network host="smtp3.test.org" port="587" enableSsl="true"
               userName="test" password="test"/>
    </partners>
    
    Run Code Online (Sandbox Code Playgroud)

  2. 那么最好创建一种包装器.请注意,大部分的代码下面是从.NET源代码采取SmtpClient 这里

    public class CustomSmtpClient
    {
        private readonly SmtpClient _smtpClient;
    
        public CustomSmtpClient(string sectionName = "default")
        {
            SmtpSection section = (SmtpSection)ConfigurationManager.GetSection("mailSettings/" + sectionName);
    
            _smtpClient = new SmtpClient();
    
            if (section != null)
            {
                if (section.Network != null)
                {
                    _smtpClient.Host = section.Network.Host;
                    _smtpClient.Port = section.Network.Port;
                    _smtpClient.UseDefaultCredentials = section.Network.DefaultCredentials;
    
                    _smtpClient.Credentials = new NetworkCredential(section.Network.UserName, section.Network.Password, section.Network.ClientDomain);
                    _smtpClient.EnableSsl = section.Network.EnableSsl;
    
                    if (section.Network.TargetName != null)
                        _smtpClient.TargetName = section.Network.TargetName;
                }
    
                _smtpClient.DeliveryMethod = section.DeliveryMethod;
                if (section.SpecifiedPickupDirectory != null && section.SpecifiedPickupDirectory.PickupDirectoryLocation != null)
                    _smtpClient.PickupDirectoryLocation = section.SpecifiedPickupDirectory.PickupDirectoryLocation;
            }
        }
    
        public void Send(MailMessage message)
        {
            _smtpClient.Send(message);
        }
    
    Run Code Online (Sandbox Code Playgroud)

    }

  3. 然后只需发送电子邮件

    new CustomSmtpClient("mailings").Send(new MailMessage())