Override the SMTP settings in the Sitecore Web.config file

I recently needed to set up some sort of testing mode on a production server for email notifications that are sent from Sitecore. The email notifications are sent via a scheduled task in Sitecore which alerts users that their account will expire in the near future.

While in testing mode I was looking for a way to make sure all emails were sent to localhost on the production server instead of actually being sent to users through the configured SMTP server. I would then use a tool such as smtp4dev to catch all emails sent to localhost. Admittedly this is a fairly easy task but I was worried that Sitecore would not allow me to override the SMTP settings configured in the Web.config file.

The first step was to investigate how Sitecore sends emails via the Sitecore.MainUtil.SendMail method. Opening up Sitecore.MainUtil.SendMail through reflection gave me all the information I needed.

public static void SendMail(System.Net.Mail.MailMessage message)
{
  string mailServer = Settings.MailServer;
  SmtpClient smtpClient;
  if (string.IsNullOrEmpty(mailServer))
  {
    smtpClient = new SmtpClient();
  }
  else
  {
    int mailServerPort = Settings.MailServerPort;
    smtpClient = mailServerPort <= 0 ? new SmtpClient(mailServer) : new SmtpClient(mailServer, mailServerPort);
  }
  string mailServerUserName = Settings.MailServerUserName;
  if (mailServerUserName.Length > 0)
  {
    string mailServerPassword = Settings.MailServerPassword;
    NetworkCredential networkCredential = new NetworkCredential(mailServerUserName, mailServerPassword);
    smtpClient.Credentials = (ICredentialsByHost) networkCredential;
  }
  smtpClient.Send(message);
}

As you would expect Sitecore leverages the System.Net.Mail.SmtpClient.Send method in .NET and grabs the SMTP client settings from the Web.config file.

This means you are free to use the standard System.Net.Mail.SmtpClient.Send method and pass in SMTP client settings as needed. Here is a simple example that I used to reroute my notification emails to localhost. This code is triggered instead of the standard Sitecore MainUtil.SendMail method when Sitecore administrators turn on testing mode.

private void SendEmailToLocalhost(MailMessage mailmessage)
{
    try
    {
        SmtpClient client = new SmtpClient("localhost")
        {
            Credentials = new NetworkCredential("", ""),
            Port = 25
        };
        client.Send(mailmessage);
    }
    catch (Exception ex)
    {
        Sitecore.Diagnostics.Log.Error("An error was caught while sending an email to localhost for testing", ex, this);
    }
}

If you are sending to a different SMTP client then don’t forget that you made need to pass in the EnableSsl = true setting when initializing the new SmtpClient object.

Advertisement
Posted in Sitecore

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s