Why am I not receiving Apex emails in sandbox?

Question:
I am trying to send a simple email from the anonymous window in my Salesforce sandbox, but I am not receiving any emails in my mailbox. The execution log shows email invocation 1, but nothing reaches the inbox. The same code works fine in production, and I receive emails there without issues.
Here is the code I am using:
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] {'someone@gmail.com'});
email.setSubject('Test Subject');
email.setPlainTextBody('Hi, This is Test Body');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });I have already checked that Email Deliverability is set to “All Email” and Email Relay is marked as “Preferred.” Why am I not receiving the email in my sandbox?
Answer:
In Salesforce sandboxes, email behavior is different from production. Emails are often suppressed or redirected for security and testing purposes. By default, sandbox emails are sent only to authorized addresses defined under Deliverability settings and Organization-Wide Email Addresses.
Boost your career with CRS Info Solutions’ Salesforce online training, offering expert guidance and practical experience. Master the Salesforce ecosystem through comprehensive, hands-on learning.
One reason you are not receiving the email is that Salesforce automatically masks or redirects outgoing emails in sandbox environments to prevent accidental messages being sent to real users. Depending on the sandbox type, emails may either be blocked or sent only to addresses listed as test emails.
To resolve this, you should check the following:
You should first make sure that Deliverability is set to “All Email”, which you’ve already confirmed. Next, verify if Salesforce is redirecting the email to your sandbox user email address instead of the external Gmail address, as sandboxes sometimes override recipient addresses. It’s also a good idea to check the Email Logs in Setup to see whether the email was attempted and where it was delivered.
Additionally, try sending the email to your Salesforce user email address instead of Gmail to confirm whether delivery works. If your organization is using an Email Relay, test without it to determine whether the relay is blocking sandbox emails. Keep in mind that in sandbox environments, external emails are not always guaranteed. For end-to-end testing of outbound emails, you may need to use a developer org or production instead.
In short, the issue is expected behavior in sandboxes. You can confirm email sending through logs, but emails to external addresses may not always reach your Gmail account from a sandbox.
Summing Up
In Salesforce sandboxes, emails often behave differently than in production, with delivery sometimes redirected, blocked, or suppressed for security reasons. Even if Deliverability is set to “All Email,” messages may not reach external addresses like Gmail, and testing should be done using your Salesforce user email or verified logs. For reliable external email testing, a developer org or production environment is the best option.
Master Salesforce with Expert Training in Hyderabad
Kickstart your career with our comprehensive Salesforce training in Hyderabad. Whether you’re new to Salesforce or looking to enhance your skills, our program offers specialized tracks in Salesforce Administration, Development, and Artificial Intelligence. We equip you with the knowledge and hands-on experience necessary to excel in the tech industry.
Our training program includes interactive modules, real-world projects, and expert-led sessions to ensure you’re fully prepared for certification exams and interviews. Our experienced instructors provide personalized attention, making even complex concepts easy to understand.
Start your journey to success today! Enroll for the free demo and set yourself on the path to a rewarding future!!!

