Send Email(With Image) from Plugin on Post Operation of an entity in Microsoft Dynamics CRM

In this blog I am going to explain how to send Email to User in CRM from Plugin.

1. Add image url properly if you want to add image inside mail body like below::
     string crmModule = "<html> <img  src='http://1.bp.blogspot.com/-OZdI4vK6hT4/U8-uNZYO99I/AAAAAAAAAro/ehRveHHRgt0/s1600/1032510.png'  width='150' height='150'></html>";

   Or,

  Alternatively, You can also Display Image Which are Created in CRM as Web Resource. Upload your image as Web resource in Crm and Copy paste the URl available in webresource string as I did below.
     string webresource = "<html> <img  src='~/WebResources/new_Crm'  width='205' height='150'></html>";

2. Add necessary referances to your solution.

3. Now, add code like below for sending Email on Post Creation of new Lead

4. Don't forgot to register your plugin message: Create

5. Primary Entity: Lead (Or select whatever you want. Here i have used Lead Entity)

6. Choose Post Operation


Code:: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;

namespace Training1Javascripts._entities.account
{
    class SendEmailPostAccountCreate :IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext Context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(Context.UserId);
           // ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            if (Context.InputParameters.Contains("Target") && Context.InputParameters["Target"] is Entity)
            {
                // Below commented code is for sending mail with Image in body

                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);

                // To the Newly Created Lead User
                Entity to = new Entity("activityparty");
                to["partyid"] = new EntityReference("lead", Lead.Id);

                //Uploaded Crm webresource Url i.e Image added as a web resource in CRM
                string webresource = "<html> <img  src='~/WebResources/new_Crm2013'  width='205' height='150'></html>";

                //published Image Url Link
                string crmModule = "<html> <img  src='http://www.gomonews.com/wp-content/uploads/2009/07/microsoft-logo.jpg'  width='150' height='150'></html>";

                //Hyperlink (or) Re-Direct Url link if you want to place it in your email  signature
                string HyperLink = String.Format("<a href='http://aayushsinghrajput.blogspot.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./ Mrs " + Name;
                Email["description"] = "<h3> Dear  " + Name + "</h3>" + "<br/>" + "Welcome to my blog. Hope it helped you." + "<br/>" + crmModule + "<br/>" + "visit my blog for any query" + HyperLink + "<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);
            }
        }
    }

}

Output will be like this ::





Comments

  1. i have try this plugin example....but get error while i create new lead...please help

    ReplyDelete

Post a Comment

Popular posts from this blog

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Dumps for Microsoft Dynamics CRM MB2-703 Practice Exam Questions Free

How to import CSV files into DataTable in C#

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

go to top image