function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AbAb 

Attaching attachment to the email

Hello,

I want to implement the fucntionality of attaching document to a email.
As of now i am able to send email.

like below 
      Messaging.reserveSingleEmailCapacity(2);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'test@google.com'};
            String[] ccAddresses = new String[] {'test@google.com'};
                mail.setToAddresses(toAddresses);
        mail.setCcAddresses(ccAddresses);
        mail.setReplyTo('test@google.com');
        mail.setSenderDisplayName('Support Team');
        mail.setSubject('test Subject');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setHtmlBody('<html><body><p>Hello Text</p></body></html>');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
I want to improve it to let user choose file in "Computer" or "Salesforce document", attach the file and send it with email.

Thank you for sugetions !
 
Best Answer chosen by Ab
Veenesh VikramVeenesh Vikram
I guess this post will help you:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_sending_attachments.htm

In VF page, create an <apex:inputFile>, bind it to a blob variable in controller and then send it via email as shown in the post.

Hope this helps!
Veenesh