Skip to content
July 9, 2020 / kiranpatils

Sending an email using SendGrid with Sitecore

Challenge:

We’ve been using SendGrid for sending emails for most of our Azure PaaS clients. In case you are not aware of SendGrid. The following information will help you understand more. Source:

SendGrid is a cloud-based SMTP provider that allows you to send email without having to maintain email servers. SendGrid manages all of the technical details, from scaling the infrastructure to ISP outreach and reputation monitoring to whitelist services and real-time analytics.

Recently we migrated one client from IaaS to PaaS and faced an interesting challenge where Sitecore’s OOTB Workflow was trying to send an email and it was failing due to SMTP configurations.

We wanted to get this fixed without overriding Sitecore code and have minimal impact on this change. You are also looking forward to doing the same? Then this post might help you!

Solution:

As you do, We also did a quick google search and came across this blog: https://sitecore.stackexchange.com/questions/8809/sitecore-and-sendgrid

We tried those configurations. But no luck 😦

As per this document: https://sendgrid.com/docs/for-developers/sending-email/v2-csharp-code-example/ there are a couple of options to use SendGrid:

  1. Using SendGrid’s C# Library
  2. Using .NET’s Built-in SMTP Library

We wanted to avoid the options #1 route as it would cause overriding a few Sitecore’s OOTB methods.

We also created a Sitecore patch file that overrides default MailServer configuration and uses SendGrid specific details. (You need to provide exact values for following variables)

<?xml version="1.0"?>
<configuration xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
	<sitecore>
		<settings>
			<setting name="MailServer" set:value="#{SendGrid.SMTP.server}" />
			<setting name="MailServerUserName" set:value="#{SendGrid.SMTP.username}" />
			<setting name="MailServerPassword" set:value="#{SendGrid.SMTP.password}"/>
			<setting name="MailServerPort" set:value="#{SendGrid.SMTP.port}" />
		</settings>
	</sitecore>
</configuration>

Just a note: If you would like to enable SSL. Then you can configure that using following setting. <setting name=”MailServerUseSsl” value=”true”/> Which I believe is new in Sitecore 9.3 (Default value is false)

But that didn’t fix our issue. After digging more we figured out that the above configurations are only applied when an email is being sent using Sitecore.MainUtil.SendMail() method.

Post refactor we figured out that method which was causing issue has been using System.Net.Mail.SmtpClient.Send(); method. Which doesn’t respect the above changes and relies mainly on the Web. Config configuration values. Based on the above learning, we made the following changes and that fixed our issue (You need to provide exact values for following variables):

<system.net xdt:Transform="InsertIfMissing">
    <mailSettings xdt:Transform="InsertIfMissing">
      <smtp xdt:Transform="InsertIfMissing">
        <network host="__SendGrid.SMTP.server__" password="__SendGrid.SMTP.password__" userName="__SendGrid.SMTP.username__" port="__SendGrid.SMTP.port__" />
      </smtp>
    </mailSettings>
  </system.net>

Long story short, If you want to use SendGrid then you have to make changes at couple of places:

  1. Patch MailServer setting values
  2. Update Web.Config for System.Net Node

I’ve also created gist for your ease :

Let me know if this helped you or you learned something new while implementing this which others should be aware of.

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 )

Google photo

You are commenting using your Google 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

%d bloggers like this: