Synchronization of E-mails to Activities in K2
Automatic downloading of e-mails to activities in K2 is a tool that sends the entire e-mail to K2. This function is available only for use with Office 365 and the MS Exchange mail server. It can be paired with any e-mail client (most often MS Outlook). Based on it, an activity with the content of the e-mail is created and the activity is linked to contact persons or partners according to the sender or recipients of the e-mail. The activity is created without user intervention based on the scheduled task set for E-mail synchronization. The tool can be used especially in customer support, where all communication with the customer is recorded, or as support for sales representatives and marketing when monitoring and evaluating opportunities, etc.
Prerequisites for Ensuring the Functionality of E-mail Synchronization to K2
The service runs outside K2, on a separate Linux server, and communicates with K2 via the K2 API.
- An application server must be installed so that the scheduled task can run.
- For the function of selecting Categories and folders in the settings of a particular e-mail account, the supplied DLL library, which is part of the K2 installation including the local installation, must be registered in the operating system of the system where K2 runs. If this library is not registered, K2 will try to register it itself. This action will require Windows local administrator rights. If the library cannot be registered, an error message is displayed. Even so, the configuration can be completed by manually filling in the category/folder name in the relevant field.
- Microsoft 365 or Google Workspace with business e-mail accounts. Personal accounts are not supported.
- K2 API v2, through which the service communicates with K2
- A Linux server on which the service runs in a Docker container
Who Installs the Service
The Linux server, the Docker container and the service itself are set up by a K2 system specialist. The specialist also selects or generates an encryption key for OAuth tokens, which is handed over to the customer. The customer then enters the key in the global configuration on the MS Graph API tab (see below).
RabbitMQ service for K2 version 27 and higher
RabbitMQ is a third-party service. Synchronization works without it, but if synchronization is operated on version 2027 or higher, it is advisable to have it installed. The service then receives configuration changes from K2 immediately and does not need to query K2 regularly.
How the service fits into the K2 architecture is described in K2 Architecture, scenario 6.
Note: Synchronization from an on-premise MS Exchange server continues to run via the original settings on the MS Exchange API tab (see the chapter MS Exchange API Tab) and there is no need to install the new service.
Settings in K2
Basically, two kinds of settings can be made according to the customer's needs. The user can choose which e-mails to save to K2, or an entire e-mail folder can be saved to K2. The settings in K2 are then made as needed.
In the tree menu Marketing/E-mail synchronization, there are two functions that need to be set for automatic import of e-mails:
- Global e-mail synchronization settings
- Configuration of accounts for synchronization
Global e-mail synchronization settings
The global configuration is set by the K2 administrator once for the entire company. The configuration is available in the tree menu Marketing / E-mail synchronization, Global e-mail synchronization settings function. The form has four tabs: Common, MS Exchange API, MS Graph API and Google.

Picture: Global email synchronization configuration - Common tab
Common Tab
Description of fields:
The type of activity of the received email message - The type of activity created when a received e-mail message is imported.
The type of activity of the sent email message - The type of activity created when a sent e-mail message is imported.
Folder for activities - The folder under which activities from imported e-mails are created.
Document type of imported attachments - The document type created when attachments from e-mails are imported. The document type must have an archive folder set and a Unique transfer selected, where the attachments will be saved.
Intervals (new e-mail service only)
Note: we recommend not changing the values entered below
Interval of configuration loading by the email-service [s] - How often the service asks K2 for changes in the configurations. It is not used with RabbitMQ; changes arrive immediately. The default value is 300 s (5 min)
Interval of e-mail synchronization by the email-service [s] - How often the service goes through all configurations and rules and starts the synchronization. The default value is 300 s (5 min)
Advance of access token renewal before expiration [s] - How many seconds before the access token expires the service renews it. The default value is 120 s (2 min)
MS Exchange API Tab
This tab applies only to the original synchronization via Exchange Web Services (EWS), which continues to be used by on-premise MS Exchange servers. The new e-mail service does not support synchronization of entire conversations, so the fields for conversations have remained only here.

Picture: Global email synchronization configuration - MS Exchange API tab
If the customer wants to synchronize e-mails, they must first find out the Client Id, Client Secret and Tenant Id values. These can be found in Azure Active Directory (https://aad.portal.azure.com/), where an administrator must log in. The administrator must register a new application there that will have permission to use the EWS API and use "app-only authentication". The entire procedure is described at: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth.
If the administrator wants to limit the permissions of the application, which in this way has access to all mailboxes, they can do so and allow access to only some of the mailboxes. More on this at: https://learn.microsoft.com/en-us/graph/auth-limit-mailbox-access.
After the Client Id, Client Secret and Tenant Id values are entered, individual users can set up their accounts for access to their mailboxes. In the Configuration of accounts for e-mail synchronization, they set their e-mail address and the URL for connecting to MS Exchange (for Office 365 it is https://outlook.office365.com/EWS/Exchange.asmx).
The Use OAUTH login check box sets login using OAUTH.
The values for the OAuth Tenant Id, OAuth Client Id and OAuth Client Secret fields are provided by the administrator by logging in to Azure Active Directory.
The The type of conversation activity field is set to the Type of activity that will be created when importing e-mail communication, i.e. several messages with the same e-mail subject. If I synchronize one e-mail message, all previous and subsequent e-mails will also be synchronized. For the activities representing the e-mails, one parent activity is created that will cover the conversation and will have the specified type.
MS Graph API Tab
This tab is used to set the connection of the new service to Microsoft 365 via Microsoft Graph. The values for the Microsoft group can be obtained in Microsoft Entra, see the chapter Application Registration in Microsoft Entra.
Microsoft 365: only work accounts of your organization
Only work accounts of your organization in Microsoft 365 can log in. Personal Microsoft accounts, such as @outlook.com, @hotmail.com or @live.com, do not work. That is why the application in Microsoft Entra is registered only for your organization (chapter Application Registration in Microsoft Entra, step 3).

Picture: Global email synchronization configuration - MS Graph API tab
Common for OAuth2
Use OAUTH login - The new service always logs in via OAuth.
Key for encrypting OAuth tokens - Must be identical to the key set in the e-mail service. It will be handed over to you by the K2 system specialist who installed the service.
Redirect port for login from the desktop - If the port is not blocked on the computers from which users log in, there is no reason to change the value. The same port is entered in the redirect address in Microsoft Entra (chapter Application Registration in Microsoft Entra, step 3) and in Google Cloud (chapter XY, step 7). The default value is 51576.
Microsoft
TenantId of your own company in Microsoft Entra - The ID of your organization in Microsoft Entra. How to find the value is described in the next chapter Application Registration in Microsoft Entra.
ClientId of your own application registration in Microsoft Entra - The ID of the application that you register for K2 using the procedure described in the next chapter Application Registration in Microsoft Entra.
ClientSecret assigned to the application registration in Microsoft Entra - The secret key. See the chapter Application Registration in Microsoft Entra.
Google Tab
Only work accounts of your organization in Google Workspace can log in. Personal Google accounts (@gmail.com) do not work. That is why the OAuth consent screen must be set as Internal (see the next chapter).
This is where the connection of the new service to Google Workspace is set. The Common for OAuth2 group is the same as on the MS Graph API tab. The values for the Google group can be obtained in Google Cloud as described in the next chapter Client Registration in Google Cloud.

Picture: Global email synchronization configuration, Google tab
ClientId of the client from Google Cloud - The ID of the OAuth client that you create for K2 in Google Cloud. You copy it from the Client ID and Client secret part in the chapter Client Registration in Google Cloud.
ClientSecret of the client from Google Cloud - The secret key of this client. You copy it from the Client ID and Client secret part in the chapter Client Registration in Google Cloud.
The values for the Microsoft group are obtained by the Microsoft 365 administrator in the Microsoft Entra admin center. It is enough to perform the procedure once for the entire company.
You will find the Tenant ID on the Home page. Copy it to the TenantId of your own company in Microsoft Entra field.

Picture: Tenant ID on the Microsoft Entra home page
Create a new application registration. In the Entra ID menu, open App registrations and click New registration.

Picture: App registration, New registration button
Fill in the registration
Name - a name by which you will recognize the application, e.g. K2 E-mail synchronization.
Supported account types: Single tenant only - i.e. only accounts of your organization.
Redirect URI - select Web as the platform, even though it is localhost, and enter http://localhost:51576/. This address is used for login from the desktop K2. The port must match the Redirect Port for login from the desktop field. Confirm with the Register button.

Picture: Application registration with the redirect address for localhost
Copy the ClientId
On the Overview page of the new application, copy the Application (client) ID to the ClientId of your own application registration in Microsoft Entra field.

Picture: Application (client) ID on the Overview page
Add permissions for Microsoft Graph
Open API permissions and click Add a permission. Select Microsoft Graph, then Delegated permissions, check the following permissions and confirm with the Add permissions button:
Mail.ReadWrite: reading and editing the user's mail,
offline_access: the service can renew access without the user logging in again.
The registration has the User.Read permission from the beginning; keep it.
Finally, click Grant admin consent for <your organization>. Users then do not have to approve the application themselves at the first login. If your organization does not allow users to approve applications, this step is necessary.

Picture: API permissions with granted permissions and the Grant admin consent button
Create a secret key
Open Certificates & secrets and on the Client secrets tab click New client secret. Enter a description, select the validity and confirm with the Add button.

Picture: Certificates & secrets, New client secret button
Copy the secret key value
Copy the value from the Value column, not the Secret ID, to the ClientSecret assigned to the application registration in Microsoft Entra field. Entra shows the value only immediately after creation; you will not see it later.
Key validity
The secret key has limited validity (the Expires column). Before it ends, create a new key and enter it in K2. After expiration, the service will not connect to Microsoft 365 and e-mails will stop being synchronized. K2 will notify administrators about this. As soon as you enter the new key in K2, synchronization resumes by itself. It is not necessary to restart the service.

Picture: Copy the Value, not the Secret ID (the key value is hidden in the picture)
Add redirect addresses for web K2
Open Authentication and via Add Redirect URI add an address for each web K2. The platform type is Web; the address has the form https://<web-K2-address>/Redirect.
Important:
You must add redirect addresses for all web K2 instances you operate if users are to log in from them.

Picture: Redirect addresses for localhost and two web K2 instances
The values for the Google group are obtained by the administrator in the Google Cloud Console. It is enough to perform the procedure once for the entire company. Work in the project that you use or create for K2 (My project in the pictures). The project must belong to your company's organization in Google Workspace, otherwise it cannot be set as Internal in step 4.
Permissions
Unlike in Microsoft Entra, mail permissions are not set in Google Cloud. Each user grants them themselves when they log in to their Google account in K2 for the first time.
Open the list of enabled APIs
In the menu, open APIs and services and select Enabled APIs and services.
Configure email accounts for email synchronization
In the Configuration of e-mail accounts, we set the e-mail account that the function will handle. Several e-mails can be set. The user enters the access data for their e-mail (name and password) and specifies the rules according to which synchronization will take place. It is possible to synchronize:
- By folder - all e-mails from the specified folder will be imported to K2.
- By category – only e-mails marked with the given category (colour) will be imported to K2.
New account
You add a new account with the New item button and by selecting the account type:
MS Graph API account configuration (newer) for Microsoft 365,
Gmail account configuration for Google Workspace,
MS Exchange API account configuration (older) only for on-premise MS Exchange servers.

Picture: Configuration of e-mail accounts for e-mail synchronization – new account
Basic Data and Login

Picture: MS Graph API account configuration - basic data
Email address - The work e-mail address that the tool will handle.
Contact person - The contact person is used for linking with K2, where it will be filled in as the Responsible person when an activity is created.
Optional settings
Import only e-mails from saved partners/persons - Only e-mails whose addressee (sender) is saved in contact persons will be imported.
Save attachments - Option for saving e-mail attachments.
Save messages in EML format - The entire e-mail is saved as an EML attachment.
Category for messages that were synchronized to K2 - The e-mail category used to mark e-mail messages that have already been synchronized to K2.
Select from the list only with RabbitMQ (button outlined in green in the picture)
The button offers categories directly from the mailbox. It works only when operating with RabbitMQ (see the previous chapter). Without RabbitMQ, the name is typed manually, exactly as it is in the mailbox. The same applies to folders and categories in the synchronization rules and to labels in Gmail.
The Sign in in browser button opens the Microsoft login page; for a Gmail account, it opens the Google login page. The user logs in securely directly there with their work account. The password is entered only to Microsoft or Google; K2 neither sees nor stores it.
For Google Workspace, the user grants K2 permission to the mail at the first login. The Valid login field shows whether the login is valid.
Gmail Account
The Gmail account configuration is the same as for Microsoft 365. Only labels are used instead of categories: a label for synchronized messages and a synchronization rule by label.
The settings of e-mails and access data are made on the Basic data tab.

Picture: Configuration of e-mail accounts for e-mail synchronization – Basic data
Description of fields:
E-mail address |
The e-mail address that the tool will handle. |
Contact person |
The contact person is used for linking with K2, where it will be filled in as the Responsible person when an activity is created. |
User name |
User name for access to Exchange. |
Password |
Password for access to Exchange. |
URL to connect to the Exchange service |
The URL address of the e-mail server from which mail is to be synchronized, ending with the suffix "/ews/exchange.asmx". If the URL address is entered incorrectly, it should not be possible to connect to the server and the scheduled task will report an error. |
Categories for messages that have been synchronized to K2 |
The e-mail category used to mark e-mail messages that have already been synchronized to K2. |
Import emails from saved partners/persons only |
Only e-mails whose addressee (sender) is saved in contact persons will be imported. |
Save attachments |
Option for saving e-mail attachments. |
Save messages in EML format |
The entire e-mail is saved as an EML attachment. |
Settings for Office 365:
If the user has multi-factor authentication turned on, they must add a new "App password" method in the settings ( https://account.activedirectory.windowsazure.com/AppPasswords.aspx), where a password for the application that bypasses multi-factor authentication is generated. (The procedure is described at https://allthingscloud.info/2019/03/21/iphone-calendar-synchronization-with-office-365/). If the user does not have multi-factor authentication turned on, this step is not necessary.
Then it is only necessary to fill in the configuration of accounts for synchronization correctly:
- Both the e-mail address and the user name have the value of the e-mail with which the user logs in to Office365
- The value generated above is inserted into the password, or the password with which the user normally logs in if they do not have multi-factor authentication turned on.
- The URL for connecting to the Exchange service has the value https://outlook.office.com/EWS/Exchange.asmx
On the Synchronization rules tab, the user sets whether the entire folder will be synchronized or whether the user will select only some e-mails. A new rule is added with the New item button and by selecting Folder synchronization or Synchronization by category.

Picture: MS Graph API account configuration - synchronization rules
In synchronization by folder, an e-mail folder is selected that we want to import to K2 in its entirety.

Picture: Folder synchronization
Synchronized folder |
The e-mail folder from which e-mails for synchronization will be selected. Without RabbitMQ, the name must be entered manually; for a subfolder, the entire path, for example Inbox/Customers. |
Synchronize subfolders |
Option for synchronization including subfolders in the e-mail. |
Synchronize whole conversations |
Option for synchronizing the entire conversation. |
Date of the last synchronization |
The date and time up to which e-mails from the folder have been synchronized. During the next synchronization, e-mails from this date onwards are imported. When creating the rule, you can enter a date and the synchronization will then start from it. If you leave the field empty, all e-mails in the folder will be imported during the first synchronization, starting from the oldest. The service processes them gradually, at most 50 e-mails from one rule in each synchronization round. After the rule is saved, the date can no longer be changed; it is then advanced by the service. |
After selecting synchronization by category, a form is displayed where the Synchronized folder is entered. Clicking the dots displays a list of folders from the e-mail. Only e-mails marked with the selected category are synchronized, from all folders of the mailbox.

Picture: Synchronization by category
Synchronized folder |
The user selects the e-mail folder from which e-mails for synchronization will be selected. After the user name and password are set, the folders are offered directly from the specified e-mail. |
Name of synchronized category |
The user enters the e-mail category that they want to import to K2. Without RabbitMQ, the name must be entered manually. |
The rule has no date of the last synchronization, because a category can also be assigned to an old e-mail. After successful synchronization, the service removes the category from the e-mail, so the same e-mail is not imported twice. If the import fails, the category remains and the e-mail is tried again in the next round. In Gmail, the same applies to labels.
E-mails for synchronization to K2 are selected by assigning a Category (distinguished by colours) to the given e-mail. The user can create their own categories in the e-mail. With the right mouse button, they select the Categorize option and then right-click All categories.

Picture: Categorize in e-mail
Pressing the "New" button creates a new category in the e-mail, which is named and assigned a colour. The user can also use existing categories and, for example, just rename them.

Picture: Colour categories in e-mail
For demonstration, I created the Categories To K2 and Synchronized with K2. I assigned the category To K2 to the e-mails that I selected for synchronization.

Picture: Synchronization rules

Picture: Name of synchronized category menu - Categories of e-mail messages
In Gmail, labels are used instead of categories. You create a new label with the + button next to the Labels heading in the left panel. You then assign it to an e-mail with the label icon above the open message.

Picture: Creating a new label in Gmail
Scheduled Task Settings
For the function to start working, it is necessary to set the scheduled task Synchronize e-mails to K2, which is classified under Other. The scheduled task then sets the time interval in which synchronizations will take place, e.g. every 10 minutes or only at night.

Picture: Scheduled task for Synchronize e-mails to K2
Created Activity
After e-mails are imported via the scheduled task, a new activity is created in K2.

Picture: Imported activity
Activity fields:
Type of activity |
The type set in the Global settings function is inserted. |
Name |
The e-mail subject is written. |
Sup. folder |
The folder specified in the Global settings function. |
Date From |
Date of the received/sent e-mail. |
Date To |
Date of the received/sent e-mail (same as the From date). |
Report |
Contents of the e-mail. |
Contact person |
Sender/recipient of the e-mail, if their e-mail is entered in K2 in contact persons. If the given e-mail is not found in contact persons, the field remains empty. |
Partner |
Partner from the contact person. |
Responsible person |
Owner of the e-mail account. |
If the e-mail has an attachment and Save attachments is set in the Account configuration, the attachment from the e-mail is imported to the Documents tab of the given activity.