To use an Email Template from Apex Class in order to send an email:
In order to send an email from apex class, you can use any of the below messaging objects.
Single Email Messaging :
Instantiates the object to send single email message.
Ex: To send single email to a selected Contact.
public void SendEmail()
{
contact con=[Select id from contact limit 1];
EmailTemplate et=[Select id from EmailTemplate where name=:’EmailTemplatename’];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(con.Id);
mail.setSenderDisplayName(‘Charan Tej’);
mail.setTemplateId(et.id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
For other object methods of Single Email Messaging, Click Here
Note: Do remember that we can send only 10 emails in method invocation of Apex class using SingleEmailMessage.
Mass Email Messaging:

Instantiates the object to send mass email message.
Ex: To send mass email to a Contacts.
public void SendEmail()
{
List lstcon=[Select id from contact limit 200];
List lstids= new List();
for(Contact c:lstcon){
lstids.add(c.id);
}
EmailTemplate et=[Select id from EmailTemplate where name=:’EmailTemplatename’];
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(lstIds);
mail.setSenderDisplayName(‘Charan Tej’);
mail.setTemplateId(et.id);
Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}
For other object methods of Mass Email Messaging, Click Here

(1036)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in IBM Austraila as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 10 Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply