Loading

'Too many API email invocations 11' error when sending emails via Apex

Дата публикации: Jun 17, 2026
Описание
There is no email limit from Apex. The limit is on the number of times the sendEmail() method can be invoked from Apex.  We will not be having a limit for Emails from Apex, however, we will be having the limit for number of times the sendEmail() method can be invoked from Apex in a transaction. 

 

Best practice is to ensure that you're not calling the sendEmail() method inside a for loop

Решение

How to Avoid the 'Too Many API Email Invocations' Error

To stay within the 11-invocation limit per transaction, collect all Messaging.SingleEmailMessage objects into a List and invoke Messaging.sendEmail() once with the entire list, outside any loop.

The correct pattern is:

  1. Create an empty List to hold email messages.
  2. Loop through the set of recipients or records requiring emails.
  3. Inside the loop, construct each Messaging.SingleEmailMessage object (setting subject, body, recipient, etc.) using a helper method.
  4. Add each message to the list inside the loop.
  5. After the loop completes, call Messaging.sendEmail(listOfMessages) once with the full list.

This approach ensures that no matter how many emails are being sent, only a single sendEmail() invocation is used per transaction.


Note: The code provided in this article is an example only. Always test in a sandbox before deploying to production.

//method to send mail to a list of users
public static void DispatchEmail() {
    List lstMails = new List();
      lst = 20;
       for(Integer i : lst) {
          Messaging.SingleEmailMessage message = getEmail();       
 //Construct the email message here by populating the values such as body, subject
                //Adding emails to the list       
 lstMails.add(message);
    }  
   //Sending the email list in a single call.
    Messaging.sendEmail(lstMails);
  }

 
public static Messaging.SingleEmailMessage getEmail() {
    Messaging.SingleEmailMessage mailMessage = new Messaging.SingleEmailMessage();
    mailMessage.setSaveAsActivity(false);
    return mailMessage;
  }
Номер статьи базы знаний

000385111

 
Загрузка
Salesforce Help | Article