Search code examples
salesforceapexapex-code

Messaging.SingleEmailMessage Limits in Salesforce


I am actually trying to send email through apex. So I got this link https://help.salesforce.com/s/articleView?id=000334302&type=1.

So I tried it in my dev org where the limit is 15. So I am using targetobjectObjectId as contact Id in Messaging.SingleEmailMessage. But after sending email. I could see that the count is reducing. Can someone help me here?

In the above link it says like below:
Each organization can send single emails to a maximum of 5,000 external email addresses per day. "External emails" refers to any email that's not associated to a Contact, Lead, or User. There’s no limit on sending single emails to contacts, leads, person accounts, and users in your org directly from account, contact, lead, opportunity, case, campaign, or custom object pages.

But why is my limit reducing in my Org? Please not that this piece of code I am trying in my Dev Org.

Below is my piece of code

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTargetObjectId(targetObjectId);//Contact Id
email.setWhatId(whatId);  Account Id
email.setTemplateId(templateId);//Email template Id
email.setCcAddresses(ccRecipientList);//List of Contact Ids
if(attach != null)
  email.setFileAttachments(attach); //Attachments if any

Solution

  • The answer lies in the documentation you quoted:

    directly from account, contact, lead, opportunity, case, campaign, or custom object pages

    You are not sending that email via standard tools in those record page. You're sending it via apex, so you should refer to apex documentation of SingleEmailMessage (emphasis mine)

    setCcAddresses(ccAddresses)
    Optional. A list of carbon copy (CC) addresses or object IDs of the contacts, leads, and users you’re sending the email to. The maximum size for this field is 4,000 bytes. The maximum total of toAddresses, ccAddresses, and bccAddresses per email is 150. All recipients in these three fields count against the limit for email sent using Apex or the API.

    Moreover governor limits doc states:

    If you use SingleEmailMessage to email your org’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.


    Increase email limit

    Mass Email limit can be temporary increased by Salesforce support. You could ask for a permanent increase, but they are rarely approved.

    The same considerations are valid for Single Email Message limit.
    They both are High Risk feature.

    In the case you must provide the following info:

    • Organization ID (Setup > Company Information)
    • The exact new limit you want for your org
    • The duration for which the increase is requested
    • A detailed and strong business case