+ Start a Discussion
Arnold Joseph TodasArnold Joseph Todas 

Batch Email Message using template

How can i call the email template in batch apex class and send an email?

Thanks

AJ
 

KaranrajKaranraj
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    
Id templateId =  [select id, name from EmailTemplate where developername = 'templateName].id; //replace with your template name
    
email.setToAddresses(toRecipients);
email.setCcAddresses(ccRecipients);
// Who you are sending the email to
email.setTargetObjectId(targetObjId);
email.setWhatId(whatId); //Your Record Id if applicable
email.setorgWideEmailAddressId(orgWideEmailId);
email.setTemplateId(templateId);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
You can directly the query the EmailTemplate object using SOQL query to the template ID and assign that in your email message.