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
BrandiTBrandiT 

Add Attachments to Outgoing Email Class

I have a custom object called Email_Draft__c which is meant to replace SFDC standard email functionality.  It is just about completed, but I cannot figure out how to add attachments. 

 

I have an apex class which takes information from this custom object and sends an email, but I need to edit the class to pull in files that might be in the Notes & Attachments section of my custom object.  I also have a related object called VPL__c.  If there are files attached to that related object, I need those to be attached to the email as well. 

 

I've tried several options with no luck.  I would really appreciate any help you can give me with this.

 

I'm sure I need to create a new list of attachments and add the Email_Draft__c & VPL__c attachments to that list, but I can't even seem to get that to work.

 

Here is the class I'm using:

 

public class EmailDraft
{
private Email_Draft__c ed;
public EmailDraft(ApexPages.StandardController controller)
{
this.ed=(Email_Draft__c)controller.getRecord();
}

public PageReference SendEmail()
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

if(ed.introduction_contact__r.email != null){
String[] toAddresses = new String[] {ed.introduction_contact__r.email};
                  mail.setToAddresses(toAddresses);   
                  }

 

if(ed.send_cc_contact__r.email != null){
String[] ccAddresses = new String[] {ed.send_cc_contact__r.email};
                  mail.setCcAddresses(CcAddresses);   
                  }

if(ed.send_bcc_contact__r.email != null){
String[] bccAddresses = new String[] {ed.send_bcc_contact__r.email};
                  mail.setbCcAddresses(bCcAddresses);   
                  }


    
mail.setOrgWideEmailAddressId('0D2F0000000CbiO');
mail.setWhatId(ed.id);
mail.setTemplateId('00XF0000001KOXA');
mail.settargetobjectid(ed.send_to_contact__r.id);

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

ed.approval_status__c = 'Approved & Sent';
update ed;

     PageReference next = new PageReference('https://na10.salesforce.com/a0s?fcf=00BF0000006XuMw');
          next.setRedirect(true);
 
     return next;

}
}

osamanosaman

  List<Messaging.EmailFileAttachment> attach1 = new List<Messaging.EmailFileAttachment>();

 

List<Document> doc = [Select id, DeveloperName, ContentType, type, Body from Document where Name = 'Document Name]; 

 for(Document dc: doc)         

  {               

Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment(); 

attach.setContentType(dc.contentType);               

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

attach.setInline(false);               

attach.Body = dc.Body;               

attach1.add(attach);           

}  

 

In the SignleEmailMessage object set this:

 

mail.setFileAttachments(attach1);

.