Activity Forums Salesforce® Discussions I am new to salesforce. i want to send email with attachment

  • I am new to salesforce. i want to send email with attachment

    Posted by Louis on August 24, 2017 at 5:49 pm

    I am new to salesforce. i want to send email with attachment that stored in static resources. please tell me how to do it

    Answer-

    public class EmailStaticResource {

    public void StaticresourceDataAsEmail(){

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

    email.setUseSignature(false);

    email.setSaveAsActivity(true);

    email.setSubject('Send Email using staticResource as body of mail ');

    String[] toAddresses = new String[] {'[email protected]'};

    email.setToAddresses(toAddresses);

    email.setHtmlBody('<html><body>Dear devoplers<b>See the code for Email service</b></body></html>');

    StaticResourcesr = [Select  Name, Id, Body From StaticResource where Name = 'Document'];

    Blob tempBlob = sr.Body;

    Messaging.EmailFileAttachmentefa = new Messaging.EmailFileAttachment();

    efa.setBody(tempBlob);

    efa.setFileName('attachment.pdf');

    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

    Messaging.SingleEmailMessage[] emailList = new Messaging.SingleEmailMessage[] {email};

    Messaging.sendEmail(emailList);

    }

    }

     

    William replied 6 years, 5 months ago 3 Members · 2 Replies
  • 2 Replies
  • Shaharyar

    Member
    August 29, 2017 at 5:21 am

     

    public class EmailStaticResource {

    public void StaticresourceDataAsEmail(){

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

    email.setUseSignature(false);

    email.setSaveAsActivity(true);

    email.setSubject(‘Send Email using staticResource as body of mail ‘);

    String[] toAddresses = new String[] {‘[email protected]’};

    email.setToAddresses(toAddresses);

    email.setHtmlBody(‘<html><body>Dear devoplers<b>See the code for Email service</b></body></html>’);

    StaticResourcesr = [Select  Name, Id, Body From StaticResource where Name = ‘Document’];

    Blob tempBlob = sr.Body;

    Messaging.EmailFileAttachmentefa = new Messaging.EmailFileAttachment();

    efa.setBody(tempBlob);

    efa.setFileName(‘attachment.pdf’);

    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

    Messaging.SingleEmailMessage[] emailList = new Messaging.SingleEmailMessage[] {email};

    Messaging.sendEmail(emailList);

    }

    }

  • William

    Member
    July 24, 2018 at 6:54 am

    Here is the method, You can send the document/email with an attachment:

    public with sharing class EmailDocumentSend {

    public list<ID> documentId {get;set;}

    public String email {get;set;}

    public list<selectoption> getfindDocument(){

    list<selectoption> documentlist = new list<selectoption>();

    for(Document docs:[select id, name, type from Document order by name]){

    documentlist.add(new SelectOption(docs.id, docs.name+' – '+docs.type));

    }

    return documentlist;

    }

    public PageReference emailDocSend() {

    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.setContentType(doc.contentType);

    attach.setFileName(doc.developerName+'.'+doc.type);

    attach.setInline(false);

    attach.Body = doc.Body;Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    mail.setUseSignature(false);

    mail.setToAddresses(new String[] { email });

    mail.setSubject('Email from Salesforce');

    mail.setHtmlBody('Salesforce Doc is: '+doc.name);

    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });

    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Document '+doc.name+email));

    }

    return null;

    }

    }

     

    Visualforce Page

    <apex:page controller="EmailDocumentSend" showHeader="false" sidebar="false">

    <apex:sectionHeader title="Email Document " subtitle="Email a Document"/>

    <apex:form >

    <apex:pageMessages />

    <apex:pageBlock title="Enter Email Address">

    <apex:pageBlockButtons >

    <apex:commandButton action="{!emailDocSend}" value="Send Document"/>

    </apex:pageBlockButtons><apex:pageBlockSection>

    <apex:pageBlockSectionItem >

    <apex:outputLabel value="Email to send to" for="email"/>

    <apex:inputText value="{!email}" id="email"/>

    </apex:pageBlockSectionItem>

    <apex:pageBlockSectionItem >

    <apex:outputLabel value="Document" for="document"/>

    <apex:selectList multiselect="true"  value="{!documentId}" id="document" size="4">

    <apex:selectOptions value="{!findDocument}"/>

    </apex:selectList>

    </apex:pageBlockSectionItem>

    </apex:pageBlockSection>

    </apex:pageBlock>

    </apex:form>

    </apex:page>

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos