Click here to Skip to main content
12,949,816 members (76,422 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 22 Aug 2013

Create SendEmail Workflow 4 Custom Activity

, 22 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
In this tip, we are going to create an activity that can send an e-mail message to a target user.


Activity is the essence of workflow; even the workflow itself is an Activity. WF4 provides some build-in activities that can be used directly in the workflow designer panel. But many times, we need to create our own activities—for example, an activity that can send e-mail to inform someone about finishing a task or any other important thing.


To define our own activity, we should write a class that implements the root abstract Activity or one of its predefined subclasses.

Using the Code

Before moving ahead, please create two projects. The first is the ActivityLibrary project named ActivityLibrary. The other is the Workflow Console Application named WorkflowConsoleApp.

Delete Workflow1.xaml, which is created by default. The ActivityLibrary project is for all customized activities, whereas the WorkflowConsoleApp project is used for testing our customized activities. The following screenshot shows the project structure:

  • Step 1: Create the SendEmail activity

    Add a new code file to the ActivityLibrary project named SendEmail.cs. Then, fill the file with the following code:

    using System.Activities;
    public sealed class SendEmailActivity : CodeActivity
        public InArgument<string> from { get; set; }
        public InArgument<string> host { get; set; }
        public InArgument<string> userName { get; set; }
        public InArgument<string> password { get; set; }
        public InArgument<string> to { get; set; }
        public InArgument<string> subject { get; set; }
        public InArgument<string> body { get; set; }
        public OutArgument<string> result { get; set; }
        protected override void Execute(CodeActivityContext context)
            var mailMessage = new System.Net.Mail.MailMessage();
            mailMessage.Subject = subject.Get(context).ToString();
            mailMessage.Body = body.Get(context);
            mailMessage.From =
            new System.Net.Mail.MailAddress(from.Get(context));
            var smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = host.Get(context);
            smtp.Credentials =
            new System.Net.NetworkCredential(
            userName.Get(context), password.Get(context));
            smtp.EnableSsl = true;
            result.Set(context, "Sent Email Successfully!");

    Then build the activity project so that SendEmail appears in the toolbox panel of the workflow designer.

  • Step 2: Add a reference to ActivityLibrary

    In the WorkflowConsoleApp project, add an assembly reference to ActivityLibrary for us to be able to use these customized activities in the WorkflowConsoleApp project.

  • Step 3: Create a workflow to test the SendEmail activity

    Add a new workflow in the WorkflowConsoleApp project and name it TestSendEmailWF.xaml. Create the workflow as shown in the following screenshot:

    Save and build the solution.

  • Step 4: Run it.

    Alter the Main method of the Program.cs </code />file to this:

    static void Main(string[] args)
               WorkflowInvoker.Invoke(new TestSendEmailWF());

Set WorkflowConsoleApp as Startup project. Next, press Ctrl+F5 to run the workflow without debugging.

Points of Interest

Sending an e-mail usually costs some time. In real workflow applications, we should create an
asynchronous activity or an independent WF service to send e-mail.


  • 21st August, 2013: Initial post


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


About the Author

Software Developer For-Softwares
Tunisia Tunisia
Computer and technology enthusiast, interested in .Net developpement. Has a Engineer's degree on Software Engineering specialized on ERP.

You may also be interested in...

Comments and Discussions

QuestionSending Email Not Working For Some Mail Id's!, This Comment May be Funnny,But Its True.. Pin
ummidikondababu18-Dec-14 22:22
memberummidikondababu18-Dec-14 22:22 
Questionhow to use this activity in CRM? Pin
Member 952042814-Jan-14 22:50
memberMember 952042814-Jan-14 22:50 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170524.1 | Last Updated 22 Aug 2013
Article Copyright 2013 by Sami CHNITER
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid