Setting multiple SMTP settings in web.config?

2019-01-08 09:37发布

I am building an app that needs to dynamically/programatically know of and use different SMTP settings when sending email.

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().

However, I need more of a connectionStrings-like approach, where I can pull a set of settings based on a key/name.

Any recommendations? I'm open to skipping the tradintional SmtpClient/mailSettings approach, and I think will have to...

8条回答
叛逆
2楼-- · 2019-01-08 10:00

I ended up building my own custom configuration loader that gets used in an EmailService class. Configuration data can be stored in web.config much like connection strings, and pulled by name dynamically.

查看更多
\"骚年 ilove
3楼-- · 2019-01-08 10:06

I had the same need and the marked answer worked for me.

I made these changes in the

web.config:

      <configSections>
        <sectionGroup name="mailSettings2">
          <section name="noreply" type="System.Net.Configuration.SmtpSection"/>
        </sectionGroup>
        <section name="othersection" type="SomeType" />
      </configSections>

      <mailSettings2>
        <noreply deliveryMethod="Network" from="noreply@host.com"> // noreply, in my case - use your mail in the condition bellow
          <network enableSsl="false" password="<YourPass>" host="<YourHost>" port="25" userName="<YourUser>" defaultCredentials="false" />
        </noreply>
      </mailSettings2>
      ... </configSections>

Then, I have a thread that send the mail:

SomePage.cs

private bool SendMail(String From, String To, String Subject, String Html)
    {
        try
        {
            System.Net.Mail.SmtpClient SMTPSender = null;

            if (From.Split('@')[0] == "noreply")
            {
                System.Net.Configuration.SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("mailSettings2/noreply");
                SMTPSender = new System.Net.Mail.SmtpClient(smtpSection.Network.Host, smtpSection.Network.Port);
                SMTPSender.Credentials = new System.Net.NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
                System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
                Message.From = new System.Net.Mail.MailAddress(From);

                Message.To.Add(To);
                Message.Subject = Subject;
                Message.Bcc.Add(Recipient);
                Message.IsBodyHtml = true;
                Message.Body = Html;
                Message.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");
                Message.SubjectEncoding = Encoding.GetEncoding("ISO-8859-1");
                SMTPSender.Send(Message);

            }
            else
            {
                SMTPSender = new System.Net.Mail.SmtpClient();
                System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
                Message.From = new System.Net.Mail.MailAddress(From);

                SMTPSender.EnableSsl = SMTPSender.Port == <Port1> || SMTPSender.Port == <Port2>;

                Message.To.Add(To);
                Message.Subject = Subject;
                Message.Bcc.Add(Recipient);
                Message.IsBodyHtml = true;
                Message.Body = Html;
                Message.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");
                Message.SubjectEncoding = Encoding.GetEncoding("ISO-8859-1");
                SMTPSender.Send(Message);
            }
        }
        catch (Exception Ex)
        {
            Logger.Error(Ex.Message, Ex.GetBaseException());
            return false;
        }
        return true;
    }

Thank you =)

查看更多
登录 后发表回答