Sending Custom Mail from K2

K2's default notifications are an easy way to send email to users participating in an activity. Unfortunately, most clients want more information included in the message as well as the freedom to modify the message text without having to redeploy the process.

Fortunately for us, it's easy to create and reference a utility class to send custom email messages. The first step is to create a new class library in Visual Studio and add a reference to SourceCode.KO from the GAC and the necessary using statements.


using System;
using System.Collections.Generic;
using System.Text;
using SourceCode.KO;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Xml;
using System.Configuration;


The next step is to define the function that will send mail. How you retrieve your custom mail templates is up to you. You could pull the templates from external files or a database. You have complete flexibility to do as you please. Note in the sample code below that we have access to the server context to pull values from the string table. We could just as easily access process data fields to populate our message body.



namespace K2Utility
{
public class EmailHelper
{
public static void SendMessage(ServerEventContext K2,
string sender, string recipients)
{
MailMessage email = new MailMessage(sender, recipients);
email.Body = "insert your custom email body here";

SmtpClient smtp = new SmtpClient(K2.StringTable["Mail Server"]);
smtp.Send(mail);
}
}
}



Once the class has been created and compiled, you can add a reference to it in the
workflow process by clicking on the reference button shown in the following image.



Calling the method from a server event within an activity presents a challenge because the server context name is based on a GUID. The GetServerContext() takes care of this for us, returning an object that can be passed to the external function. The activity containing this server event code has been configured using the advanced destination settings to be Plan Per Destination - All at once. This gives us access to the ActivityInstanceDestination from which we can get the user's email.

Note: You will need to overload your mail class method to send mail from an escalation because the ActivityEscalationActionContext class does not share a common base class with the ServerContext object.



using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using SourceCode.KO;
using SourceCode.Workflow.Common.Extenders;
using hostContext = Project_8fce5761e56f476687d44dc840c2c0d1.EventItemContext_d63aca644e924977acfd0355872ec5d8;
namespace ExtenderProject_8fce5761e56f476687d44dc840c2c0d1
{
public partial class EventItem_d63aca644e924977acfd0355872ec5d8 : ICodeExtender
{
public void Main(Project_8fce5761e56f476687d44dc840c2c0d1.EventItemContext_d63aca644e924977acfd0355872ec5d8 K2)
{
string sender = K2.StringTable["Mail From"];

string recipients = K2.ActivityInstanceDestination.User.Email;

K2Utility.EmailHelper.SendMessage(K2.GetServerContext(), sender, recipients);
}
}
}



This post has covered making references to the K2 server context, referencing
external DLLs in K2, and accessing the destination user for an activity when
calling the DLL. These techniques can also be used for integration with other
external systems.

0 comments: