Something wrong with EmailAction or I do something wrong

Recently I noticed that Sitecore.Workflows.Simple.EmailAction does not do what I expect from it. Below I’ll describe what I mean.
I created simple workflow which contains two states – Inital and Final. Initial state contains ‘Go to final’ command which contains ‘Send Email’ action based on ‘/sitecore/templates/System/Workflow/Email action’ template.

Article workflow

Also I created template which uses workflow mentioned above.
For catching email message which have to be sent after execute of ‘Go to final’ command I use smpt4dev. And what was surprise for me – there was no messege caught by smpt4dev.
I took a look at EmailAction class. Here it is:

namespace Sitecore.Workflows.Simple
{
	public class EmailAction
	{
		public void Process(WorkflowPipelineArgs args)
		{
			Assert.ArgumentNotNull(args, "args");
			ProcessorItem processorItem = args.ProcessorItem;
			if (processorItem == null)
			{
				return;
			}
			Item innerItem = processorItem.InnerItem;
			string fullPath = innerItem.Paths.FullPath;
			string text = this.GetText(innerItem, "from", args);
			string text2 = this.GetText(innerItem, "to", args);
			string text3 = this.GetText(innerItem, "mail server", args);
			string text4 = this.GetText(innerItem, "subject", args);
			string text5 = this.GetText(innerItem, "message", args);
			Error.Assert(text2.Length > 0, "The 'To' field is not specified in the mail action item: " + fullPath);
			Error.Assert(text.Length > 0, "The 'From' field is not specified in the mail action item: " + fullPath);
			Error.Assert(text4.Length > 0, "The 'Subject' field is not specified in the mail action item: " + fullPath);
			Error.Assert(text3.Length > 0, "The 'Mail server' field is not specified in the mail action item: " + fullPath);
			System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(text, text2);
			mailMessage.Subject = text4;
			mailMessage.Body = text5;
			System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(text3);
			smtpClient.Send(mailMessage);
		}
		...

After I checked code of SendEmail method within Sitecore.MainUtil class. This method works in proper way:

public static void SendMail(System.Net.Mail.MailMessage message)
{
	using (System.Net.Mail.SmtpClient smtpClient = MainUtil.CreateSmtpClient())
	{
		smtpClient.Send(message);
	}
}

As you can see the difference is that the call of Send method on instance of smtpClient is made inside of using block. I decided to create copy of Sitecore.Workflows.Simple.EmailAction class with the amendment to call Send method inside using block:

namespace TestEmail
{
    using Sitecore.Configuration;
    using Sitecore.Data.Items;
    using Sitecore.Diagnostics;
    using Sitecore.Workflows.Simple;
    using System.Net.Mail;

    public class EmailAction
    {
        public void Process(WorkflowPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            ProcessorItem processorItem = args.ProcessorItem;
            if (processorItem == null)
            {
                return;
            }
            Item innerItem = processorItem.InnerItem;
            string fullPath = innerItem.Paths.FullPath;
            string text = this.GetText(innerItem, "from", args);
            string text2 = this.GetText(innerItem, "to", args);
            string text3 = this.GetText(innerItem, "mail server", args);
            string text4 = this.GetText(innerItem, "subject", args);
            string text5 = this.GetText(innerItem, "message", args);
            Error.Assert(text2.Length > 0, "The 'To' field is not specified in the mail action item: " + fullPath);
            Error.Assert(text.Length > 0, "The 'From' field is not specified in the mail action item: " + fullPath);
            Error.Assert(text4.Length > 0, "The 'Subject' field is not specified in the mail action item: " + fullPath);
            Error.Assert(text3.Length > 0, "The 'Mail server' field is not specified in the mail action item: " + fullPath);
            MailMessage mailMessage = new MailMessage(text, text2);
            mailMessage.Subject = text4;
            mailMessage.Body = text5;
            using (System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(text3))
            {
                smtpClient.Send(mailMessage);
            }
        }
        ...

And wrote ‘TestEmail.EmailAction, TestEmail’ in Type field of ‘Send Email’ item (first pic) instead of ‘Sitecore.Workflows.Simple.EmailAction, Sitecore.Kernel’.
And lo and behold, now email message was sent and caught by smtp4dev.
It looks stranger ’cause it’s out of the box functionality. Or am I doing something wrong?

Note: Sitecore.NET 8.2 (rev. 170407)

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 )

Google+ photo

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

Connecting to %s