2

Is it advisable to use sendEmail() method inside a for loop? Does it perform any DML Operations that could probably hit my SOQL limits?

1 Answer 1

4

You get a maximum number of email invocations as well, and it is actually much lower, 10. So no, you should not call it in a loop if you can avoid it.

See Execution Governors and Limits:

Total number of sendEmail methods allowed: 10

See also Outbound Email:

Note the following:

  • The email is not sent until the Apex transaction is committed.
  • The email address of the user calling the sendEmail method is inserted in the From Address field of the email header. All email that is returned, bounced, or received out-of-office replies goes to the user calling the method.
  • Maximum of 10 sendEmail methods per transaction. Use the Limits methods to verify the number of sendEmail methods in a transaction.
  • Single email messages sent with the sendEmail method count against the sending organization's daily single email limit. When this limit is reached, calls to the sendEmail method using SingleEmailMessage are rejected, and the user receives a SINGLE_EMAIL_LIMIT_EXCEEDED error code. However, single emails sent through the application are allowed.
  • Mass email messages sent with the sendEmail method count against the sending organization's daily mass email limit. When this limit is reached, calls to the sendEmail method using MassEmailMessage are rejected, and the user receives a MASS_MAIL_LIMIT_EXCEEDED error code.
  • Any error returned in the SendEmailResult object indicates that no email was sent.

There used to be a limit on the number of messages you could send per invocation (100), so you would have to batch them into multiple sendEmail calls if you went over that number. It seems to have been lifted recently, however. See: Emails Per Invocation Still Capped at 100?

2
  • 1
    Thanks for the quick response :) I will be exploring MassEmailMessage option for my use case now.
    – Anisa
    Commented Aug 17, 2016 at 15:27
  • I've deleted my answer, since it was incorrect. You may want to simply state that you can send them all at the end now.
    – sfdcfox
    Commented Aug 17, 2016 at 16:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .