• Ashley Underwood
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
I have created a Visualforce page and Apex Class to be able to send an email from an Opportunity with Attachments that are in the Notes & Attachments section. The page allows them to pick an attachment to send with the email. The email will send but does not include the attachment. I know I am missing something, any help would be appriciated.

Apex Class
public class email_class{

    public email_class(ApexPages.StandardController controller) {

    }
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string opportunityId {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
       
    Public PageReference send(){
    
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
            mail.setToAddresses(new String[] {emailTo}); 
            mail.setCcAddresses(new String[] {emailCC});   //set the cc address
            mail.setSubject(subject);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody(email_body);
            mail.setWhatId(opportunityId);// Set email file attachments 
            
       List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

            for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :opportunityId]){  // Add to attachment file list  
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
                efa.setFileName(a.Name); 
                efa.setBody(a.Body); 
                fileAttachments.add(efa);}
                mail.setFileAttachments(fileAttachments);// Send email
                
                
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
        }
}



VF page
<apex:page standardController="Opportunity" extensions="email_class">
    <apex:form >
           
        <apex:pageBlock title="Email Details">
            <b>To: </b> <apex:inputText value="{!emailTo}"/><p/>
            <b>CC: </b> <apex:inputText value="{!emailCC}" /><p/>  
            <b>Enter Subject: </b> <apex:inputText value="{!subject}" maxlength="200"/><p/>            
            <b>Enter Body: </b> <apex:inputTextArea value="{!email_body}" rows="10" cols="100"/><p/>
          
        <apex:pageBlock title="Attachments">
        <apex:pageBlockTable value="{!opportunity.Attachments}" var="Att">
            <apex:column headerValue="Select">
            <apex:inputCheckbox value="{!opportunity.Send_Email_with_Quote__c}"/>
            </apex:column>
       <apex:column value="{!Att.createddate}"/>
       <apex:column value="{!Att.name}"/>
       <apex:column value="{!Att.description}"/>
       </apex:pageBlockTable><p/>
       </apex:pageblock>
                
       <apex:commandButton value="Send Email" action="{!send}"/>
       <apex:commandButton value="Canel" action="{!cancel}"/>
            
       </apex:pageBlock>
                           
    </apex:form>    
</apex:page>