What is the best way to handle multiple mail servers in the mailSettings section of the web.config file?

I have an application that sends 500K + transactional emails per month. Some of them are more important than others. I need important emails in order to come out using a high-delivery email solution with tracking (more expensive) and less important emails using a regular mail server.

Is there a way to configure several smtp sections in mailSettings, pointing to two mail servers, and let the code select which mail server they want to send.

There is a way to do this using the "location" and process the pages by sending an email, select the SMTP server based on the path. However, I have a separate background process that does this asynchronously, and that won't help.

Thank!

+3
source share
1 answer

Instead of using email settings, perhaps consider using the appsettings settings to store connection strings for the server.

<appSettings>
   <add key="SmtpServer.Fast" value="fast.smtp.mycompany.com" />
   <add key="SmtpServer.Slow" value="slow.smtp.mycompany.com" />
</appSettings>

Then just use new SmtpClient(server)instead new SmtpClient(). Then you can configure the code this way:

SmtpClient client = null;

if (IsHighPriorityMessage(msg))
  client = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer.Fast"]);
else
  client = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer.Slow"]);

If you need to configure authentication just use client.Credentials

+1
source

Source: https://habr.com/ru/post/1760763/


All Articles