I have an email template which I am calling it from a work flow rule. Also I have a check box in contact object, when I click on check box the email template will send email to the contact email address. Its working fine.

But I want to know how we can implement controller side. I mean a have a page where I am entering contact details(with email id), when I click on Save i am able to save the record and those records I am displaying in a different table of same page. So from the second table, I will select one contact and when I will click on button(something called send email) in that page, it should send an email to selected to contact.

I am just looking for the logic on how to call an email template from controller and send email to selected record.


This is a very simple piece of code

EmailTemplate templateId = [Select id from EmailTemplate where name = 'Your template Name'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

Use this piece of code to send email from apex using template.

Update: Or you can create a utility class as suggested by @cropredy. You can find reference here.

  • 1
    FWIW, there is a nice fluent pattern to exploit SFDC outbound email services that I've added into my orgs
    – cropredy
    Jun 15 '16 at 18:59
  • @Tushar, I checked the utilityclass idea, but there we are not selecting any email template and instead of that we are using messaging methods in that class. i checked the messaging methods and it doesnt have any method to select the Email template developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – KSL
    Jun 16 '16 at 8:36
  • Ok Thanks Tushar... I can use the above format in my code level
    – KSL
    Jun 16 '16 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.