+ Start a Discussion
abhik dey 1abhik dey 1 

Salesforce sends Blank email gets sent out in single email message if setting HTMLBODY

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

Below code snippet:

public class testclassforemail {
    public static void sendemail(){
        EmailTemplate etdfinalNotification=[Select id,HtmlValue from EmailTemplate where name = 'letterheadtemplate' limit 1];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTemplateId(etdfinalNotification.Id);
        mail.setTargetObjectId(userinfo.getUserId());
        mail.setToAddresses(new String[] {'testuser@gmail.com'});
        String htmlbdy = etdfinalNotification.HtmlValue.replace('{!User.Name}','mynamecheck');
        mail.setHtmlBody(htmlbdy);
      //  mail.setSubject('User De-activate Status');
        //   mail.setPlainTextBody('The batch Apex job processed111 ');
        mail.setSaveAsActivity (false);
        mail.setTreatTargetObjectAsRecipient(false);
        system.debug('***'+mail);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

If I comment out the sethtmlbody and the setsubject , it works fine and the email gets delivered. However inorder to change the merge fields I have to use SETHTMLBODY with string replace.

Anyone experience this issue? or any workaround will help