allBlogsList

How to Send Custom Email Using Sitecore Dispatch Manager and Test it Locally

Every now and then you run into a client business requirement where an email needs to be sent after an event happens. Most of the time, the answer is through automation and having a preconfigured EXM campaign handle it.

However, there are time when this normally advised route is not the best option. This can be due to any number of reasons ranging from it’s a utility email to x, y, and z. Whatever the reason may be, this short and sweet article will you how you can utilize the same EXM engine to send your email without going through an EXM campaign.

Oh, and did I mention that you can test it locally? Read on.

How to Test Email Sending Locally using SMTP4Dev

SMTP4Dev is an SMTP emulation software that can be used to listen to and monitor known SMTP ports. The port number is configurable and can be changed at will to suit any needs. The software also provides a webpage where users can view emails sent via the SMTP server. This is very important since this is the only way we can view the actual message as sent to the recipients.

Link: https://github.com/rnwood/smtp4dev

Figure 1: Launching SMTP4Dev

Send Test Email using EXM

For us to verify that our fake SMTP works, let’s use EXM to send a quick test message in the hopes that SMTP4Dev receives it. Should this work, we will be able to move on to the next step which is to send custom email without going through EXM.

Figure 2: Email Experience Manager App

Figure 3: Adding Subject text for our test message

Figure 4: Sending a quick test of our email

Figure 5: SMTP4Dev Manager showing us the sent test message

Sending Email via the Dispatch Manager

The Dispatch Manager is your friend. It is the same manager that EXM uses to send its emails. We just skipped the middleman and went directly to THE guy. In order to use it, I found that I needed to add the dependency injection configuration below.

<register serviceType="Sitecore.EDS.Core.Dispatch.IDispatchManager, Sitecore.EDS.Core" implementationType="Sitecore.EDS.Core.Dispatch.DispatchManager, Sitecore.EDS.Core" />

Now that we have our fake SMTP server and dispatcher setup, it is now time to run code to send an email and use our server to “send” it out.

Figure 6: Code for Sending Custom Email

Code above shows a sample Email Manager that uses dependency injection to get an instance of the Dispatch Manager and contains SendEmail() methods to send…well…emails.

The Dispatch Manager takes in an argument of type EmailMessage and then sends it out using SendAsync() method. We also check its state prior to sending using .IsConfigured property.

Send Custom Email using Code Results

Figure 7: Sample Email Received by SMTP4Dev from Dispatch Manager

Summary

Using the Dispatch Manager is an easy way to send out custom email messages in Sitecore. This method enables you to send them out without using EXM campaigns which can significantly complicate things. SMTP4Dev is a nice tool to emulate an SMTP server and greatly complements our solution.

Having both on your arsenal will enable you to develop email sending solutions in the complete comfort of your local development environment.