[SalesForce] Apex Email Limits Clarification

I need to send a (potentially large) number of emails to (community) users via code using an Email Template, with a custom object as the whatId.

  • Because my whatId is a custom object, I am using SingleEmailMessage, not MassEmailMessage.
  • Because I am using an Email template, I am using setTargetObjectId(), not setToAddresses(), so I must construct one SingleEmailMessage per recipient.
  • Because I am emailing to internal users, I am setting my target object id to user id values to avoid limits (see next section).

I have found the following relevant passages in the Limits Quick Reference (spring 15):

Total number of sendEmail methods allowed: 10 (p. 30)

If you use SingleEmailMessage to email your organization’s internal users, specifying the user’s ID in setTargetObjectId means the email doesn’t count toward the daily limit. However, specifying internal users’ email addresses in setToAddresses means the email does count toward the limit. (p. 41)

You can send an unlimited amount of email to your organization’s internal users, which includes portal users (p. 42)

The other email limits I have found are related to Email Services (inbound email), or to emails to external email address, neither of which apply.

Messaging.sendEmail accepts a list<Messaging.Email> of messages to send – so is there a limit on how many SingleEmailMessage objects I can pass in that list? Is there a limit on number of emails per transaction? Some other limit I'm missing?

Let's say I need to send emails to 25,000 Community Users (extreme case, may be a possibility). Can I do this in a single call to Messaging.sendEmail()? I'm guessing I'd hit a heap limit building my list. Any way to tell how many I can send at once? I am planning to use a Batchable class, and was prepared to work in batches of 10, sending one email per sendEmail invocation (first limit above) before I realized that sendEmail accepts a list. What is a sensible batch size? If the list size is limited, is the per-transaction total limited? Batchable batches are limited to 2000 records, could I send one list of 2000? or 10 lists of 200?

Best Answer

Edit: Having been questioned about the behavior, I just tested this and found that you can send as many emails as you can handle within governor limits. As a proof of concept, I wrote the following code:

Messaging.SingleEmailMessage[] messages = new Messaging.SingleEmailMessage[0];
for(integer i = 0; i < 10000; i++) {
    messaging.SingleEmailMessage m = new messaging.SingleEmailMessage();
    m.settargetobjectid(userinfo.getUserId());
    m.setsubject('hello'+i);
    m.setplaintextbody('world'+i);
    m.setsaveasactivity(false);
    messages.add(m);

}
messaging.sendemail(messages);
system.assert(false);

This code did not fail with any errors about too many emails in the list, heap limits, CPU limits, etc. So, as long as you don't break any other governor limits, you can theoretically send 25,000 in a single call (but, you'd likely run in to governor limits without going asynchronous).


You can send 100 SingleEmailMessage per sendEmail call, and 10 sendEmail calls per transaction. So, you can send 1000 per transaction. There's no limit for sending to portal and internal users using setTargetObjectId.

All of this information is found in the Governor Limits document in the Email Limits section (the link goes there directly).

Related Topic