I am trying to send an email from trigger using SingleEmailMessage.

As of now, I am composing my email body using the method setHTMLBody inside the trigger itself and the email recipients are: user, contact and some hard-coded email address also.

My email has many recipients and I have some complex logic so I have to go for visualforce email template.

While I tried, I found the following points:

  1. I have to use singleemailmessage only as I have to use VisualForce email template
  2. More than one id cannot be added in the setTargetObjectId method so I cannot mail to more than one person.
  3. Also if I use an email template, I have to specify the recipient type as User/Lead/Contact but in my case I have to send email to user & contact & some email address. As of now, I am doing it using setToAddresses method.

My Question: Is it possible to replace the email body using VisualForce email template instead of composing it using setHTMLBody method?

2 Answers 2


In order to use VF Email templates, you have to send an email to a Lead, Contact, or User via setTargetObjectId with the associated merge fields.

I don't believe you can freeform an email address.

Also be aware sending single emails you max out at 10 emails per request, so you'll have to make sure that your trigger doesn't send anymore than 10 emails.

If it does, you'll need to use 'Bulk email' which doesn't allow templates either.

  • So, it means that it is good to send the email directly instead of sending via VisualForce email template?
    – Priyanka
    Sep 12, 2012 at 7:26
  • 1
    Yes, or temporarily create a Lead or Contact record (which you can than discard after the message is sent). This becomes more complicated if you have any logic associated with creating Lead or Contact records. Sep 12, 2012 at 7:29

I wrote a blog on how to approach through workflows and visualforce template .Hope this may help in alternate designing


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .