Sending emails using the explicit account credentials
Glossary Item Box
Introduction
In Creatio, you can send emails using both, customer and developer means. With the developer means, you can send emails:
- Using an existing account
- Using the explicit account credentials
This article covers sending emails using the explicit account credentials. Read more about sending emails using an existing account in the “Sending emails from existing accounts" article.
Preparing and sending an email
To send an email using the explicit account credentials:
- Create an instance of the EmailClientFactory class.
- Create an instance of the EmailSender class.
- Create a config file of connection to the mailbox.
- Create a config file of the email.
- Add an attachment (if applicable).
- Perform sending.
Creating an instance of the EmailClientFactory class.
To create an instance of the EmailClientFactory class, make sure you have UserConnection established.
var emailClientFactory = ClassFactory.Get<IEmailClientFactory>( new ConstructorArgument("userConnection", UserConnection));
Creating an instance of the EmailSender class.
To crate an instance of the EmailSender class, pass the created EmailClientFactory instance and the UserConnection to the constructor.
var emailSender = ClassFactory.Get<IEmailSender>( new ConstructorArgument("emailClientFactory", emailClientFactory), new ConstructorArgument("userConnection", UserConnection));
Creating a config file of connection to the mailbox
To create a config file of connection to the mailbox, use the EmailContract.DTO.Credentials class. Popualte the following parameters:
var credentialConfig = new EmailContract.DTO.Credentials { // Name or IP address of the outgoing mail server. ServiceUrl = "mail service host", // Might be empty for some protocols. Port = "Port", // Use the SSL protocol to encrypt the connection. UseSsl = false, // The mailbox user name. UserName = "EmailUserName", // The mailbox user password. Password = "UserPassword", // The mailbox type (Exchange or Imap/Smtp). ServerTypeId = EmailDomain.IntegrationConsts.ExchangeMailServerTypeId || EmailDomain.IntegrationConsts.ImapMailServerTypeId, // The Sender's mailbox. SenderEmailAddress = "sender@test.com" };
ATTENTION
The ServiceUrl, UserName, Password, ServerTypeId, SenderEmailAddress values 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 below parameters to ensure the validity of your email:
var message = new EmailContract.DTO.Email { // The Sender's email address. Sender = "Sender@email.com", // The Recipients' email addresses. Recepients = List<string>{ "first@recepient.co", "second@recepient.co"}, // Email subject. Subject = "Message subject", // Email body. Body = "Body", // Priority, the EmailContract.EmailImportance enumeration values. Importance = EmailContract.EmailImportance.High };
Adding attachments (if applicable)
Additionally, you can add attachments to your email. To do this, populate the [Attachments] field. Attachments are basically a list of EmailContract.DTO.Attachment instances.
// Creating an attachment. var attachment = new EmailContract.DTO.Attachment { Name = "FileName", Id = "844F0837-EAA0-4F40-B965-71F5DB9EAE6E" }; // Setting data for the attachmen. attachment.SetData(byteData); // Adding attachment to the email. message.Attachments.Add(attachment);
Sending the email
To send the email, use the Send method of the EmailSender class with the arguments of the email and the connection configuration file.
//Sending the created email with parameters of connection to the Sendre's
// mailbox. To ignore the access permissions when sending,
// set the ignoreRights parameter value to "true".
emailSender.Send(message, credentialConfig, ignoreRights);