[SalesForce] How to monitor delivery of emails sent by salesforce by source code

I have following questions.
Assuming I have following code

public class MessageMaker {
public static void helloMessage() {
    System.debug( 'Entry point' );

    Case c = new Case();
    insert c;

    EmailMessage e = new EmailMessage();
    System.debug( 'EmailMessage created' );
    e.parentid = c.id;
    // Set to draft status.
    // This status is required 
    // for sendEmailMessage().
    e.Status = '5'; 
    e.TextBody = 
      'Sample email message.';
    e.Subject = 'Apex sample';
    e.ToAddress = 'my@mail.com';
    insert e;

    List<Messaging.SendEmailResult> 
      results = 
      Messaging.sendEmailMessage(new ID[] 
        { e.id });
    System.debug(results.size());
    System.debug(results[0].success);       
    System.debug(results[0].getErrors().size());
    System.assertEquals(1, results.size());
    System.assertEquals(true, results[0].success);
}
}
  1. First question.
    I want to find out using apex code if the message was really delivered.

Here documentation says
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_sendemail_emailresult.htm

Even if success = true, it does not mean the intended recipients received the email, as it could have bounced or been blocked by a spam blocker. Also, even if the email is successfully accepted for delivery by the message transfer agent, there can still be errors in the error array related to individual addresses within the email.

So I have been trying to send email by apex code and look for results[0].success.
It seems it says like it is described in documentation, so success is true even though email address was incorrect.

Also I have tried to check this manually through email logs
http://eu2.salesforce.com/help/doc/en/email_logs.htm

And I have found following row in resulting log regarding my email sent to incorrect address

9/2/2013 9:36 66/FC-09306-20B54225 T julfy@i.ia julfy=i.ua__0-6uvic1ltvun1nf@95mngihd2hpe0w.b-ysubea0.bl.bnc.salesforce.com 1434 005b0000000J7bm <_0vTJ000000000000000000000000000000000000000000000MSHRSY00W1-CKg3DShWC5xu24ccHFA@sfdc.net> 1 311.627583 421 4.4.0 [internal] no MXs for this domain could be reached at this time

But I don't know how to access this information by apex code. Any thoughts?

  1. Second question.
    If message was delivered and recipient forwarded it, is any possibility to monitor that using apex code?
    Anybody?

Best Answer

SendEmail "success" indicates that the salesforce.com MTA tentatively accepted the email. It is actually placed into a mail queue for later delivery (only after the current transaction completes). This is necessary because salesforce.com does not send the email until after the transaction completes. Any uncaught exception will cancel the email from being sent, as if it hadn't happened. There's no programmatic access to email logs; the "Request Email Log" actually queries the MTA's logs for emails sent from the organization. This data isn't specifically stored "in salesforce.com" proper, and thus, there's no access to the information directly.

Related Topic