How To Email Documents From Salesforce

How To Email Documents From Salesforce

By admin December 3, 2013
Blog, Salesforce Development 0

Another great Salesforce development question has been sent to our team: “How can I send list of documents as an email attachment within Salesforce.” Here, we will give you a step by step walk through of how you can get a list of all documents in a Visualforce page and how to send it via email as an attachment. For example, we have different types of documents like a PDF, Word Document, Excel Sheet etc. We have to send all these documents together to a specific email address from the Salesforce CRM.


Step 1 –

Go To Setup → Develop → Pages

Click the “New” button for creating new pages:


Step 2 – In the label and name box type “Email Documents”


Step 3 – In Visualforce editor paste the following code:

<apex:page controller=”EmailDocument” showHeader=”false” sidebar=”false”>
<apex:sectionHeader title=”Document Example” subtitle=”Email a Document” description=”Example of how to email a Document.”/><apex:form >
<apex:pageMessages />
<apex:pageBlock title=”Enter Email Address”><apex:pageBlockButtons >
<apex:commandButton action=”{!emailDoc}” value=”Send Document”/>
<apex:pageBlockSectionItem >
<apex:outputLabel value=”Email to send to” for=”email”/>
<apex:inputText value=”{!email}” id=”email”/>
<apex:pageBlockSectionItem >
<apex:outputLabel value=”Document” for=”document”/>
<apex:selectList multiselect=”true”  value=”{!documentId}” id=”document” size=”4″>
<apex:selectOptions value=”{!findDocument}”/>

Step 4 – Paste this controller code in Apex class editor:

public with sharing class EmailDocument{public list<ID> documentId {get;set;}
public String email {get;set;}public list<selectoption> getfindDocument(){
list<selectoption> doclist = new list<selectoption>();
for(Document docs:[select id, name, type from Document order by name]){
doclist.add(new SelectOption(,’ – ‘+docs.type));
return doclist;
}public PageReference emailDoc() {list<Document> docmnt= [select id, name, body, contenttype, developername, type from Document where id = :documentId];
for(Document doc:docmnt){
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.Body = doc.Body;Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] { email });
mail.setSubject(‘Demo of Document Email’);
mail.setHtmlBody(‘Here is an attachment in your mail: ‘;
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, ‘Email with Document ‘ ‘ sent to ‘+email));
return null;


“Save” this code

Step 5 – Open this page in your Salesforce organization to check its working correctly: “ Email Documents”

Step 6 – Enjoy! If you have any questions, comments etc. please feel free to let us know. As always, please feel free to get in touch with any of our Salesforce developers as they would be more than happy to assist you with any of your Salesforce development needs.

Related Posts

Popular post