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)

How to programmatically send an email to customer using an Email template with custom HTML?

(0) ShareShare
ReportReport
Posted on by

Hi, 

We have a requirement where we need to send an email using an asp.net web portal to customer. The requirement is to fetch the email template from CRM, and send a personalized email to customer based on the template. Please note that the email template will be a custom HTML template. I know we can fetch the email template using the sdk and send an email using that but here we need to personlize that email like mentioning the customer name etc. So we also need to read the placeholders in the email template embedded in the HTML. 

Kindly advise. 

Thanks, 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    M.Azwar Alam Profile Picture
    on at

    Hi,

    You can try following code to fetch template data and use following link to send email using template

    msdn.microsoft.com/.../gg334461.aspx

    public String GetTemplateDescription(Guid leadid, Guid templateId, string entityName,IOrganizationService service, ITracingService trace)

          {

              string temp = string.Empty;

              try

              {

                  InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest

                  {

                      TemplateId = templateId,

                      ObjectId = leadid,

                      ObjectType = entityName

                  };

                  InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);

                  if (instTemplateResp != null)

                  {

                      Entity template = instTemplateResp.EntityCollection.Entities[0];

                      if (template != null && template.Attributes.Contains("description"))

                      {

                          // temp = template.Attributes["subject"].ToString();

                          //temp += "^";

                          temp = template.Attributes["description"].ToString();

                      }

                  }

                  return HTMLToText(temp);

              }

              catch (Exception ex)

              {

                  throw new InvalidPluginExecutionException("GetTemplateDescription :", ex);

              }

          }

          public string HTMLToText(string htmlString)

          {

              string ss = string.Empty;

              Regex regex = new Regex("\\<[^\\>]*\\>");

              ss = regex.Replace(htmlString, String.Empty);

              return ss;// Plain Text as a OUTPUT

          }

    Mark answer as verified, if it works for you

  • Suggested answer
    Emre GULCAN Profile Picture
    2,379 on at

    Hi Fahad,

    Do you use OOB template entity? If yes, MS CRM replace placeholders byself, because of you have to select an entity before you create template and you can only select this entity's attributes for placeholders.

    You can do it as explained in SDK, and you can also do it simply with using XrmLibrary by 2 lines of code.

    EntityReference regarding = new EntityReference("Regarding logical name", Guid.Parse("Record Id"));
                
    Entity emailEntity = new Entity("email");
    emailEntity["....."] = "....";
    
    EmailHelper helper = new EmailHelper(_organizationService);
    
    //Send with template Id
    helper.Send(emailEntity, Guid.Parse("Template GUID"), regarding);
    
    //Send with template name
    helper.Send(emailEntity, "Template Name", regarding);

    But if you use custom entity for templates instead of OOB template entity, you should create placeholders with standart format (ex : dear {{firstname}} {{lastname}} ....) and replace this placeholders in your code with your real-data

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Fahad,

    It is possible to read the placeholders in the email template embedded in the HTML and send the email with this template.

    See: nishantrana.me/.../sample-code-to-use-templates-content-while-sending-mail-using-sendemailreuquest-in-crm-2011

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