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
Terence VibanTerence Viban 

Send Single Email in Apex

Hi all,
I am sending a single email from Apex code. Although I do not have any erros, the email doesn't get sent. As you can see from the code snippet, it is pretty uncomplicated. Wanted to do a test send before I get to the complicated stuff. There are not error messages but the email doesn't get sent. Any ideas???

Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();
mail.setToAddresses(new String[] {''});
mail.setSenderDisplayName('Private Budget App');
mail.setSubject('Private Budget records for ' + + ' ' +;
mail.setPlainTextBody('This is a test');
system.debug('@@@@ sendEmail - mail : ' + mail);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

The sytem debug produces the following
@@@@ sendEmail - mail : Messaging.SingleEmailMessage[getBccAddresses=null;getCcAddresses=null;getCharset=null;getDocumentAttachments=null;getFileAttachments=null;getHtmlBody=null;getInReplyTo=null;getOrgWideEmailAddressId=null;getPlainTextBody=This is a test;getReferences=null;getTargetObjectId=null;getTemplateId=null;getToAddresses=(;getWhatId=null;isUserMail=false;]

I am working on the dev org and executing the class which sends the mail from execute anonymous. 
Vadim RudkovVadim Rudkov
Try to check the deliverability settings: Setup -> Administer -> Email Administration -> Deliverability
Access level should be 'All email'.
Terence VibanTerence Viban
That is the first thing I checked and it is set to "All Email". So the problem is apparently not here. Thanks though for your suggestion
Grazitti TeamGrazitti Team
Hii Terence Viban,
Yord Code is working Fine for me, so tere is an issue of Deliveribility Settings,
Go to: Setup -> Administer -> Email Administration -> Deliverability   and set the Access level to "All email".

let us know if you have any question .
please don't Forget to Mark this as your best answer if it works fine for you

Grazitti Team
Terence VibanTerence Viban
Vadim above already suggested this possibility and irrespective of that I already double checked this before posting. The code really seems to work fine as there are no errors but the mail isn't sent.
How did you try this on your org? did you use execute anonymous as well? could this be the problem?. At this point am just guessing because I do not see why the email should not be sent. I have tried two different valid email addresses. 
Thanks for your contribution
Chinmay Menon 7Chinmay Menon 7
Hi Terence, 

Were you able to solve this issue? I am facing the same issue. The code works from Execute Anonymous but not from apex.
Ihor HorchynslyiIhor Horchynslyi
Hi! Same issue. 
Hi all, facing the same issue.
Hi Deepika,

The last time I ran into this problem, the reason was that the code was executed by the Automated Process user. This user doesn't have any email assigned, so I had to set it explicitly using Org-Wide Email Address:
I also have this problem.
It works in Exec Anon but not apex controller!
I have fiddled with all the settings I can see.
I have a resolution.
Messaging.sendEmail() doesn't work from controller level Apex.
Messaging.sendEmail() DOES work from trigger level Apex. (which runs at the same level as Exec Anon)
Use a trigger to send email! Templates using custom objects all work, everything!