Developing an Email Activity Control in WF


Windows Workflow Foundation and Activities

Windows Workflow Foundation (WF) provides a visual interface for creating and hosting workflows that integrate seamlessly into line-of-business applications built on the .NET 3.0 Framework. Activity binding, one feature of Windows Workflow Foundation, allows an activity author to expose the properties of his or her activity to other activities at a workflow level. Once the properties are available at the workflow level, they can interact with the components that serve as the solution user interface. This allows developers to set activity properties dynamically in a way with which they are familiar in a Visual Studio environment. This article constructs an activity and shows how to use that activity within a workflow. For this example, it is assumed that you are already familiar with the [System.Workflow.ComponentModel.Activity] class.

The first step is to create a simple activity. In Visual Studio 2005, start a new solution with a Workflow Activity Library project. Visual Studio will provide a base activity (Activity1). Rename the activity to something suitable such as .System.Workflow.ComponentModel.Activity.

Configuring Dependency Properties

public static DependencyProperty From Property = DependencyProperty.Register("From", typeof(string),typeofSendMailActivity));
System.Workflow.ComponentModel.Compiler.ValidationOptionAttributeSystem.
Workflow.ComponentModel.Compiler. ValidationOption.Required)]


public
string From
{
     get
{
     return ((string)(this.GetValue(SendMailActivity.FromProperty)));
}
     set
{
     this.SetValue(SendMailActivity.FromProperty, value);
}
}

Defining an Activity Function

Now that the dependency properties have been defined, it is time to define the activity function, which is accomplished by overriding the Execute method of the activity. Simply add the following code.

protected
override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
     MailAddress toAddress = new MailAddress(To);
     MailAddress fromAddress = new MailAddress(From);
     MailAddressCollection addresses = new MailAddressCollection();
     addresses.Add(toAddress);
     MailMessage msg = new MailMessage(fromAddress, toAddress);
     string body = System.String.Empty;
     msg.Body = body;//You can Customize Email Body whatever you required
     //First Parameter SMTP Server URL and Second One Default Port
     SmtpClient mail = new SmtpClient(MailServerUrl, 25);
     mail.Send(msg);
}

Using the Activity Component

You can find your new Activity Component in your ToolBox Item. Simply drag and drop in your workflow state where you need the ability to send a notification email.

Figure 1: Representing State of Machine Workflow.



Figure 2: Dynamic Dependency Property.


Similar Articles