Is it possible to send email to an arbitrary recipient using a VF Template?

No there's not - it has to be a record in the system, and it has to be a Contact/User/Lead and not a custom object. I recently found this out the hard way.

You can send emails to any address via Apex, but you can't reference VF templates directly that way. For HTML emails like this, I often use Apex and have a class which is specifically designed to create and handle the HTML for me.


It turns out there is a way to do this that I learned from John Conners. You can set the relatedTo as your user, and then setToAddresses/setBCCAddresses/setCCAddresses.

This causes any merge fields related to the recipient to be filled in with your user (or some dummy user/contact/lead)'s data, but if that doesn't matter for your use case this can be particularly useful - especially for sending a templated email to multiple addresses.

This only works with visualforce email templates.

Example code that will send to your user's email address, as well as [email protected]:

Id templateId = '00Xd0000000Y058';
Id someAccount = '001d000001MMNin';
Messaging.singleEmailmessage em = new Messaging.singleEmailMessage();
em.setTemplateId(templateId);
em.setTargetObjectId(UserInfo.getUserId());
em.setWhatId(someAccount);
em.setSaveAsActivity(false);
em.setToAddresses( new List<String>{ '[email protected]' } );
Messaging.sendEmail( new List<Messaging.Email>{em} );

It comes across looking like this:

enter image description here