Sending emails from existing account
Glossary Item Box
Introduction
Creatio version 7.16 and up supports sending emails from an existing email account.
In Creatio, you can send emails using both customer and developer means. By developer means, you can send emails:
- Using an existing account
- Using the explicit account credentials
This article covers sending emails from existing accounts. Read more about sending emails using your account credentials in the “Sending emails using the explicit account credentials" article.
Sending an email from an existing account is done by using a business process. To set up a business process, use the [Auto-generated page] and [Script task] elements.
Preparing and sending an email
To send an email from an existing account:
- Create a business process. The process must include [Auto-generated page] and [Script task] required elements.
- Create a config file for the email.
- Add an attachment (optional).
- Run the business process to send the email.
Creating a config file for the email
To create a configuration file of the email, use the Terrasoft.Mail.Sender.EmailMessage class. Populate the parameters below to ensure the validity of your email:
var message = new Terrasoft.Mail.Sender.EmailMessage { // Sender email address. From = "Sender@email.com", // Recipient email addresses. To = List<string>{ "first@recepient.co", "second@recepient.co"}, // Copy (optional) Cc = List<string>{ "first@recepient.co", "second@recepient.co"}, // Hidden copy (optional) Bcc = List<string>{ "first@recepient.co", "second@recepient.co"}, // The subject of the email. Subject = "Message subject", // Email body. Body = "Body", // Priority, Terrasoft.Mail.Sender.EmailPriority enumeration values. Priority = Terrasoft.Mail.Sender.EmailPriority.Normal };
Adding attachments (optional)
You can add attachments to your email. To do this, populate the [Attachments] field. Attachments are essentially a list of Terrasoft.Mail.Sender.EmailAttachments instances.
// Creating an attachment. var attachment = new Terrasoft.Mail.Sender.EmailAttachment { // Attachment ID. Id = new Guid("844F0837-EAA0-4F40-B965-71F5DB9EAE6E"), // Attachment name. Name = "attachName.txt", // Data. Data = byteData }; // Adding the attachment to the message. message.Attachments.Add(attachment);
Sending the email
To send the email, use the Send method of the EmailSender class with the email parameters and the connection configuration file.
// Sending the configured email message. To ignore access permissions when sending the message.
// set the ignoreRight parameter to "true".
emailSender.Send(message, ignoreRights);
Case description
Create a business process that will open a page with email parameters fields to send an email using an existing account.
Source code
You can download the package with a sample case implementation using the following link.
Case implementation algorithm
1. Create a business process
In the [Configuration] section execute the [Add] –> [Business process] action (Fig. 1).
Fig. 1. [Add] –> [Business process] action
In the opened Process Designer, set the following values for the properties in the setup area (Fig. 2):
- [Title] — "Sending emails from existing account".
- [Code] — "UsrSendEmailFromExistingUserProcess".
Fig. 2. The properties of the business process
2. Add the [Auto-generated page] element
The [Auto-generated page] element enables the process to open an arbitrary page created by the user. For this element, add “Filling parameters” as a caption and set the following properties (Fig. 3):
- [Page title] — "Fill parameters for sending Email".
- [To whom should the page be shown?] – select [Formula] and specify [#System variable.Current user contact#].
Fig. 3. Auto-generated page properties
3. Add a button to the page
To add a [Continue] button in the [Buttons] block, click and specify the following parameters (Fig. 4):
- [Caption] — "Continue".
- [Code] – “ContinueButton”.
- [Style] — select "Green".
- Set the [Active] checkbox.
- Set the [Performs value validation] checkbox.
Fig. 4. Adding a button to the auto-generated page
Click [Save].
4. Add elements to the page.
To add an element that will contain the email sender’s mailbox, click in the [Page Items] block, select the [Selection field] type and specify the following parameters (Fig. 5):
- [Title] – "Sender Mailbox".
- [Code] – “SenderMailbox”.
- [Data source] — select "Mailbox synchronization settings".
- [View] — select "Drop down list".
Fig. 5. Adding an element to the auto-generated page
Click [Save].
To add an element that will contain the email recipient’s mailbox, click in the [Page Items] block, select the [Text field] type and specify the following parameters (Fig. 6):
- [Title] — "Recipient (many recipients separated by semicolon ";")".
- [Code] – “Recipient”.
- Set the [Required] checkbox.
Fig. 6. Adding an element to the auto-generated page
Click [Save].
To add an element that will contain the subject of the email, click in the [Page Items] block, select the [Text field] type and specify the following parameters (Fig. 7):
- [Title] – “Subject”.
- [Code] – “Subject”.
- Set the [Required] checkbox.
Fig. 7. Adding an element to the auto-generated page
Click [Save].
To add an element that will contain the body of the email, click in the [Page Items] block, select the [Text field] type and specify the following parameters (Fig. 8):
- [Title] – “Body”.
- [Code] – “Body”.
- Set the [Required] checkbox.
Fig. 8. Adding an element to the auto-generated page
Click [Save].
5. Add a [ScriptTask] element
Set the value of the [Title] property of the [Script task] element to “Send Email” The element must execute the following program code:
// Id выбранного почтового ящика. var mailBoxSettingId = Get<Guid>("SenderMailbox"); // Создание экземпляра EmailClientFactory. var emailClientFactory = ClassFactory.Get<IEmailClientFactory>( new ConstructorArgument("userConnection", UserConnection)); // Создание экземпляра IEmailSender. var emailSender = ClassFactory.Get<IEmailSender>( new ConstructorArgument("emailClientFactory", emailClientFactory), new ConstructorArgument("userConnection", UserConnection)); var entity = UserConnection.EntitySchemaManager.GetInstanceByName("MailboxSyncSettings").CreateEntity(UserConnection); if (entity.FetchFromDB("Id", mailBoxSettingId, new List<string> { "SenderEmailAddress" })) { // Получение почтового адреса отправителя из выбранного почтового ящика. var senderEmailAddress = entity.GetTypedColumnValue<string>("SenderEmailAddress"); // Заполнение параметров отправляемого сообщения. var message = new Terrasoft.Mail.Sender.EmailMessage { // Email-адрес отправителя. From = senderEmailAddress, // Email-адреса получателей. To = Get<string>("Recipient").Split(';').ToList<string>(), // Копия (не обязательно). // Cc = List<string>{ "first@recepient.co", "second@recepient.co"}, // Скрытая копия (не обязательно). // Bcc = List<string>{ "first@recepient.co", "second@recepient.co"}, // Тема письма. Subject = Get<string>("Subject"), // Тело письма. Body = Get<string>("Body"), // Приоритет, значения из перечисления Terrasoft.Mail.Sender.EmailPriority. Priority = Terrasoft.Mail.Sender.EmailPriority.Normal }; // Дополнительно можно прикреплять вложения (в примере используются тестовые значения). // Создание вложения. var attachment = new Terrasoft.Mail.Sender.EmailAttachment { // Идентификатор вложения. Id = Guid.NewGuid(), // Название файла. Name = "test.txt", // Данные. Data = Encoding.ASCII.GetBytes("some test text") }; // Добавление вложения в письмо. message.Attachments.Add(attachment); // Отправка письма. emailSender.Send(message); } return true;
6 Add parameters
To add a business process parameter that will contain the email recipient’s mailbox, execute the [Add parameters] —> [Text] action in the [Parameters] tab of the setup area and specify the following parameter properties (Fig. 9):
- [Title] – “Recipient”.
- [Code] – “Recipient”.
- [Value] – click –> [Process parameter] and select the “Recipient (many recipients separated by semicolon ";")” process element.
Fig. 9. Adding a process parameter
Click [Save].
To add a business process parameter that will contain the subject of the email, execute the [Add parameters] —> [Text] action in the [Parameters] tab of the setup area and specify the following parameter properties (Fig. 10):
- [Title] – “Subject”.
- [Code] – “Subject”.
- [Value] – click –> [Process parameter] and select the “Subject” process element.
Fig. 10. Adding a process parameter
Click [Save].
To add a business process parameter that will contain the body of the email, execute the [Add parameters] —> [Text] action in the [Parameters] tab of the setup area and specify the following parameter properties (Fig. 11):
- [Title] – “Body”.
- [Code] – “Body”.
- [Value] – click –> [Process parameter] and select the “Body” process element.
Fig. 11. Adding a process parameter
Click [Save].
To add a business process parameter that will contain the email sender’s mailbox, execute the [Add parameters] —> [Other] —> [Unique identifier] action in the [Parameters] tab of the setup area and specify the following parameter properties (Fig. 12):
- [Title] – "Sender Mailbox".
- [Code] – “SenderMailbox”.
- [Value] – click –> [Process parameter] and select the “Sender Mailbox” process element.
Fig. 12. Adding a process parameter
Click [Save].
7. Add methods
To add business process methods, click in the [Usings] block in the [Methods] tab of the process designer setup area, and specify Terrasoft.Configuration value in the [Name Space] field. Click [Save].
Using the same method, add the following namespaces:
- Terrasoft.Mail.Sender
- Terrasoft.Core.Factories
- Terrasoft.Core
- Terrasoft.Mail
- IntegrationApi
- System.Linq
Save all changes in the Process Designer.
8. Run the business process
To run the business process successfully, make sure that a user account for the email sender is available in the Creatio application. Learn more about adding a user account in the “Integration with the MS Exchange service” block of articles.
After the business process is run using the [Run] button, a page containing fields for specifying email parameters will open.
Fig. 13. The page for preparing an email
To send an email from the corresponding email account, click [Continue].