web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CALLING WEB API POST REQUEST THROUGH PLUGIN

(0) ShareShare
ReportReport
Posted on by

i have created a web api that sends email using smtp client. 

now i have called that api in my plugin which is a post request. 

The problem is i am able to send plain text through email body but if it is html content the body is null. 

Why is it so?

Please provide me with the solution

I m using sandbox enviornment. Online CRM

*This post is locked for comments

I have the same question (0)
  • ashlega Profile Picture
    34,477 on at

    Hi,

     you did not mention if you were able to confirm your web api works outside of CRM. Create a console application, use hte same code you have in the plugin to call your web api, see what happens. If you get an empty email, the problem  does not have anything to do with CRM/plugin then..

  • Community Member Profile Picture
    on at

    Hi 

    i tried sending the html data through console application but the mail comes as

    <p>This is a test email for api</p>.

    Values are coming from crm.

  • Community Member Profile Picture
    on at

    Value of body is coming in the format &lt;&amp; p&gt; &amp;

  • Community Member Profile Picture
    on at

    Hi,

    please check when the plugin trigger, Pre or Post ?

  • Community Member Profile Picture
    on at

    Hi,

    You need to check smsclient class property. IsBodyHtml = true;

    or

    htmlencoding/htmldecoding

  • Community Member Profile Picture
    on at

    Hardik,

    my plugin triggers on post operation

  • Community Member Profile Picture
    on at

    There are not issue with plugin. (problem in webapi code)

    please try to send email using console application.

    1. email body "<p>This is a test email for api</p>"

    2. email body "<p>This is a test email for api</p>"

    3. email boy "<![CDATA[<p>This is a test email for api</p>]]>"

  • Community Member Profile Picture
    on at

    i have used this code in my console app

    string encodebody = System.Net.WebUtility.HtmlDecode(objEmail.body);

    to decode body content.

    When i send mail through console application the mail is sent successfully and html tags are also rendered. But what to do in case of plugin.

  • Community Member Profile Picture
    on at

    Can you shared you plugin code?

  • Community Member Profile Picture
    on at

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Email;

    using System.Net;

    using System.Net.Http;

    using System.Net.Http.Headers;

    using System.Net.Mail;

    using System.Web;

    using System.Web.Http;

    using System.Web.Mvc;

    using System.Web.Script.Serialization;

    using System.Configuration;

    using System.IO;

    using System.Runtime.Serialization.Json;

    namespace DDU.CALLAPI_PLUGIN

    {

       public class Class1 : IPlugin

       {

           #region Class Member Declaration

           IPluginExecutionContext pluginexecutioncontext;

           IOrganizationServiceFactory servicefactory;

           IOrganizationService service;

           #endregion

           public void Execute(IServiceProvider serviceProvider)

           {

               #region Class Member Definition

               pluginexecutioncontext = (IPluginExecutionContext)serviceProvider.GetService((typeof(IPluginExecutionContext)));

               servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService((typeof(IOrganizationServiceFactory)));

               service = servicefactory.CreateOrganizationService(pluginexecutioncontext.UserId);

               #endregion

               Entity ObjPrimaryEntity = (Entity)pluginexecutioncontext.InputParameters["Target"];

               //Entity ObjPrimaryEntity = (Entity)service.Retrieve("email", pluginexecutioncontext.PrimaryEntityId, new ColumnSet(true));

               EmailActivityModel objEmail = new EmailActivityModel();

               objEmail.To = ObjPrimaryEntity.GetAttributeValue<EntityCollection>("to").Entities[0].GetAttributeValue<EntityReference>("partyid").Id; //gets id

               Entity obRegistration = (Entity)service.Retrieve("new_registration", objEmail.To, new ColumnSet(true));

               objEmail._To = obRegistration != null ? Convert.ToString(obRegistration.Attributes["new_emailaddress"]) : string.Empty; //gets email address

               objEmail.From = ObjPrimaryEntity.GetAttributeValue<EntityCollection>("from").Entities[0].GetAttributeValue<EntityReference>("partyid").Id;//gets id

               Entity obSystemUser = (Entity)service.Retrieve("systemuser", objEmail.From, new ColumnSet(true));

               objEmail._From = obSystemUser != null ? Convert.ToString(obSystemUser.Attributes["internalemailaddress"]) : string.Empty; //gets email address

               objEmail.Subject = ObjPrimaryEntity.GetAttributeValue<string>("subject");

               objEmail.body = ObjPrimaryEntity.GetAttributeValue<string>("description");

               #region Post - Send email

               string decodebody = System.Net.WebUtility.HtmlDecode(objEmail.body);

               ASCIIEncoding encoding = new ASCIIEncoding();

               string postData = "To=" + objEmail._To;

               postData += ("&Subject=" + objEmail.Subject);

               postData += ("&body=" + decodebody);

               postData += ("&From=" + objEmail._From);

               postData += ("&CC=" + objEmail.CC);

               byte[] Postdata = encoding.GetBytes(postData);

               #endregion

               #region Prepare Web Request

               // Prepare web request...

               try

               {

                   WebRequest request = WebRequest.Create("uatservice.azurewebsites.net/.../SendEmail&quot;);

                   request.Method = "POST";

                   request.ContentLength = Postdata.Length;

                   request.ContentType = "application/x-www-form-urlencoded";

                   Stream dataStream = request.GetRequestStream();

                   //  dataStream.Length = 1073741823;

                   dataStream.Write(Postdata, 0, Postdata.Length);

                   dataStream.Close();

                   WebResponse response = request.GetResponse();

                   string desc = ((HttpWebResponse)response).StatusDescription;

                   dataStream = response.GetResponseStream();

                   StreamReader reader = new StreamReader(dataStream);

                   string responseFromServer = reader.ReadToEnd();

                   reader.Close();

                   dataStream.Close();

                   response.Close();

               }

               catch (InvalidPluginExecutionException ex)

               {

               }

               #endregion Prepare Web request

           }

           public struct PostData

           {

               public string _To { get; set; }

               public string FromEmail { get; set; }

               public string Body { get; set; }

               public string Subject { get; set; }

           }

       }

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans