The Windows Workflow Foundation project that I’m working on relies on my ability to create a library of custom activities that can be used together to handle many different customer scenarios. Creating a simple custom activity is a very easy and straightforward task with WF4. This post will walk you through the steps of creating a custom send-email activity.
The first thing you’ll need to do is create a new class that inherits from NativeActivity. (Note that NativeActivity is not your only option–check out this post for an overview of the different choices.) NativeActivity is an abstract class that requires us to implement an Execute method. This is the method that runs when the activity executes.
public sealed class EmailActivity : NativeActivity
{
protected override void Execute(NativeActivityContext context)
{
throw new NotImplementedException();
}
}
We’ll need some additional information in order to send an email, and we can use input arguments to get what we need. To add an input argument, create a public property of type InArgument<T>. Here are my input properties. Notice that I’ve decorated some of them with the [RequiredArgument] attribute to indicate that they are required. Output arguments can be added in a similar fashion by using OutArgument<T>, though we do not need any for this example.
[RequiredArgument]
public InArgument<string> Host { get; set; }
[RequiredArgument]
public InArgument<string> Sender { get; set; }
[RequiredArgument]
public InArgument<string> Recipients { get; set; }
public InArgument<string> Subject { get; set; }
public InArgument<string> Body { get; set; }
Now that we have all the information we need, we just need to implement the Execute method to send an email. Once again, it’s very simple.
protected override void Execute(NativeActivityContext context)
{
// create the email message
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(Sender.Get(context));
mailMessage.To.Add(new MailAddress(Recipients.Get(context)));
mailMessage.Subject = Subject.Get(context);
mailMessage.Body = Body.Get(context);
// send the message
var smtpClient = new SmtpClient(Host.Get(context));
smtpClient.Send(mailMessage);
}
This activity can now be added to your workflow XAML through the designer or any text editor. In the example below, I have bound the Body property to a variable named StatusText that will be set in an earlier activity.
<my:EmailActivity
Host="mail.mydomain.com"
Sender="myworkflow@mydomain.com"
Recipients="someuser@mydomain.com"
Subject="Workflow Status Update"
Body="[StatusText]" />
Here is the complete class:
public sealed class EmailActivity : NativeActivity
{
[RequiredArgument]
public InArgument<string> Host { get; set; }
[RequiredArgument]
public InArgument<string> Sender { get; set; }
[RequiredArgument]
public InArgument<string> Recipients { get; set; }
public InArgument<string> Subject { get; set; }
public InArgument<string> Body { get; set; }
protected override void Execute(NativeActivityContext context)
{
// create the email message
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(Sender.Get(context));
mailMessage.To.Add(new MailAddress(Recipients.Get(context)));
mailMessage.Subject = Subject.Get(context);
mailMessage.Body = Body.Get(context);
// send the message
var smtpClient = new SmtpClient(Host.Get(context));
smtpClient.Send(mailMessage);
}
}
Like this:
Like Loading...