{"id":5839,"date":"2017-06-09T18:21:11","date_gmt":"2017-06-09T12:51:11","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=5839"},"modified":"2021-08-25T11:51:13","modified_gmt":"2021-08-25T11:51:13","slug":"tip-using-sendemailfromtemplaterequest-to-directly-send-an-email-using-the-email-template","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2017\/06\/tip-using-sendemailfromtemplaterequest-to-directly-send-an-email-using-the-email-template\/","title":{"rendered":"Tip: Using SendEmailFromTemplateRequest to directly send an email using the Email Template"},"content":{"rendered":"<p>Earlier when we had an Email Template created for any entity, we used to follow the below <strong>four steps to send an email using Template:<\/strong><\/p>\n<ol>\n<li><strong>Retrieve the Email Template and get the Template Id<\/strong><\/li>\n<li><strong>Instantiate the template using InstantiateTemplate request and get the Response<\/strong><\/li>\n<li><strong>Create the Email in CRM using IOrganizationService object or OrganizationServiceContext object<\/strong><\/li>\n<li><strong>Execute Send Email Request to Send the Email<\/strong><\/li>\n<\/ol>\n<p>But recently while working with sending emails, we came across <strong>\u201cSendEmailFromTemplateRequest\u201d<\/strong> request which can be used to directly send emails using email templates by following the below steps:<\/p>\n<ol>\n<li><strong>Retrieve the Email Template and get the Template Id<\/strong><\/li>\n<li><strong>Create an Email object in the code, don\u2019t create in CRM using any service<\/strong><\/li>\n<li><strong>Simply execute <\/strong><strong>SendEmailFromTemplateRequest<\/strong><strong> request passing the parameters required<\/strong><\/li>\n<\/ol>\n<blockquote><p><strong><em>\u00a0Note: This approach can be used if you don\u2019t want to edit the Email body or subject of an email using the Template.<\/em><\/strong><\/p><\/blockquote>\n<h4><strong>Below is the code snippet in C# which you can use for reference:<\/strong><\/h4>\n<pre class=\"lang:default decode:true \">\/\/Return Email Object\r\n\r\nEntity emailRecord = SetEmailProperties();\r\n\r\nSendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest\r\n\r\n{\r\n\r\n\u00a0\u00a0\u00a0\u00a0 \/\/ The Email Object created\r\n\r\n\u00a0\u00a0\u00a0\u00a0 Target = emailRecord,\r\n\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0 \/\/ The Email Template Id\r\n\r\n\u00a0\u00a0\u00a0\u00a0 TemplateId = emailTemplateRef.Id,\r\n\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0 \/\/ Template Regarding Record Id\r\n\r\n\u00a0\u00a0\u00a0\u00a0 RegardingId = recordId,\r\n\r\n\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0 \/\/Template Regarding Record\u2019s Logical Name\r\n\r\n\u00a0\u00a0\u00a0\u00a0 RegardingType = recordLogicalName\r\n\r\n\u00a0\r\n\r\n};<\/pre>\n<blockquote><p>\u00a0<strong><em>Note: SetEmailProperties() method returns the Email object with all the required attributes set i.e. as shown here.<\/em><\/strong><\/p><\/blockquote>\n<pre class=\"lang:default decode:true \">\/\/create activityparty\r\n\r\n\u00a0\r\n\r\n\u00a0Entity Fromparty = new Entity(\"activityparty\");\r\n\r\n\u00a0\r\n\r\n\u00a0Entity Toparty = new Entity(\"activityparty\");\r\n\r\n\u00a0\r\n\r\n\/\/To set to Contact\r\n\r\nToparty[\"partyid\"]= new EntityReference(\"contact\", _ contactid));\r\n\r\n\u00a0\r\n\r\n\/\/From set to User\r\n\r\nFromparty[\"partyid\"]= new EntityReference(\"systemuser\", _From));\r\n\r\n\u00a0\r\n\r\n\/\/create email Object and set attributes\r\n\r\nEntity email = new Entity(\"email\");\r\n\r\n\u00a0\r\n\r\nemail[\"from\"] = new Entity[] { Fromparty };\r\n\r\n\u00a0\r\n\r\nemail[\"to\"] = new Entity[] { Toparty };\r\n\r\n\u00a0\r\n\r\nemail[\"directioncode\"] = true;\r\n\r\n\u00a0\r\n\r\n\/\/setting the Regarding as Contact\r\n\r\nemail[\"regardingobjectid\"] = new EntityReference(\"contact\", _contactid);<\/pre>\n<p>Hope this helps and saves unnecessary step and request to CRM!<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">Free 70% of storage space in CRM with Attachment Management Apps!<\/div><\/div><\/h2>\n<p><em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/attach-2-dynamics-365-crm-upload-multiple-files-sharepoint-cloud-storage\" target=\"_blank\" rel=\"noopener noreferrer\">Attach2Dynamics<\/a> &#8211; Store and manage documents\/attachments in cloud storage of your choice &#8211; SharePoint, Dropbox or Azure Blob Storage from within Dynamics 365 CRM.<\/em><br \/>\n<em><strong><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/dynamics-365-crm-sharepoint-security-metadata-sync\" target=\"_blank\" rel=\"noopener noreferrer\">SharePoint Security Sync<\/a><\/strong> \u2013 Robust and secure solution to integrate Dynamics 365 CRM and SharePoint Security Sync thereby ensuring secure access to confidential documents stored in SharePoint.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: Retrieve the Email Template and get the Template Id Instantiate the template using InstantiateTemplate request and get the Response Create the Email in CRM using IOrganizationService object or OrganizationServiceContext object\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2017\/06\/tip-using-sendemailfromtemplaterequest-to-directly-send-an-email-using-the-email-template\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":5853,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15,16,19],"tags":[1571,1575],"class_list":["post-5839","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","category-dynamics-365","category-dynamics-crm","tag-send-an-email-using-email-template","tag-sendemailfromtemplaterequest"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/5839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=5839"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/5839\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/5853"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=5839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=5839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=5839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}