Question

I wrote code to send email. It works fine but my goal is:

When someone sent to non-existing email address, I want to log the result as 'false' or 'failure' etc (and when email address is valid, just say 'success')

I've tried 2 things with the code below.

  1. provided non-email string 'foo@!'

  2. provided non-existing email address 'thisdoesnotexistignsdfkjsdf@gmail.com'

result:

Execute case 1 caused code to go into catch block thus outputting error message on the html page which is expected.

Execute case 2 caused code to return 'ok sent!'

And after few minutes I received email that delivery failed.

My guess is SendEmailResult object's isSuccess() is not really responsible for non-existing email address check. It only cares if the email is fired???

Is there any way to log if the email account does not exist so I can log such occasion in my Apex code?

try {
    Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new
            Messaging.SingleEmailMessage[] { mail });

    resultMail[0].getErrors();

    //display success or error message
    if (resultMail[0].isSuccess()) {
        response = 'ok sent!';
    } else {
        response = resultMail[0].getErrors().get(0).getMessage();
    }

    //log
    boolean isSuccess = resultMail[0].isSuccess();
    Integer out = EmailLogger.logEmailSent(this, isSuccess);
} catch (System.EmailException ex) {
    system.debug('============== email exception caught!!!=============');
    response = ex.getMessage();
}
Was it helpful?

Solution

Email (SMTP) is a store and forward protocol, at the time of sending, you can't tell that the destination email address is non-existant, you can only find that out once the message actually gets to the final destination server.

OTHER TIPS

if there was a way to find email address whether it really exists or not, a spammer might have tried brute force attack - trying every possible combination of email and sending infinite spams :)

thank god, that's not possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top