title: How to handle error in Messaging.sendEmail()?
body: <p>I wrote code to send email. It works fine but my goal is:</p>
  
  <p>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')</p>
  
  <p>I've tried 2 things with the code below.</p>
  
  <ol>
  <li><p>provided non-email string 'foo@!' </p></li>
  <li><p>provided non-existing email address 'thisdoesnotexistignsdfkjsdf@gmail.com'</p></li>
  </ol>
  
  <p><strong>result:</strong></p>
  
  <p>Execute case 1 caused code to go into <code>catch block</code> thus outputting error message on the html page which is expected.</p>
  
  <p>Execute case 2 caused code to return <code>'ok sent!'</code></p>
  
  <p>And after few minutes I received email that delivery failed.</p>
  
  <p>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???</p>
  
  <p>Is there any way to log if the email account does not exist so I can log such occasion in my Apex code?</p>
  
  <pre><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();
  }
  </code></pre>