2

I have below code I am using but somehow I neither catch any exception and in debug logs it shows success still I do not receive the email. I checked with deliveribility settings which is fine and set to all emails.

try{
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTargetObjectId(emailUserId);
            mail.saveAsActivity = false;
            mail.setSenderDisplayName('Salesforce');
            mail.setSubject('Ownership for account');
            mail.setPlainTextBody('test');
            System.debug('### sending email');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
    }
    catch(exception e){
        System.debug('cannot send email'+e);
    }
  • Are you doing this in a unit test, by chance? – sfdcfox Aug 28 '17 at 20:47
  • no this is in apex class not the unit test where email User Id is the id of the user – user5129806 Aug 28 '17 at 20:47
  • Unit tests are also written in Apex. I'm asking if you're trying this out in a unit test method (one that starts with @isTest or testMethod). – sfdcfox Aug 28 '17 at 20:48
  • You also mention email deliverability... did you verify the email address on the user object to make sure it is valid? – sfdcfox Aug 28 '17 at 20:49
  • Yes it is a valid address – user5129806 Aug 28 '17 at 20:50
0

Messaging.SendEmail returns an array of Messaging.SendEmailResult. You should use this return value to see if the emails were sent successfully and get any error messages. See here

Also note that if you are calling this method from inside a unit test then the email will not be sent.

  • result for email send is(Messaging.SendEmailResult[getErrors=();isSuccess=true;]) this is what I get which means it is sending the email still I am not getting it. – user5129806 Aug 28 '17 at 21:51
  • What if you set the recipient directly with the address you're trying to send to? mail.setToAddresses(new String[]{'email@test.com'); – Adam Tyler Aug 28 '17 at 22:01
  • If that doesn't work, what is the context of where this code is and how it's being called? – Adam Tyler Aug 28 '17 at 22:07
  • The code is being called on a button click from the UI. – user5129806 Aug 28 '17 at 22:16
0

it means that the email was sent from Salesforce and rejected by the recipient server. Messaging.SendEmailResult is not able to intercept/process these types of rejection.

Analysis steps:

Generate an email log (SETUP > EMAIL LOG FILES)

Verify the value in column Mail Event

Note failure messages in the email log

Resolution :

Execute the apex outbound email FROM a user with a different email address

E.g, log in as a user with email = @gmail.com instead of the failing domain

0

If you are trying to send an email using SingleEmailMessage from the constructor in a Apex class, the emails would not be sent. Follow this Salesforce Knowledge Article for a alternate solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.