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
Neoxia 7Neoxia 7 

SendEmail not working in Sandbox

Hi,

In a sandbox, I would like to send an email to some users when a particular action is done (i.e. in a trigger on a custom object).

First of all, I configured the access level to "All Emails" in Email Administration > Deliverability.
Then, I use the classic Messaging class/namespace in my trigger :
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//Test
toAddresses = new List<String> {'some.email@test.com'};

mail.setToAddresses(toAddresses);
mail.setReplyTo('no-reply@test.com');
mail.setSubject('subject');
mail.setPlainTextBody('Test message... Again.');
mail.setHtmlBody('Test message... <br/> <i>Again</i>.');

Messaging.SendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }, false);
System.debug('Email invocations : ' + Limits.getEmailInvocations());
	
//errors
List<Messaging.SendEmailError> errors = new List<Messaging.SendEmailError>();
for(Messaging.SendEmailResult currentResult : results) {
	System.debug('Success : ' + currentResult.isSuccess());
}
In my debug logs, I clearly read "Success : true" for each mail this function has to send. But in my mailbox, nothing to read.
I also logged the number of sent emails with "Limits.getEmailInvocations()". It always increases...

In some forums, I read that it could be a problem with the sandbox (https://developer.salesforce.com/forums/ForumsMain?id=906F000000091ECIAY). Any idea or experience here ?

Thanks !
Evan KennedyEvan Kennedy
There is a global email setting in an organization that can prevent emails from going out. Under "Email Administration -> Deliverability"
Scott HungScott Hung
Absolutely right.  Sandboxes have this setting disabled (except for system emails).  See this article for more information under Email Deliverability:  https://help.salesforce.com/HTViewHelpDoc?id=data_sandbox_implementation_tips.htm&language=en_US
Neoxia 7Neoxia 7
Do you mean this setting (Access level) can't be modified for sandboxes ? Because this is the first thing I changed in my sandbox (see the first post, first action).
It it can't be modified, how to test mail sending before going to production env ?