+ Start a Discussion
Steve ThurstonSteve Thurston 

Emails in Sandbox not being delivered from Apex Class

I'm stumped as to why my emails aren't being delivered from my Sandbox when I use an Apex class.  Here is what I've checked:

(I have replaced the email from actual results with 'MY EMAIL 1' and 'MY EMAIL 2', so that's not bad email values in the code.)

1) Deliverability:  Set to All email

2) Bounce management:  Both checked and unchecked

3) Test Deliverability:  Recieved all 32 emails

4)  Single message:  I need to deliver a templated email with an attachment, but just to try and see if I can get anything to work, I've commented that out and just tried to send a single email.  I copy and pasted the code off the web in an effort to make sure I'm not overlooking anything.  Here it is, copy-and-pasted straight from my class (and then masked the email):

Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
    String[] sendingTo = new String[]{'MY EMAIL ADDRESS 1'};
    semail.setToAddresses(sendingTo);
    String[] sendingToBccAdd = new String[]{'MY EMAIL 2'};
    semail.setBccAddresses(sendingToBccAdd);
    String[] sendingTocAdd = new String[]{'MY EMAIL 1'};
    semail.setCcAddresses(sendingTocAdd);
    semail.setSubject('Single Email message Example');
    semail.setPlainTextBody('Hello!!!!!!!!!!This is a test email to test single email message program');
            
try {
    system.debug('Yapa - Sending a non-template email.');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
    }
catch (exception e) {
        system.debug('Yapa - Tried to send a non-template email and got an error:  ' + e);
    }    

5)  Debug logs:  system.debug statements and try... catch

I've thrown in all sorts of debug entries.  You can see in the single message email I'm sending that I am logging both success and failure.  When I look at debug logs, the "try" statement succeeds and I can see the email in the logs.

6)  Email logs:  Nothing.  The Deliverability test shows up in the log, but none of the many, many email tests I've tried.

For testing, I've got a button on my Lead object that calls a class to send the email.  Like I said, I've got it debug logged the !*(& out of, and I've traced it working all the way to the email send in the 'try...catch', and it's working, with all the correct values, right up to the send which gives the appearance of having worked.  But the emails never show up.

Here's a sample from the log:

15:00:07.0 (68224890)|VARIABLE_SCOPE_BEGIN|[176]|semail|Messaging.SingleEmailMessage|true|false
15:00:07.0 (68286222)|VARIABLE_ASSIGNMENT|[176]|semail|"common.api.soap.wsdl.SingleEmailMessage@76bb46ad"|0xac78023
15:00:07.0 (68293666)|STATEMENT_EXECUTE|[177]
15:00:07.0 (68303142)|HEAP_ALLOCATE|[177]|Bytes:4
15:00:07.0 (68324170)|HEAP_ALLOCATE|[177]|Bytes:29
15:00:07.0 (68332788)|VARIABLE_SCOPE_BEGIN|[177]|sendingTo|List<String>|true|false
15:00:07.0 (68351056)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
15:00:07.0 (68360976)|VARIABLE_ASSIGNMENT|[177]|sendingTo|["MY EMAIL 1"]|0x344d1916
15:00:07.0 (68366779)|STATEMENT_EXECUTE|[178]
15:00:07.0 (68380256)|STATEMENT_EXECUTE|[179]
15:00:07.0 (68386333)|HEAP_ALLOCATE|[179]|Bytes:4
15:00:07.0 (68401241)|HEAP_ALLOCATE|[179]|Bytes:25
15:00:07.0 (68407362)|VARIABLE_SCOPE_BEGIN|[179]|sendingToBccAdd|List<String>|true|false
15:00:07.0 (68421543)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
15:00:07.0 (68429426)|VARIABLE_ASSIGNMENT|[179]|sendingToBccAdd|["MY EMAIL 1"]|0x2e1ff2aa
15:00:07.0 (68435010)|STATEMENT_EXECUTE|[180]
15:00:07.0 (68446165)|STATEMENT_EXECUTE|[181]
15:00:07.0 (68451784)|HEAP_ALLOCATE|[181]|Bytes:4
15:00:07.0 (68467537)|VARIABLE_SCOPE_BEGIN|[181]|sendingTocAdd|List<String>|true|false
15:00:07.0 (68480803)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
15:00:07.0 (68488466)|VARIABLE_ASSIGNMENT|[181]|sendingTocAdd|["MY EMAIL 1"]|0x5624acf0
15:00:07.0 (68493667)|STATEMENT_EXECUTE|[182]
15:00:07.0 (68511652)|STATEMENT_EXECUTE|[183]
15:00:07.0 (68515434)|HEAP_ALLOCATE|[183]|Bytes:28
15:00:07.0 (68534615)|STATEMENT_EXECUTE|[184]
15:00:07.0 (68537295)|HEAP_ALLOCATE|[184]|Bytes:72
15:00:07.0 (68549852)|STATEMENT_EXECUTE|[186]
15:00:07.0 (68551392)|STATEMENT_EXECUTE|[186]
15:00:07.0 (68552661)|STATEMENT_EXECUTE|[187]
15:00:07.0 (68554725)|HEAP_ALLOCATE|[187]|Bytes:36
15:00:07.0 (68571269)|USER_DEBUG|[187]|DEBUG|Yapa - Sending a non-template email.
15:00:07.0 (68578657)|STATEMENT_EXECUTE|[188]
15:00:07.0 (68599370)|HEAP_ALLOCATE|[188]|Bytes:4
15:00:07.0 (68652773)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
15:00:07.0 (68674221)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
15:00:07.0 (111182217)|EMAIL_QUEUE|[188]|subject: Single Email message Example, bccSender: false, saveAsActivity: true, useSignature: true, toAddresses: [MY EMAIL 1], ccAddresses: [MY EMAIL 1], bccAddresses: [MY EMAIL 2], plainTextBody: Hello!!!!!!!!!!This is a test email to test single email message program, 
15:00:07.0 (111271089)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
15:00:07.0 (111306604)|METHOD_EXIT|[63]|01p0U00000058Uf|sendEmailWithAttachment.sendEmailWithPDFAttachment(Id, String)
15:00:07.0 (111320211)|STATEMENT_EXECUTE|[65]
15:00:07.0 (111326767)|HEAP_ALLOCATE|[65]|Bytes:1
15:00:07.0 (111398051)|HEAP_ALLOCATE|[65]|Bytes:18
15:00:07.0 (111413617)|HEAP_ALLOCATE|[65]|Bytes:19


7)  I don't see any of them in my Junk email box for either email address.

8)  If I send an email directly from the UI for a Lead (i.e. not via code), it successfully reaches the email address.

Any ideas what's going wrong?
jigarshahjigarshah
Steve,

You can check for the following.  
  • Navigate to Setup > Email  > Deliverability within your Salesforce instance and check if the Access Level dropdown shown in the snapshot below, is set to All email. If not, this restricts the sending of outgoing emails inspite of the code functioning as desired.
Salesforce Email Deliverability
  • Last but not the least, there is possibility of your emails being automatically routed to the Spam folder within the target email address hence it might be worthwhile to check the Spam folder.
Manj_SFDCManj_SFDC
Hi Steve,

please check if Email Relay Activation (Setup->Email Administration->Email Relay Activation​) is active
jigarshahjigarshah
Steve,

Are the emails working for you now?
sunny.sfdcsunny.sfdc
Any luck?

I am facing the same issue. Although debugs says email delivered successfully. But i didnt get any in my gmail box.
Keerthi GKeerthi G
I am facing the same issue. Is there any workaround? Receiving emails sent from catch block with email messaging but without catch, not seeing any Apex exception emails. 
Spencer BartzSpencer Bartz

I'm having the exact same problem in my Sandbox. Emails are not being sent from the Apex code I've deployed.
Deliverability:  All email
Test Deliverability:  Recieved 12 emails
Spam Folder: Empty
Exceptions / Errors: None
Single emails used: 3 out of 5000


I can even send mails using the Developer Console's "Execute Anonymous Window"
What gives?