function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC coderSFDC coder 

blank email rendered when using sethtmlbody method

hi all,

i have a scenario where in i query an html email template and populate its merge fields in my apex class itself.
however,when i use the sethtmlBody() method,it renders an empty email.
So i have to comment the sethtmlbody() and go with setplaintextbody().

What can be the reason for this?
Also how do i use the setHtmlBody() along with setplainTextBody()?

Below is my code
public static void sendingEmail(String cid,String cname,String eventId)
        String plainTxtBody='',startDate='',endDate='',scheduledDate='';
        String hbody='';
        Date sdate;

        //get the email template
        EmailTemplate template=[SELECT HtmlValue,Body FROM EmailTemplate WHERE          DeveloperName='Meeting_Reminder'];
        Event eve=[select subject,StartDateTime,EndDateTime,,Owner.Email,Owner.Phone 
                    From Event where Id=:eventId];
        //setting the merge fields
        //Construct the email message
        Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
        mail.setSubject('Meeting Reminder');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});     
Also can anyone explain why do we have to set the plaintextbody() even when the email template is of type html?

Any help is appreciated...

Best Answer chosen by SFDC coder
SFDC coderSFDC coder
hi sonam,

i solved this issue by changing the email template type from HTML with Letterhead to custom HTML template and now its working fine.
However i still dont understand as to why its not working with the letterhead type.Do you have any clue?

All Answers

SonamSonam (Salesforce Developers) 
I have used the same code(triggered via trigger on Contact object) in my test ORG and am able to get the email with content with both :

public class Emailsend

public static void sendingEmail(String cid,String cname,String eventId)
        String plainTxtBody='',startDate='',endDate='',scheduledDate='';
        String hbody='';
        Date sdate;

        //get the email template
        EmailTemplate template=[SELECT HtmlValue,Body FROM EmailTemplate WHERE Name='Meeting_Reminder'];
        Event eve=[select subject,StartDateTime,EndDateTime,,Owner.Email,Owner.Phone 
                    From Event where Id=:eventId];
        //setting the merge fields
        //Construct the email message
        Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
        mail.setSubject('Meeting Reminder');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});     

Please try to deliver the email to any other email client(say gmail/yahoo) might be that the HTML content is being blocked at the email server level and not appearing in the inbox..
SFDC coderSFDC coder
hi sonam,

i tried the same code.still getting a blank body.However when i receive an email,a small email notification pops up in the bottom right corner of the window whre the contents are visible but when i click and open it up everything disapperas.

Also when i comment out the setHtmlBody() method,the plain text is delivered sucessfully.
What can be the problem?
i have created an email template of type HTML letterhead where email layout is of type free form layout.
Can you please help??
its a long time now since i am stuck with this issue.  :( Its little urgent.. 

SFDC coderSFDC coder
hi sonam,

i solved this issue by changing the email template type from HTML with Letterhead to custom HTML template and now its working fine.
However i still dont understand as to why its not working with the letterhead type.Do you have any clue?
This was selected as the best answer
Jerrod Knapp 11Jerrod Knapp 11
I am having the same issue when Apex generates an HTML letterhead email.  The email will come in as blank.  I cannot use HTML w/o letterhead because we use letterhead.  Any help on this would be awesome!!  URGENT!  Thanks to everyone in advance.
abhik dey 1abhik dey 1
Hi Everyone, Just wanted to check if there is a solution to this. ?

I have to use template with letter head and if I use sethtmlbody, it blanks out the entire email and customers receiving empty emails.

David Roberts 4David Roberts 4
Could you not include the letter head design (as html) in your template?
Mahesh AdiMahesh Adi
Did anyone solved this? I got ended with same issue.
Mahesh AdiMahesh Adi
It worked for me when I remove below lines, no need to set them but make sure set WhatId()
mail.setSubject('Meeting Reminder');

Dewald Steenkamp 4Dewald Steenkamp 4
The Messaging.sendEmail returns a result of List<Messaging.SendEmailResult>, that can tell you why an email was not sent.  In my case, it was having an issue because I was using the .setWhatId method and did not need to.
Messaging.SingleEmailMessage mess = new Messaging.SingleEmailMessage();
        mess.setToAddresses(new String[]{cbsQuoteRef.Contact__r.Email});
        mess.setFileAttachments(new Messaging.EmailFileAttachment[]{mailAtt});
        List<Messaging.SendEmailResult> result = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mess},false);
        System.debug('emailresult:' + result);

Hope this helps someone out there, as it was driving me insane.
Hai HaHai Ha
Figured out the issue. The html contains <![CDATA[ .... ]]>
Need to remove the <![CDATA[ and ]]> from the html.
Vijay LaxmiVijay Laxmi
Hi @Hai Ha 
Could you please tell me how you have removed this from apex class while sending email
Prashant Kshirsagar 10Prashant Kshirsagar 10

This worked for me when I removed the CDATA from html as suggested by @Hai Ha. Below is the link to remove CDATA
Ankit Agrawal 28Ankit Agrawal 28

I also faced the same issue, Body was not getting rendered only in Outlook when Replacing any content, but thanks to @Hai Ha, and @Prashant, removing these tags works for me. But, Letterhead is not coming, whether we are openng in web mail or in outlook.

Can someone help on it?

Do not set the 


These are explicitly set by the template and if you set them in the apex code you will override them. Even if you only set the subject in apex it will also negate the use of the template for the htmlBody. So, don't set either of these for a plain text templated emails or it will not use the template.