+ Start a Discussion

Creating HTML Email by Apex


I have trouble with creating HTML formatted EmailMessages by APEX.


EmailMessage is created, but Salesforce does not display the label "HTML Body" in layout.


            sSubject = 'Some text';
            sBody = '<html><body><p>here comes the body</p></body></html>';      
            // Create an Email Message
            EmailMessage objEmailMessage = new EmailMessage(ParentID = objCase.ID,
                                                            MessageDate = datetime.now(),
                                                            Subject = sSubject,  
                                                            ToAddress = sToMail,
                                                            CcAddress = sToMailCC,
                                                            BCcAddress = sToMailBCC,
                                                            HtmlBody = sBody,
                                                            FromName = Userinfo.getName());
            objEmailMessage.FromAddress = Userinfo.getUserEmail();            
            insert objEmailMessage;      



I see the email message in Salesforce. Open this mesaage. Just "Text Body" is visible.


Do I miss anything? I need "HTML Body" to be visible.

Do you possibly have a soluton for me?

Thanks a lot!


remove HtmlBody .. try somthign like this


  Messaging.SingleEmailMessage objEmailMessage= new Messaging.SingleEmailMessage();     


use   objEmailMessage.setHtmlBody(sbody);    


Not possible. Got error message:
Save error: Invalid field Body for SObject EmailMessage


sorry i posted wrong message plz see my updated post



No, it does not function.

In 1 step I send an email using "SingleEmailMessage" -> no problems. User sees html body.

In 2 step I like to have the sent email in Salesforce, hence i used "EmailMessage" object.

Your solution regards 1.

I need a solution for step 2.


I think sfdc is showing the body in TEXT format. just pus some system.debug to verify if the html tags are still there

Also, try this body   sBody = '<html><body><p>here <B>comes</B> the body</p></body></html>';      


I think, you are right. Just the Text Content is displayed. There is no possibility to show HTML Body by default.

In my case the Text Body is empty, so the only solution would be to create a new VF page with HTML Body instead of Text Body.