+ Start a Discussion

How to add attachments to an outbound email with apex class

I have a custom object called Email Drafts and I have an apex class that will send an outbound email out to specific contacts based on information on the email drafts object.


I have two questions I need help on:


1.)  If there is an attachment on the email drafts object (notes & attachments section), is there any way to specify in my apex class that the email needs to include that attachment as well?  The only documentation I can find about attachments is about creating a pdf from a visualforce page and attaching.  I need images and documents already in SFDC to be attached to my email.


2.) I need to modify this code to redirect the user to another visualforce page after the email is sent and update a status field to Email Sent.  I cannot figure out how to do this.  I keep getting error messages when I add a pagereference to the class.


Here is my class and the error message I've been getting.  I would really appreciate any help you can give me on it.


public class EmailDraft
private Email_Draft__c ed;
public EmailDraft(ApexPages.StandardController controller)

public void SendEmail()
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {ed.send_to_contact__r.email};
IF(ed.introduction_contact__r.email != null) {
if(ed.send_cc_contact__r.email != null){
String[] ccAddresses = new String[] {ed.send_cc_contact__r.email};

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

     update ed;



Error: Compile Error: Void method must not return a value at line 33 column 1 



Ritesh AswaneyRitesh Aswaney

If you're trying to return a PageReference from this method

public void SendEmail()


Then it ought to be


public PageReference SendEmail()


The method declaration needs to specify the return type. Void implies no return type, hence you get the exception.


Also, here's an example on how to attach an existing document rather than creating a pdf




Thank you!  That solved my redirect issue perfectly.  Guess I needed a second pair of eyes  :)


I will see if I can't get the attachments to work from the link you sent me. 


Thanks again!