0

Im trying to send multiple email on item approved in sitecore workflows, but i think the workflows doesnt wait on the async method to complete, it's run through the send mail function and out of workflows even if i await. I have used the same method on publish end event, and it's worked perfectly. How can i make the workflows wait on the async method ?

Here is my workflows event code(the SendMailToUser method go through a couple process mail method and eventually to the sendmailasync method :

public async Task  Process(WorkflowPipelineArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        Item item = args.DataItem;
        if (item != null)
        {
           await SendMailToUser(item);
        }
        else
        {
            Log.Info(String.Format("Item is null or not found"), this);
        }
    }

And here is my send mail function :

public async Task SendMail(MailModel mail, SMTPConfig config)
    {

       ...
        await smtpClient.SendMailAsync(message);
        Log.Info("Mail sent",this);
    }  
public async Task SendMailAsync(MailModel mail, SMTPConfig config, List<string> listRecipent)
    {
        try
        {
            for (int i = 0; i < listRecipent.Count; i++)
            {
                mail.to = null;
                mail.to = new MailAddress(listRecipent[i]);
                await SendMail(mail, config);
            }
        }
        catch (ProtocolException ex)
        {
            Sitecore.Foundation.SitecoreExtensions.Extensions.StringExtensions.LogError(ex.ToString(), "SMTPService.SendMail");
         
        }
    }
1

Instead of creating your own sendmailasync maybe you can try using the built in sendmailasync function of sitecore?

The function can be found inside the Sitecore.MainUtil class. Below is the code snippet from the Sitecore.Kernel.

Just make sure you setup your SMTP server by editing /App_Config/Include/smtp.config or use your own patch to update the settings.

    // Sitecore.MainUtil
using System;
using System.ComponentModel;
using System.Net.Mail;
using Sitecore.Diagnostics;

/// <summary>
/// Sends the mail asynchronously.
/// </summary>
/// <param name="message">The message.</param>
public static void SendMailAsync(MailMessage message)
{
    try
    {
        Assert.ArgumentNotNull(message, "message");
        SmtpClient client = CreateSmtpClient();
        client.SendCompleted += delegate(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                Log.Info("SMTP client email sending canceled.", typeof(MainUtil));
            }
            if (e.Error != null)
            {
                Log.Error("SMTP client email sending failed.", e.Error, typeof(MainUtil));
            }
            client.Dispose();
            message.Dispose();
        };
        client.SendAsync(message, new object());
    }
    catch (Exception exception)
    {
        Log.Error("SMTP client email sending error.", exception, typeof(MainUtil));
    }
}

Here is the sample config.

<!--  MAIL SERVER
SMTP server used for sending mails by the Sitecore server
Is used by MainUtil.SendMail()
Default value: ""
-->
<setting name="MailServer" value="" />
<!--  MAIL SERVER USER
If the SMTP server requires login, enter the user name in this setting
-->
<setting name="MailServerUserName" value="" />
<!--  MAIL SERVER PASSWORD
If the SMTP server requires login, enter the password in this setting
-->
<setting name="MailServerPassword" value="" />
<!--  MAIL SERVER PORT
If the SMTP server requires a custom port number, enter the value in this setting.
The default value is: 25
-->
<setting name="MailServerPort" value="25" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.