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
MiddhaMiddha 

SingleEmailMessage not working

I am using SingleEmailMessage to send emails to Salesforce users by setting setTargetId.

As per Salesforce document "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."

For some reason, this stops working after few emails. I do receiece some emails but then it stops and starts working again next day, Seems like it is consuming some daily limit which is reset next day.

On checking logs, it still shows "Success" and there is not error: Messaging.SendEmailResult[getErrors=();isSuccess=true;])

Any solution to this?
srlawr uksrlawr uk
Is it possible either of these points are getting in your way?

Most likely, I suspect you are working in a developer edition which has a limit of 10:
(as per the govenor limits page here https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm)

"
In Developer Edition organizations and organizations evaluating Salesforce during a trial period, your organization can send mass email to no more than 10 external email addresses per day. This lower limit does not apply if your organization was created before the Winter '12 release and already had mass email enabled with a higher limit. Additionally, your organization can send single emails to a maximum of 15 email addresses per day.
"

There is serious cap on sending out external messages on developer editions, for obvious (spam) reasons.

I would also point out that I am pretty sure that the SingleEmailMessage interface can only be invoked 10 times in a single context, so if you have one complicated request (from any source, such as batch apex, data loading, triggers etc.etc.) that could be calling the SingleEmailMessage more than 10 times (for example in a loop) you might find it dies on you.
MiddhaMiddha
Few points to notice:
  1. I am using a dev Org
  2. Not sending email to any outside Id and not using setToAddresse() method as well.
  3. Only setting user Id in SetTargetId() method which doesnt counts against governor limits, as per the document.
  4. I dont get any error message, it shows succes, just that i stop getting emails.
MiddhaMiddha
Few points to notice:
  1. I am using a dev Org
  2. Not sending email to any outside Id and not using setToAddresse() method as well.
  3. Only setting user Id in SetTargetId() method which doesnt counts against governor limits, as per the document.
  4. I dont get any error message, it shows succes, just that i stop getting emails.