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
Manu@devManu@dev 

SINGLE_EMAIL_LIMIT_EXCEEDED, Failed to send email: []

Hi all,

I am trying to send an email using single email message and I am unable to overcome the SINGLE_EMAIL_LIMIT_EXCEEDED exception.  I am using html email template and I am using the mail.setTargetObjectId(con.Id) as contact Id. When I am sending an email to contact, is it still considered as external email?

By the search I cam to know that email to User, Contact and Lead is considered as salesforce internal emails and linitation will not applicable for this. Please correct me if this is wrong.

I think this exception will not come, if the email is considered as salesforce internal email?

Please help me to overcome this exception. Below is the code I have used.

public PageReference sendEmail() {
    Customer__c customer;
    customer = [ SELECT Id, Email__c, Name, Salesforce_Org_Id__c FROM Customer__c WHERE Id =: 'a009000000q4Rek'];
    contact con = new contact();
    con.email = 'manu.tej@ceptes.com';
    con.LastName = 'Manu';
    insert con;
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            EmailTemplate et =[SELECT Id, HtmlValue, DeveloperName FROM EmailTemplate WHERE DeveloperName = 'Order_Delivery_Email' ];
            String subject = 'Order Reference Number : ORD - 010' ;
            String emailBody = ((String)et.HtmlValue).replace('**Customer Name**',customerName ).replace('**Order Reference Number**',customerName );
            String [] toAddresses = new String [] {emailAddress};
            mail.setTargetObjectId(con.Id);
            mail.setSubject(subject);
            mail.setSaveAsActivity(false);
            mail.setHtmlBody(emailBody);
            mail.setSenderDisplayName('Support');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    return null;
    }

Thanks in advance.

R - Manu
Vinit_KumarVinit_Kumar
To answer to your question,

Yes,sending an email to contact would be considered as external email and the limit for the same is 1000 emails in a day per org.

As per salesforce docs,

'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 addresseses in setToAddresses means the email does count toward the limit.'

That means only User Ids are exempted when sending emails using emails through setTargetObjectId ,contact and lead ids would be considered as external emails.

Go through the below link to elarn more :-

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

If this helps,please mark it as best answer to help others.
Karishma K 2Karishma K 2
@Vinit is right. There is this one article I came across while facing a similar situation. http://sanketsarang.blogspot.in/2013/05/salesforce-single-email-limit-exceeded.html. It does integrate beautifully for sending out emails without limits. You need to ensure your code is as per SFDC guidelines or integrate with a 3rd party tool to resolve your issue.

Hope it helps you Manu!