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

Apex heap size error when sending email with large attachments

I'm attempting to send an email with a large attachment (or attachments) using SingleEmailMessage. A EmailFileAttachment[] List is created to contain the attachments which are within the 10mb limit for all attachments but when the attachment(s) exceed 2mb in size the Apex heap size limit is exceeded. There's obviously something I'm missing here. With this in mind, how are files, of up to 10mb collectively, attached to an email using the setFileAttachments method without exceeding the Apex heap size limit?


There's no problem uploading a file of up to 5mb in size to the Attachments object for example, that doesn't cause a Apex heap size error. So I assume blobs don't count against heap size. But that doesn't seem to be true for a collection of objects that contain a blob. Surely the blob is only held by reference?


The code below doesn't enforce the 10mb attachments limit but assume it's not exceeded. When fileAttachments exceeds 2mb the 'System.Exception: Apex heap size too large' occurs.


// 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 })

Message Edited by nelloC on 02-16-2010 03:35 AM
Would dearly like to resolve this issue. Anyone out there with any ideas? All and any comments gratefully received...

I've 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 lower than 2MB.


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




I am doing exactly same... but getting an error


that Variable does not exist: Name in line  efa.setFileName(a.Name);


Kumar SauravKumar Saurav
Hi nelloC,

There is a limit from salesforce side for sending large size attachement.Keep the size of the attachments lower. Email services reject email messages and notify the sender if the email (combined body text, body HTML, and attachments) exceeds approximately 10 MB (varies depending on language and character set). 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: