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
Alexander_EAlexander_E 

Add multiple Attachments to one email

Hi there,

 

I want to add several attachments to one email and sending this out.

I have no idea, where to add several attachments to the email in the code below. The two attachments are both pdf and less than 200KB.

 

If you can give me a hint, it would be great.

 

my first idea was to change

1.a Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();

to

1.b List<Messaging.Emailfileattachment> efa = new List<Messaging.Emailfileattachment>();

 

but than I am running to the issue not to be able to add the EmailFileAttachment to the (Messaging.SingleEmailMessage) email.

 

Here the code from the VisualForce Document http://www.salesforce.com/us/developer/docs/pages/Content/pages_email_sending_attachments.htm modified by me.

 

 

public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }

    private final Account account;

    // Create a constructor that populates the Account object 
    
    public sendEmail() {
        account = [SELECT Name, 
                  (SELECT Contact.Name, Contact.Email FROM Account.Contacts) 
                   FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount() {
        return account;
    }

    public PageReference send() {
        // Define the email 
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        // Reference the attachment page and pass in the account ID 
        Integer numAtts=[SELECT count() FROM attachment WHERE parentid=: account.Id];
        system.debug('Number of Attachments Atts = '+ numAtts);
        List<Attachment> allAttachment = new List<Attachment>();
    	allAttachment = [SELECT Id, Name, ContentType, Body FROM Attachment WHERE parentid =: account.Id];
        // Create the email attachment 
        //List<Messaging.Emailfileattachment> efa = new List<Messaging.Emailfileattachment>();
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
		// try{ 
		if(numAtts > 0){
				for (Integer i = 0; i < numAtts; i++){
					system.debug(allAttachment[i].Name);
					efa.setFileName(allAttachment[i].Name);
					efa.setBody(allAttachment[i].Body);
					efa.setContentType(allAttachment[i].ContentType);
				}
		}
		 // } catch (ListException  e){
			// |DEBUG|List index out of bounds: 2
		 //         system.debug( e.getMessage() );
		// } 

        String addresses;
        if (account.Contacts[0].Email != null) {
            addresses = account.Contacts[0].Email;
            // Loop through the whole list of contacts and their emails 
    
            for (Integer i = 1; i < account.Contacts.size(); i++) {
                if (account.Contacts[i].Email != null) {
                    addresses += ':' + account.Contacts[i].Email;
                }
            }
        }

        String[] toAddresses = addresses.split(':', 0);

        // Sets the paramaters of the email 
    
        email.setSubject( subject );
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( body );
        //email.setFileAttachments(new Messaging.EmailFileAttachment[] {List<efa>()});
        if(numAtts > 0){
        	email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        }

        // Sends the email 
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
        return null;
    }
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Mike@COLMike@COL

Try it like this: (changed lines marked with *****)

public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }

    private final Account account;

    // Create a constructor that populates the Account object 
    
    public sendEmail() {
        account = [SELECT Name, 
                  (SELECT Contact.Name, Contact.Email FROM Account.Contacts) 
                   FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount() {
        return account;
    }

    public PageReference send() {
        // Define the email 
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        // Reference the attachment page and pass in the account ID 
        Integer numAtts=[SELECT count() FROM attachment WHERE parentid=: account.Id];
        system.debug('Number of Attachments Atts = '+ numAtts);
        List<Attachment> allAttachment = new List<Attachment>();
    	allAttachment = [SELECT Id, Name, ContentType, Body FROM Attachment WHERE parentid =: account.Id];
        // Create the email attachment 
*****        List<Messaging.Emailfileattachment> efaList = new List<Messaging.Emailfileattachment>();
        
		// try{ 
		if(numAtts > 0){
				for (Integer i = 0; i < numAtts; i++){
					system.debug(allAttachment[i].Name);
*****Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(allAttachment[i].Name); efa.setBody(allAttachment[i].Body); efa.setContentType(allAttachment[i].ContentType); *****efaList.add( efa ); } } // } catch (ListException e){ // |DEBUG|List index out of bounds: 2 // system.debug( e.getMessage() ); // } String addresses; if (account.Contacts[0].Email != null) { addresses = account.Contacts[0].Email; // Loop through the whole list of contacts and their emails for (Integer i = 1; i < account.Contacts.size(); i++) { if (account.Contacts[i].Email != null) { addresses += ':' + account.Contacts[i].Email; } } } String[] toAddresses = addresses.split(':', 0); // Sets the paramaters of the email email.setSubject( subject ); email.setToAddresses( toAddresses ); email.setPlainTextBody( body ); //email.setFileAttachments(new Messaging.EmailFileAttachment[] {List<efa>()}); if(numAtts > 0){ ****** email.setFileAttachments(efaList ); } // Sends the email Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); return null; } }

 

 

 

All Answers

Mike@COLMike@COL

Try it like this: (changed lines marked with *****)

public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }

    private final Account account;

    // Create a constructor that populates the Account object 
    
    public sendEmail() {
        account = [SELECT Name, 
                  (SELECT Contact.Name, Contact.Email FROM Account.Contacts) 
                   FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount() {
        return account;
    }

    public PageReference send() {
        // Define the email 
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
        // Reference the attachment page and pass in the account ID 
        Integer numAtts=[SELECT count() FROM attachment WHERE parentid=: account.Id];
        system.debug('Number of Attachments Atts = '+ numAtts);
        List<Attachment> allAttachment = new List<Attachment>();
    	allAttachment = [SELECT Id, Name, ContentType, Body FROM Attachment WHERE parentid =: account.Id];
        // Create the email attachment 
*****        List<Messaging.Emailfileattachment> efaList = new List<Messaging.Emailfileattachment>();
        
		// try{ 
		if(numAtts > 0){
				for (Integer i = 0; i < numAtts; i++){
					system.debug(allAttachment[i].Name);
*****Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(allAttachment[i].Name); efa.setBody(allAttachment[i].Body); efa.setContentType(allAttachment[i].ContentType); *****efaList.add( efa ); } } // } catch (ListException e){ // |DEBUG|List index out of bounds: 2 // system.debug( e.getMessage() ); // } String addresses; if (account.Contacts[0].Email != null) { addresses = account.Contacts[0].Email; // Loop through the whole list of contacts and their emails for (Integer i = 1; i < account.Contacts.size(); i++) { if (account.Contacts[i].Email != null) { addresses += ':' + account.Contacts[i].Email; } } } String[] toAddresses = addresses.split(':', 0); // Sets the paramaters of the email email.setSubject( subject ); email.setToAddresses( toAddresses ); email.setPlainTextBody( body ); //email.setFileAttachments(new Messaging.EmailFileAttachment[] {List<efa>()}); if(numAtts > 0){ ****** email.setFileAttachments(efaList ); } // Sends the email Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); return null; } }

 

 

 

This was selected as the best answer
Alexander_EAlexander_E

You made my day!!

Thank you very much!

 

:-D