Use AgilePoint's SendMail API to Send Email Notifications

Applies To

Objective

Provides an example of how to use AgilePoint's SendMail API to send email notifications.

Summary

AgilePoint's SendMail API provides application developers a function to develop custom notification scenarios for applications. For example, it can be used to develop custom email notifications at runtime and/or associate email notifications dynamically to any workflow tasks and/or objects within an application.

There are two SendMail() functions under different classes:

An alternative approach for sending an email with attachments in an AgilePoint Web Application is to use the .NET class, System.Net.Mail.SMTPClient. Please note that for emails that are sent out using System.Net.Mail.SMTPClient, AgilePoint Server has no records of these emails. If in case you run into email issues for this scenario, the troubleshooting effort should be focused on the custom code in your Web application.

The SMTP Server address is required when using the System.Net.Mail.SMTPClient. The screen captures below provide a sample for how to obtain the SMTP Server address.

SMTP and Database is configured via the AgilePoint Server Configuration.

EXAMPLE

Snippet 1: Sample Snippet

The following is a C# code snippet of how the SendMail API is called:

using System;
using System.Collections;
using System.Data;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
using Ascentn.Workflow.Base;

namespace ManagedCode
{
 public class CSharpCodeSnippet
      {           
// Invoke method is this class's entry point
           public void Invoke(
                  WFProcessInstance pi,
                  WFAutomaticWorkItem w,
                  IWFAPI api,
                  NameValue[] parameters)
            {
                  string emailAddress = WFSystem.SysAdmEMailAddress;
               string attachment = WFSystem.HomeDirectory +
                    "\\License.en-US.RTF";
               IWFActivityInstance ai = api.GetActivityInst(w.ActivityInstID);
               api.SendMail(
                        pi,
                        ai,
                        emailAddress,
                       null,
                       string.Format("Request for Approval - {0}", pi.ProcInstName),
                       "You are assigned with this task, please 
                        review and approve within two days.",
                        attachment);
            }
      }
}

Snippet 2: In AgilePart

   //<summary>
/// This class is AgilePart runtime class that will be invoked by AgilePoint Server.
   ///</summary>
    [AgilePart("Put name here")]
   public class MyAgilePart : WFAgilePart
    {
        [
       Description("Put description here"),
       AgilePartDescriptor(typeof(MyAgilePartDescriptor))
        ]
       public void SendMailByAPI(
                           WFProcessInstance pi,
                           WFAutomaticWorkItem w,
                            IWFAPI api,
                           NameValue[] parameters)
        {
           try
            {
               string emailAddress = WFSystem.SysAdmEMailAddress;
               string attachment = WFSystem.HomeDirectory +
               "\\License.en-US.RTF";

               Logger.WriteLine("Send e-mail to '{0}' with attachment '{1}'", emailAddress, attachment);
               IWFActivityInstance ai = api.GetActivityInst(w.ActivityInstID);

                api.SendMail(
                    pi,
                    ai,
                    emailAddress,
                   null,
                   string.Format("How To Send E-Mail Using ServerAPI - AgilePart, {0}", pi.ProcInstName),
                   "How To Send E-Mail Using ServerAPI",
                    attachment);

               if (w.Synchronous) MarkSuccess(api, pi, w, parameters);
            }
           catch (Exception ex)
            {
                HandleException(api, pi, w, parameters, ex);
            }
        }

}

Snippet 3: In AgileWork

   ///<summary>
   /// This class is AgileWork runtime class that will be invoked by AgilePoint Server.   
///</summary>
    [AgileWork("MyAgileWork AgileWork", typeof(MyAgileWorkDescriptor))]
   public class MyAgileWork : WFAgileWork
    {
        #region Constructor

       public MyAgileWork(WFProcessInstance instance, 
          WFManualActivityInstance activity)
            : base(instance, activity)
        {
           base.AssignWorkItem += new EventHandler(MyAgileWork_AssignWorkItem);
        }

        #endregion

        #region Event Handlers
       
private void MyAgileWork_AssignWorkItem(Object sender, System.EventArgs e)
        {           
WFGenerateManualWorkItemEventArgs args = e as WFGenerateManualWorkItemEventArgs;
        IWFAPI api = args.WFApi;
           string emailAddress = WFSystem.SysAdmEMailAddress;
           string attachment = WFSystem.HomeDirectory 
           "\\License.en-US.RTF";

           Logger.WriteLine("Send e-mail to '{0}' with attachment '{1}'", emailAddress, attachment);

            api.SendMail(
               this.m_ProcessInstance,
               this.m_ActivityInstance,
                emailAddress,
               null,
               string.Format("How To Send E-Mail Using ServerAPI - AgileWork, {0}",
               this.m_ProcessInstance.ProcInstName),
               "How To Send E-Mail Using ServerAPI",

           Logger.WriteLine("MyAgileWork_AssignWorkItem, Activity Instance={0}",
           base.m_ActivityInstance.DisplayName);
        }

        #endregion

    }

Snippet 4: In AgileStub

using System;
using Ascentn.Workflow.Base;
     ///<summary>
     /// Summary description for Class1.
     ///</summary>
public class ProcessDefinition_85B694EF1C014A0FB759FAD859339534 :
 WFProcessInstance
{
     public ProcessDefinition_85B694EF1C014A0FB759FAD859339534()
      {
      }

     public void SendMailByAPI(WFAutomaticWorkItem w, IWFAPI api)
      {
           string emailAddress =
WFSystem.SysAdmEMailAddress;
       string attachment = WFSystem.HomeDirectory +
       "\\License.en-US.RTF";
           Logger.WriteLine("Send e-mail to '{0}' with attachment '{1}'", 
           emailAddress, attachment );

       IWFActivityInstance ai = api.GetActivityInst(w.ActivityInstID);

            api.SendMail(
                 this,
                  ai,
                  emailAddress,
                 null,
                 string.Format("How To Send E-Mail Using ServerAPI, {0}",
                 this.ProcInstName),
                 "How To Send E-Mail Using ServerAPI",
                  attachment);
      }
}