Search This Blog

Friday, October 6, 2017

Dynamically add Embeded/attach image in Email body in Dynamics CRM

IS it possible to Embeded/attach image in Email body in Dynamics CRM 

Embeded image in email body:

To add dynamically image in the email, usually we write code to create email.

There are three ways our image will add in the CRM

  • Public images hosted in external website
  • Webresource
  • Notes( add through attachment)

The below code is for "Public images hosted in external website" and "Webresource"
//Uploaded image in the Webresource .
string webresource = "<html> <img  src='~/WebResources/new_image'  width='205' height='150'></html>";

//published Image Url Link
string crmModule = "<html> <img  src='http://images.all-free-download.com/images/graphiclarge/beautiful_nature_landscape_05_hd_picture_166223.jpg'  width='150' height='150'></html>";


 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//CRM SDK Namespace
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;

namespace Sample
{
    public class Email : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context =
            (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {

                Entity Lead = (Entity)context.InputParameters["Target"];
                string Emailid = Lead.GetAttributeValue<string>("emailaddress1");
                string Name = Lead.GetAttributeValue<string>("lastname");

                //From Current User
                Entity from = new Entity("activityparty");
                from["partyid"] = new EntityReference("systemuser", context.UserId);

                
                Entity to = new Entity("activityparty");
                to["partyid"] = new EntityReference("lead", Lead.Id);

                //Uploaded Crm webresource Url.
                string webresource = "<html> <img  src='~/WebResources/new_image'  width='205' height='150'></html>";

                //published Image Url Link

                string crmModule = "<html> <img  src='http://images.all-free-download.com/images/graphiclarge/beautiful_nature_landscape_05_hd_picture_166223.jpg'  width='150' height='150'></html>";

                //Hyperlink (or) Re-Direct Url link
                string Hyper = String.Format("<a href='https://google.com/'> Click Here</a>");

                //Create Email
                Entity Email = new Entity("email");
                Email["from"] = new Entity[] { from };
                Email["to"] = new Entity[] { to };
                Email["subject"] = "welcome Mr : " + Name;
                Email["description"] = "<h3> Dear  " + Name + "</h3>" + "<br/>" + "Microsoft Dynamics CRM 365 Module" + "<br/>" 
                    + crmModule + "<br/>" +  Hyper + "<br/>" + webresource;

                //SendEmailRequest
                Guid _emailId = service.Create(Email);
                SendEmailRequest reqSendEmail = new SendEmailRequest();
                reqSendEmail.EmailId = _emailId;
                reqSendEmail.TrackingToken = "";
                reqSendEmail.IssueSend = true;
                SendEmailResponse res = (SendEmailResponse)service.Execute(reqSendEmail);

            }
        }
    }
}



The below code is for "Notes"
string crmModule = "<html> <img src='data: image/jpeg; base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw / eHBhY2tldCBiZWdpbj0i77u / IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8 + IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZ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f9CjAAluzmdX4AEIIAAAAASUVORK5CYII ='></html>";


Usually this code works in normal page but here image is not embedded with email after send. It says harmful in the email.

When I check with how inserting signature in the email works in the CRM. I have checked through sent email description column through web API and found they URL in the image tag

Check the selected item








2 comments:

  1. Kingroot Apk is a Chinese rooting tool which helps you root your phone in one tap. have a question on kingroots safety?
    is kingroot safe

    ReplyDelete
  2. There is a catch, though–the image MUST be hosted on a publicly-accessible URL. Unfortunately, this means HTTPS won’t cut it, which rules out using Web Resources. If you end up doing a Google image search to find the image (helpful for logos), be sure to use one with a reliable host. This helps minimize the risk of the image suddenly disappearing in the future!
    Know more here - MS Dynamics Development Services In USA

    ReplyDelete

Note: Only a member of this blog may post a comment.