Skip to main content
Version: 8.1

Customize sending emails

Level: advanced
Important

Creatio version 7.16 and up supports sending emails using the explicit account credentials

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

Sending an email is done by using a business process. To set up a business process, use the Auto-generated page and Script task elements.

Sending an email from an existing account algorithm

To send an email from an existing account:

  1. Create a business process. The process must include Auto-generated page and Script task required elements.
  2. Create a config file for the email.
  3. Add an attachment (optional).
  4. 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:

Creating a configuration file of the 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.

Adding attachments to email
// 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 email
// Sending the configured email message. To ignore access permissions when sending the message. 
// set the ignoreRight parameter to "true".
emailSender.Send(message, ignoreRights);

Sending an email using the explicit account credentials algorithm

  1. Create a business process. The process must include Auto-generated page and Script task required elements.
  2. Create an instance of the EmailClientFactory class.
  3. Create an instance of the EmailSender class.
  4. Create a config file for connecting to the mailbox.
  5. Create a config file of the email.
  6. Add an attachment (if applicable).
  7. Run the business process to send the email.

Creating an instance of the EmailClientFactory class

To create an instance of the EmailClientFactory class, make sure you have UserConnection established.

Creating an instance of the EmailClientFactoryclass
var emailClientFactory = ClassFactory.Get<IEmailClientFactory>(
new ConstructorArgument("userConnection", UserConnection));

Creating an instance of the EmailSender class

To create an instance of the EmailSender class, pass the created EmailClientFactory instance and the UserConnection to the constructor.

Creating an instance of the EmailSender class
var emailSender = ClassFactory.Get<IEmailSender>(
new ConstructorArgument("emailClientFactory", emailClientFactory),
new ConstructorArgument("userConnection", UserConnection));

Creating a config file for connecting to the mailbox

To create a config file for connecting to the mailbox, use the EmailContract.DTO.Credentials class. Populate the following parameters:

Creating a config file for connecting to the mailbox
var credentialConfig = new EmailContract.DTO.Credentials {
// Outbound mailserver IP address or domain
ServiceUrl = "mail service host",
// Can be left empty for some protocols
Port = "Port",
// Use SSL to encrypt the connection.
UseSsl = false,
// Mailbox user name
UserName = "EmailUserName",
// Mailbox user password
Password = "UserPassword",
// Mail server type (Exchange or IMAP/SMTP)
ServerTypeId = EmailDomain.IntegrationConsts.ExchangeMailServerTypeId ||
EmailDomain.IntegrationConsts.ImapMailServerTypeId,
// Sender mailbox
SenderEmailAddress = "sender@test.com"
};
Important

The ServiceUrl, UserName, Password, ServerTypeId, SenderMailbox parameters are required.

Creating a config file of the email

To create a configuration file of the email that you are sending, use the EmailContract.DTO.Email class. Populate the parameters shown below to ensure the validity of your email:

Creating a config file of the email
var message = new EmailContract.DTO.Email {
// Sender email address.
Sender = "Sender@email.com",
// Recipient email addresses.
Recepients = List<string>{ "first@recepient.co", "second@recepient.co"},
// The subject of the email.
Subject = "Message subject",
// Email body.
Body = "Body",
// Priority, EmailContract.EmailImportance enumeration values.
Importance = EmailContract.EmailImportance.High
};

Adding attachments (if applicable)

You can add attachments to your email. To do this, populate the Attachments field. Attachments are essentially a list of EmailContract.DTO.Attachment instances.

Adding attachments
// Creating an attachment.
var attachment = new EmailContract.DTO.Attachment {
Name = "FileName",
Id = "844F0837-EAA0-4F40-B965-71F5DB9EAE6E"
};
// Setting data for the attachment
attachment.SetData(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 passed arguments of the email and the connection configuration file.

Sending the email
// Sending a configured email message with connection parameters
// for the mailbox of the sender To ignore access permissions when sending the message.
// set the ignoreRight parameter to "true".
emailSender.Send(message, credentialConfig, ignoreRights);