Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Workflow SendEmail Custom Activity

, 29 May 2009
Rate this:
Please Sign up or sign in to vote.
The SendEmailActivity activity can be used to send e-mail messages from a workflow through Simple Mail Transfer Protocol (SMTP).

Introduction

Windows Workflow Foundation includes a default set of activities that provide functionality for control flow, conditions, event handling, state management, and communication with applications and services. However, there are times when an activity is needed to implement specific behavior. The Windows Workflow Foundation framework is extensible by allowing the creation custom activities and including them in a workflow. In this case, I have created a Custom Activity that can be used to send e-mail messages from a workflow through Simple Mail Transfer Protocol (SMTP).

Using the Code

The SendEmailActivity is a custom activity that enables you to send email from a WF. Before the e-mail message is sent, the code in the SendEmailActivity validates that the e-mail and SMTP properties are set correctly.

  1. Navigate to Start | All Programs | Microsoft Visual Studio 2005 | Microsoft Visual Studio 2005.
  2. Select the File | New | Project menu command.
  3. Visual Studio displays the New Project dialog box.
  4. Select the Visual C# | Workflow project type.
  5. Select Sequential workflow console application.
  6. Right click on the toolbox.
  7. Choose Items.
  8. Select browse to find the SendEmailActivity.dll.
  9. We should see the activity on the tool box.

toolbox1.JPG

At this point just drag and drop the SendEmailActivity on the WF designer. When the activity is dropped on the designer, the validation code for the properties is executed and you'll get a notify for all the properties you have to set up before you are able to compile and execute the code. We should write the code to validate the property setting task every time we are going to create our custom activity. To write the code to validate the properties, we have to override the "Validate" method of the Activity class:

  public override ValidationErrorCollection Validate(ValidationManager manager,
      object obj)
        {
            // Invoke the base class method implementation to
            // perform default validation.
            ValidationErrorCollection errors = base.Validate(manager, obj);

            // Make sure there is an activity instance.
            SendEmailActivity crw = obj as SendEmailActivity;
            if (crw == null)
            {
                throw new InvalidOperationException();
            }

            if (crw.Parent == null)
            {
                return errors;
            }
            #region Required Property

            if (string.IsNullOrEmpty(crw.SMTP) &&
                crw.GetBinding(SendEmailActivity.SMTPProperty) == null)
            {
                errors.Add(new ValidationError("SMTP is required", 100, false, "SMTP"));
            }

            if (string.IsNullOrEmpty(crw.From) &&
               crw.GetBinding(SendEmailActivity.FromProperty) == null)
            {
                errors.Add(new ValidationError("From is required", 100, false, "From"));
            }

            if (string.IsNullOrEmpty(crw.To) &&
            crw.GetBinding(SendEmailActivity.ToProperty) == null)
            {
                errors.Add(new ValidationError("To is required", 100, false, "To"));
            }

            if (string.IsNullOrEmpty(crw.Subject) &&
            crw.GetBinding(SendEmailActivity.SubjectProperty) == null)
            {
                errors.Add(new ValidationError("Subject is required", 100, false,
                    "Subject"));
            }

            if (string.IsNullOrEmpty(crw.Attachment) == false && System.IO.File.Exists(
                crw.Attachment) == false)
            {
                errors.Add(new ValidationError("Attachment file doesn't exist", 100,
                    false, "Attachment"));
            }

            #endregion

            return errors;
        }
    }

This method return a ValidationErrorCollection that contains all the "Error" we had added during the validation. We have to write the code to validate the values of the properties we want to validate. If the value doesn't satisfy the validation condition, we can add our custom error to the ValidationErrorCollection. When the user drags and drops the activity on the WF designer, that code is executed and all errors we have added will be shown on the designer:

validate.jpg

Unfortunately, the validation code is executed also when we build the project so we have to set up all the required properties before we'll be able to build the project.

Now we have to set up all the required properties (SMTP server, From, To, etc.). Open the Properties window and set up all the properties required by binding them to a new field as shown below:

secondWf_small.JPG

Then we have to write the code to set up the values for the fields we created in the step before. Drag and drop a code activity just before the SendEmailActivity. Double click on the activity to generate a handler for the code activity execute method.

thirdWF_small.JPG

Write the code on the code activity execute handler to set up all the properties we bind to the WF fields:

 private void SetUpSendEmailProperties_ExecuteCode(object sender, EventArgs e) {

            this.sendEmailActivity_Body = "This is a message from my WF";
            this.sendEmailActivity_From = "MyWf@hotmail.com";
            this.sendEmailActivity_To = "MyEmailAccount@hotmail.com";
            this.sendEmailActivity_Subject = "WorkFlowEmail";
        }

At this point, we can run the console application that hosts the workflow. Then we will receive an email. The activity allows the file attachment. To set up the file we want attached, we can bind the property "Attachment" to a WF field and add the code to get the file path. Otherwise we can select the file path straight from the property designer but in this way we'll always send the same attachment for all the emails.

Points of Interest

The attachment property has the designing time support to be set up. That could be used when we are developing our activity and we want to make this task quicker.To do that, we have to design the property designer as we want. In the case of the Attachment property, it is just a Windows Form with a textbox that shows the file path and one openFileDialog to select the file. I'll show how to do it on the next article (meantime just take a look at the source code. Smile | :)

History

  • 29th May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Massimiliano Peluso "WeDev Limited"
Architect PeluSoft Limited
United Kingdom United Kingdom
I have been fascinated by software development since I was 10 years old. I'm proud of learned the first programming language with an Olivetti PC 128S based on Microsoft BASIC. I'm a Software Architect/Senior Developer with a strong background of all the developing Microsoft's technologies.

Comments and Discussions

 
QuestionSendEmailActivity under .net 4.0 Pinmemberodsby8-Jun-12 10:31 
GeneralMy vote of 5 PinmemberRajkumarBathula13-Apr-11 19:09 
Questionhow to get logged in users email id for From in code PinmemberRajkumarBathula17-Mar-11 21:07 
AnswerRe: how to get logged in users email id for From in code PinmemberMassimiliano Peluso "PeluSoft Limited"25-Mar-11 5:06 
GeneralRe: how to get logged in users email id for From in code PinmemberRajkumarBathula25-Mar-11 7:51 
GeneralRe: how to get logged in users email id for From in code PinmemberMassimiliano Peluso "PeluSoft Limited"28-Mar-11 2:27 
GeneralMy vote of 5 Pinmembersteliodibello8-Dec-10 11:41 
Generaldll Pinmembersatishsashi22-Mar-10 8:31 
GeneralRe: dll PinmemberMassimiliano Peluso "PeluSoft Limited"24-May-10 6:10 
GeneralGreat! Pinmembertest stelio29-May-09 4:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 29 May 2009
Article Copyright 2009 by Massimiliano Peluso "WeDev Limited"
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid