Tip: Using SendEmailFromTemplateRequest to directly send an email using the Email Template

By | June 9, 2017

Earlier when we had an Email Template created for any entity, we used to follow the below four steps to send an email using Template:

  1. Retrieve the Email Template and get the Template Id
  2. Instantiate the template using InstantiateTemplate request and get the Response
  3. Create the Email in CRM using IOrganizationService object or OrganizationServiceContext object
  4. Execute Send Email Request to Send the Email

But recently while working with sending emails, we came across “SendEmailFromTemplateRequest” request which can be used to directly send emails using email templates by following the below steps:

  1. Retrieve the Email Template and get the Template Id
  2. Create an Email object in the code, don’t create in CRM using any service
  3. Simply execute SendEmailFromTemplateRequest request passing the parameters required

 Note: This approach can be used if you don’t want to edit the Email body or subject of an email using the Template.

Below is the code snippet in C# which you can use for reference:

//Return Email Object

Entity emailRecord = SetEmailProperties();

SendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest

{

     // The Email Object created

     Target = emailRecord,

 

     // The Email Template Id

     TemplateId = emailTemplateRef.Id,

 

     // Template Regarding Record Id

     RegardingId = recordId,

 

     //Template Regarding Record’s Logical Name

     RegardingType = recordLogicalName

 

};

 Note: SetEmailProperties() method returns the Email object with all the required attributes set i.e. as shown here.

//create activityparty

 

 Entity Fromparty = new Entity("activityparty");

 

 Entity Toparty = new Entity("activityparty");

 

//To set to Contact

Toparty["partyid"]= new EntityReference("contact", _ contactid));

 

//From set to User

Fromparty["partyid"]= new EntityReference("systemuser", _From));

 

//create email Object and set attributes

Entity email = new Entity("email");

 

email["from"] = new Entity[] { Fromparty };

 

email["to"] = new Entity[] { Toparty };

 

email["directioncode"] = true;

 

//setting the Regarding as Contact

email["regardingobjectid"] = new EntityReference("contact", _contactid);

Hope this helps and saves unnecessary step and request to CRM!

Free 70% of storage space in CRM with Attachment Management Apps!

Attach2Dynamics – Store and manage documents/attachments in cloud storage of your choice – SharePoint, Dropbox or Azure Blob Storage from within Dynamics 365 CRM.
SharePoint Security Sync – Robust and secure solution to integrate Dynamics 365 CRM and SharePoint Security Sync thereby ensuring secure access to confidential documents stored in SharePoint.