I have a visualforce page that I am trying to send from Apex code. I can't use a template for the e-mail and this visualforce page is never actually visited by anyone. I don't want the page to be added as an attachment to the e-mail - I need the body of the e-mail to be my visualforce page. A while back I ran across some code that did something with setting the HTML body of the e-mail to the HTML contents of the page or something like that. Just wondering if anyone had any experience with this, as I have not been able to find anything with my research so far.

Also the visualforce page uses a standard controller based around a custom object I have created. I don't know if that will make a difference. So far I have tried something along the lines of

public static void sendEmailToCounsel(list<Counsel_Review__c> crs){
    Apexpages.StandardController sc = new Apexpages.StandardController(crs[0]);
    ConflictResolutionController crc = new ConflictResolutionController(sc);

    PageReference pr = Page.ConflictResolutionEmail;
    pr.getParameters().put('id', String.valueOf(opp.Id));
    String pageAsString = String.valueOf(pr.getContent());

    Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
    mail.setToAddresses('[email protected]');
    mail.setSubject('Conflict was found inside of salesforce');
    Messaging.sendEmail(new Messaging.Singleemailmessage[] { mail});


  • 2
    What was the outcome of the code you tried? Did it send the Visualforce page as expected or were there any issues? Off the top of my head there could be issues with references to external resources like CSS and images. Commented Oct 23, 2013 at 1:08
  • @DanielBallinger you were exactly right. I made a slight change to the code, passing the id of the opp into the parameter list. This code works and sends the visualforce page as the body of the email but I do not retain any styling. Images appear fine and the basic structure of the tables on my visualforce page come through, just without the nice pageBlock auto styling visualforce has. I am going to try embedding an IFRAME into the email and see if this will allow me to get more of my styling to show.
    – Chris
    Commented Oct 23, 2013 at 11:22
  • 2
    I would be really surprised if any notable email client served up iframe content from a message without warnings. Is there a reason you can't use a Visualforce Email Template? Failing that, you could inline some of the required CSS. Commented Oct 23, 2013 at 23:04

2 Answers 2


Did you ever get this working? I took your start and made a few tweaks to get some basic HTML table data into an email.

public static PageReference sendSearchbyEmail() {
    PageReference pr = Page.svpemail;
    String pageAsString = pr.getContent().tostring();

    Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
    mail.setToAddresses(new list<string>{'[email protected]'});
    mail.setSubject('TEST Page to Email');
    Messaging.sendEmail(new Messaging.Singleemailmessage[] { mail});
    return null;
  • Yeah I did, check the comments to my original post. Glad you got it working!
    – Chris
    Commented Nov 21, 2014 at 0:18

Have you explored using Visualforce email template. Visualforce email templates allow you to create email content using visualforce. You get almost all the capabilities of visualforce while sending final outcome as email content.

For more information refer:- https://www.salesforce.com/docs/developer/pages/Content/pages_email_templates_intro.htm

You must log in to answer this question.

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