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
Mr. KarateMr. Karate 

Send Email with apex api Messaging.sendEmail not working.

No emails are being sent in our sandbox with Apex API.
We have Email Administartion Deliverablility Access level set to All email.
We are able to get emails with Test Deliverability.
The sendEmailResult.success is  true.
The only thing we haven't tried to fix this is delete the sandbox and create a new one.
I've been trying sending to multiple email account: gmail, mailinator, etc...
Any help is greatly appreciated.

Our testing Apex Code:

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'test@mail.com'});
mail.setSubject('test');
mail.setPlainTextBody('test plain');
 mail.setHtmlBody('test heml');
List<Messaging.sendEmailResult> results = Messaging.sendEmail(new Messaging.singleEmailMessage[] {mail});
Best Answer chosen by Mr. Karate
Mr. KarateMr. Karate
Resolved.  Apex Message.sendEmail must be only used in an action with a form submit.  It will not work with an AJAX call.

All Answers

Sonam_SFDCSonam_SFDC
Did you happen to check the email logs for the time when you tested this email sending functionality: https://help.salesforce.com/HTViewHelpDoc?id=email_logs.htm&language=en_US
Please check to see if the mails were sent out from salesforce and were they delivered or not: https://help.salesforce.com/HTViewHelpDoc?id=email_logs_format.htm&language=en_US
Mr. KarateMr. Karate
Update:

I just requested an email log.  Thank you for the advice.

We are able to get the Apex API code that I posted to send email if we run the code in an Execute Anonymous Window in the Developer Console.

However, if we run the code in an Apex Controller for a visualsource page as the same administrator user, it fails.
Mr. KarateMr. Karate
Resolved.  Apex Message.sendEmail must be only used in an action with a form submit.  It will not work with an AJAX call.
This was selected as the best answer