Skip to main content
Version: 8.1

Send email from an existing account

Level: advanced
Example

Create a business process that will open a page with email parameters fields to send an email using an existing account.

Example implementation algorithm

1. Create a business process

In the Configuration section execute the AddBusiness process action.

In the opened Process Designer, set the following values for the properties in the setup area:

  • Title – "Sending emails from existing account".
  • Code – "UsrSendEmailFromExistingUserProcess".

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:

  • Page title – "Fill parameters for sending Email".
  • To whom should the page be shown? – select "Formula" and specify #System variable.Current user contact#.

3. Add a button to the page

To add a Continue button in the Buttons block, click and specify the following parameters:

  • Caption – "Continue".
  • Code – “ContinueButton.”
  • Style – select "Green".
  • Set the Active checkbox.
  • Set the Performs value validation checkbox.

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:

  • Title – "Sender Mailbox".
  • Code – “SenderMailbox.”
  • Data source – select "Mailbox synchronization settings".
  • View – select "Drop down list".

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:

  • Title – "Recipient (many recipients separated by semicolon ";")".
  • Code – “Recipient.”
  • Set the Required checkbox.

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:

  • Title – “Subject.”
  • Code – “Subject.”
  • Set the Required checkbox.

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:

  • Title – “Body.”
  • Code – “Body.”
  • Set the Required) checkbox.

Click Save.

5. Add a Script task element

Set the value of the Title property of the Script task element to “Send Email” The element must execute the following program code:

Script Task code
var mailBoxSettingId = Get<Guid>("SenderMailbox");
var emailClientFactory = ClassFactory.Get<IEmailClientFactory>(
new ConstructorArgument("userConnection", UserConnection));
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 {
From = senderEmailAddress,
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"),
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 parametersText action in the Parameters tab of the setup area and specify the following parameter properties:

  • Title – “Recipient.”
  • Code – “Recipient.”
  • Value – click Process parameter and select the “Recipient (many recipients separated by semicolon ";")” process element.

Click Save.

To add a business process parameter that will contain the subject of the email, execute the Add parametersText action in the Parameters tab of the setup area and specify the following parameter properties:

  • Title – “Subject.”
  • Code – “Subject.”
  • Value – click Process parameter and select the “Subject” process element.

Click Save.

To add a business process parameter that will contain the body of the email, execute the Add parametersText action in the Parameters tab of the setup area and specify the following parameter properties:

  • Title – “Body.”
  • Code – “Body.”
  • Value – click Process parameter and select the “Body” process element.

Click Save.

To add a business process parameter that will contain the email sender’s mailbox, execute the Add parametersOtherUnique identifier action in the Parameters tab of the setup area and specify the following parameter properties:

  • Title – "Sender Mailbox".
  • Code – “SenderMailbox.”
  • Value – click Process parameter and select the “Sender Mailbox” process element.

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

Important

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 MS Exchange / Microsoft 365 email and calendar block of articles (user documentation).

After the business process is run using the Run button, a page containing fields for specifying email parameters will open.

To send an email from the corresponding email account, click Continue.


Resources

Package with example implementation