[SalesForce] Sending VF email template using SingleEmailMessaging- Getting blank Email body

I have a VF email template as below which has relatedTOType as Job_Application__c and recipientType as User. I am using some merge field in this template:

<messaging:emailTemplate subject="TEST" recipientType="User" relatedToType="Job_Application__c">
<messaging:htmlEmailBody >
 <html><body> 
Hello {!relatedTo.Job_Opening__r.Hiring_Manager__r.Name},
<br/><br/>


Project : {!relatedTo.Job_Opening__r.Account__r.Name}<br/>
SO: {!relatedTo.Job_Opening__r.SO__c}<br/>
Candidate Name : {!relatedTo.Candidate__r.Name}<br/>
Candidate Phone: {!relatedTo.Candidate__r.Mobile__c}<br/>
Candidate Email: {!relatedTo.Candidate__r.Email__c}<br/>

<br/><br/>

Please <a href="mailto:recruitmentapp0@gmail.com?Subject=Interview Assign {!relatedTo.Id}&body=Enter Employee ID :" target="_top">Reply with Employee Id</a> to Assign an interviewer 
or click <a href="mailto:recruitmentapp0@gmail.com?Subject=Interview Assign {!relatedTo.Id}&body=Smart Assign" target="_top">Smart Assign</a> to automatically assign an interviewer <br/>

<br/>

Thank You<br/>
Candid Application Admin
        </body></html>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>

When I use a singleemailmessaging class to send an email I get an email with empty body. I researched a bit to find out the I need to mention whatID to be used as relatedToType object id. I had to comment- setTargetObjectId as I do not have contacts in my org(My org does not have Sales cloud) and setSaveAsActivity as false. Doing this I get a blank email body. Part of my code which send email:

     EmailTemplate et = [Select Id,Name from EmailTemplate where Name = 'The VF email template name'];

              Contact cnt = [select id, Email from Contact where email != null limit 1];
     mail.setTemplateId(et.Id); 
     mail.setWhatId(jobapp.id);
     mail.setTargetObjectId(cnt.id); 
     //mail.setSaveAsActivity(false); 
     mail.setToAddresses( toAddresses );
     mail.setOrgWideEmailAddressId(owea.get(0).Id);
     mail.setSubject(Candidate.Name +' '+'has been assigned to the project'+' '+'"'+jobopenEmail.Account_Name__c+'"' +' '+'('+' '+'SO#'+' '+jobopenEmail.SO__c +')' + ' '
                     +' '+jobapp.id);
     Savepoint sp = Database.setSavepoint();
     Messaging.SendEmailResult [] r = 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});   

     Database.rollback(sp); 

     Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
     emailToSend.setToAddresses(mail.getToAddresses());
     //emailToSend.setPlainTextBody(mail.getPlainTextBody());
     emailToSend.setHTMLBody(mail.getHTMLBody());
     emailToSend.setSubject(mail.getSubject());
     Messaging.SendEmailResult [] sndmail = 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {emailToSend});   

I think my solution should be here – https://appirio.com/tech-blog/sending-emails-without-targetobject-id-apex
Any suggestion what I might be missing here?

Best Answer

Following link has an answer to the question: Using APEX to assemble HTML Letterhead Emails

Summer 16 introduced new method:

Messaging.SingleEmailMessage email = 
            Messaging.renderStoredEmailTemplate(templateId, whoId, whatId);

Instead of using Messaging.SingleEmailMessage email=new SingleEmailMessage(); we have to use

 Messaging.SingleEmailMessage email = 
            Messaging.renderStoredEmailTemplate(templateId, whoId, whatId);

This will take care of retrieving the body and merging the field in VF email templates

Related Topic