+ Start a Discussion

Email attachments causes Apex heap size exception

I'm sending an email with attachments using SingleEmailMessage. An EmailFileAttachment[] List is created to contain the attachments which are within the specified 10mb limit for all attachments but when the attachments exceed 2mb in size the Apex heap size limit is exceeded.


My question simply is this: how are files, of up to 10mb, attached to an email using the SingleEmailMessage class without getting 'System.Exception: Apex heap size too large'?



// Create EmailMessage and assign parameters
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
mail.setSubject('New Case Created : ' + case.Id);
mail.setPlainTextBody('Your Case: ' + case.Id +' has been created');

// Set email file attachments
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for (Attachment a : [select Name, Body, BodyLength 
                     from Attachment
                     where ParentId = :case.Id])
  // Add to attachment file list
  Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();

// Send email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail })


I came up against this issue a couple of months back, posted it, but got no response. So apologies, I'm posting again in the hope that I can finally find a solution.


Would appreciate any responses on this one, even if it's half a guess. Thanks in advance.

Kirk F.ax361Kirk F.ax361

I don't have a specific answer, but maybe as you iterate through your collection of attachments, add up the combined size of all the attachments?  So in your try/catch, you can report the attachment size which caused the failure.  I suppose in your catch you'd have an e-mail going to your admin, or some such alerting.


If, after a few problems, you confirm your hunch as to the size threshold (say, whenever you have attachments with total size over 2MB), then you can deal with it.  For example, attach as many files as you can to the e-mail, and then send the remaining ones in a second e-mail.  Of course, if one of those attachments is over the threshold, then you're stuck here.  ;-)  Time to gather up these attachments into a ContentDelivery object, and send the customer a link to that, instead of the files themselves.  (See the Content object for more info.)


I'm probably going to bump into the same problem with code I'm releasing, so if I have suggestions I'll post them.


I've just heard back from Salesforce. This is a known issue within Salesforce. There is currently no workaround, other than to keep the size of the attachment(s) lower than 2MB.

The R&D team is currently working to resolve this issue.

Kumar SauravKumar Saurav
Hi nelloC,

There is a limit from salesforce side for sending large size attachement.Keep the size of the attachments lower than 2mb. You can overcome this limit by using 3rd party storage server for storing the file content as well as sending them over the email with large size attachment.
Cloud Drop is One such App that is some what following the same logic in order to store any number and size of files that can be associated with any salesforce object with Rackspace server.It also allow user to send very large size file as email attachment to particular contact/lead record.
Please check the following link for more information: