How email functionality is implemented in K2

This article describes how email functions are implemented in K2. Sending of emails in K2 surface in multiple areas, such as in SmartForms Rules, Workflows Steps and SmartActions. You can refer to  the diagrams and step explanations in this article to better understand how email is handled in K2.

SmartForms: Send an E-mail action

This section describes how the Send an Email Action rule action in SmartForms works.  

Figure 1: SmartForms: Send an E-mail action

  1. The Send Email action executes on the SmartForm. 
  2. A web service call is made to IIS.
  3. The web service connects over TCP using Port 5555 to the SmartForms Runtime Hosted Service.
  4. The SmartForms Runtime Hosted Service executes the relevant method in the MessageBus Service.
  5. Depending on whether you are using Microsoft Exchange (EWS) or SMTP, the relevant K2 Library (K2 IMAP or K2 EWS) executes.
    • If EWS, the K2 EWS Library makes a web service call to the Mail Server to send the email.
    • If SMTP, the K2 IMAP Library makes a connection over TCP to Port 25 to the Mail Server to send the email.

Workflow: Send Email Step

This section describes how the Send Email step in a workflow works. 

Figure 2: Workflow: Send Email Step

  1. A workflow instance is started on a workflow that contains a Send Email step.
  2. When an Send Email event is detected, the Workflow Event Execution executes the relevant method in the Workflow Shapes Library. (The workflow Shapes Library is the code associated with various types of workflow steps). 
  3. The connection strings are checked to determine whether EWS or SMTP is used in the executing K2 environment.
    • If EWS, the K2 EWS Library makes a web service call to the Mail Server to send the email.
    • If SMTP, the K2 IMAP Library makes a connection over TCP to Port 25 to the Mail Server to send the email.

Workflow: Task Notification

This section describes how the Task Notification email works, when you use this option in the User Task step.

Figure 3: Workflow: Task Notification

  1. A workflow instance starts.
  2. When a Task event with task notifications enabled is reached, the Workflow Event Execution executes the relevant method in the Workflow Shapes Library.
  3. The Workflow Shapes library executes the relevant method to the EventBusClient API, and the data is inserted from the task notification into the [EventBus][CustomEventInstance] and [EventBus][EventPolicyMappingInstance] tables in the K2 database. The data includes the body of the mail, subject and so forth.
  4. Once the data has been added to the K2 database, the Workflow Event Execution goes into a waiting state, where it waits for the user to action the task.
  5. The EventBus Hosted Service continuously checks the [EventBus][CustomEventInstance] and [EventBus][EventPolicyMappingInstance] tables in the K2 database every thirty seconds for new mail entries. 
  6. When new mail entries are identified and retrieved, the EventBus Hosted Service executes the relevant method in the MessageBus Service.
  7. The connection strings are checked to determine whether EWS or SMTP is configured.
  8. Depending on whether you are using EWS or SMTP, the relevant K2 Library (K2 IMAP or K2 EWS) executes.
    • If EWS, the K2 EWS Library makes a web service call to the Mail Server to send the email.
    • If SMTP, the K2 IMAP Library makes a connection over TCP Port 25 to the Mail Server to send the email.

Workflow: SmartActions Replies

This section describes how K2 handles emailed replies when Task Notifications are configured for SmartActions

Figure 4: Workflow: SmartActions Replies

  1. The task recipient receives the task notification email, and replies to the email with one of the available SmartAction options.
  2. The email is sent to the SmartActions Inbox, where the EWS Mail Server checks for new e-mails.
  3. The EWS SmartActions receiver executes a web service call every 30 seconds to the Mail server, as instructed by the MessageBus service, to retrieve the relevant emails.
  4. The MessageBus retrieves the message from the SmartActions Inbox and passes it onto the SmartActions Library.
  5. The SmartActions Library retrieves the serial number and [Action] keyword from the email content, and sends it to the Workflow Server Action Task library. Assuming the task, serial number and email origin matches the task notification configuration, the workflow continues with the specified task action.
  6. The Workflow Server Action task returns the result of the action to the SmartActions Library, and the message is relayed to the MessageBus Service.
  7. Depending on the settings within your connection strings (SMTP or EWS), the relevant method is invoked to the respective library.
    • If EWS, the K2 EWS Library makes a web service call to the Mail Server, and sends the email to the recipients inbox.
    • If SMTP, the K2 IMAP Library makes a connection over TCP to Port 25 to the Mail Server and sends the email to the task recipients inbox.