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
Ben DunlapBen Dunlap 

Troubleshooting outbound email beyond the obvious

I'm trying to use Messaging.sendEmail() in my sandbox and all indications are that it is working:

 

  • Messaging.SendEmailResult.isSuccess() returns TRUE
  • Messaging.SendEmailResult.getErrors() returns an empty list
  • Before/after calls to Limits.getEmailInvocations() indicate that one invocation is being consumed
  • Logs in the Developer Console show what I would expect to see -- the sendEmail() call followed by the body of the message

But the emails just aren't getting to their destination. I have another bit of code that has been around for a while and is quite similar, and it's working just fine -- I test it and an email arrives in my Inbox almost instantly.

 

What can I do to troubleshoot this further? Also, just to fill in some details, the basic architecture of the process I'm trying to automate here is as follows:

 

  • VisualForce email template called E
  • VisualForce page called P
  • code that invokes Messaging.sendEmail() is in the controller associated with P (in the constructor method)

When page P is loaded, the controller's constructor executes and sends an email using the email template E to a test Contact record. At first I was trying to attach a PDF generated by yet another VisualForce page but I commented out this functionality just to see if simplifying things would eliminate the problem. Still no emails in my Inbox.

 

I've also added a call to setBccAddresses() and passed it two additional email addresses of mine, on different domains and hosted by different services from the email address associated with the test Contact -- and the emails don't arrive in those mailboxes, either. All three email vendors provide very straightforward spam control so I have no reason to suspect that the emails are getting blocked -- it just seems like, somehow or other, Salesforce is failing to send these emails /after/ they leave the purview of Apex.

 

What can I do to troubleshoot this problem further?

Ben DunlapBen Dunlap

Tried deploying the code to Production on the off-chance that something was wrong with my sandbox. No luck there, either. Same "symptoms": Everything indicates that the email has been sent, except that it never arrives at any of the three mailboxes it was sent to.

Mr. KarateMr. Karate
I'm having the same problem.
DhopmenDhopmen
@Ben Dunlap & @Mr. Karate - did you ever manage to resolve this?  We're having the same issue.
DhopmenDhopmen
Problem solved - APEX test classes cannot send email.  This is documented in the information about unit tests, but is not documented in the pages relating to messaging/email etc.
Ben DunlapBen Dunlap
I'm the OP (although I might have used a different login back then) -- the problem I was seeing was in regular code, not test classes.
Ben DunlapBen Dunlap
And @Dhopmen, no I didn't but I gave up on trying quite some time ago. :-/
bhoopesh chauhan 6bhoopesh chauhan 6
Hi Ben,

were you adding an error to the records using addError.
If so then that would cause the entire transaction to roll back and along with it the queued emails.

you can refer:
https://developer.salesforce.com/forums/?id=906F000000093LGIAY