I'm trying to send email from Apex using a VF Email Template. The <messaging:emailTemplate>
tag in the template specifies recipientType='contact' relatedToType='MyCustomObj__c'
.
I'm using code taken from the answer to this questionthe answer to this question, basically verbatim:
- create a new Messaging.singleEmailMessage()
- setTargetObjectId(a valid contactid)
- setWhatId(Id of a MyCustomObj__c)
- setTemplateId(id of my VF template)
- setSaveAsActivity(true)
- Messaging.sendEmail(emails, false)
The only difference in my code is that I repeat steps 1-5 to create a list<Messaging.Email>, add each message to the list after step 5, and save them all at once in step 6. This fails every time with message: Messaging.SendEmailError[getTargetObjectId=null]
. If I instead setSaveAsActivity(false)
, it succeeds. I have 'Enable Email Tracking' checked in Activity Settings in my org. Is there another setting I need to enable email tracking of mail sent to contacts? Can I not SaveAsActivity when using a Visualforce email template? I can't find any explicit prohibition. Something else?