3

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?
8

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.

  • So does that mean that I can't query MTA from my apex code? – Patlatus Sep 3 '13 at 7:43
  • At this time, that's correct. Don't shoot the messenger... – sfdcfox Sep 3 '13 at 20:52
3

The things you are trying to do are essentially impossible. Spam blockers and the like can swallow an email without reporting an error. Native email forwarding under no circumstances reports back the forward to the original sender. And "real" (is this an actual email address?) address validation is not possible under the architecture of internet email. So quick answer: no. An email open tracker can validate that none of these things happened, and that someone most likely actually opened the email, but many clients do not render them or users have set their email to not render images, so they are not a silver bullet either.

  • It is strange that some function can be accessed only manually but not from source code. I would expect if something is possible by manual request that it should be available via some source code method call. – Patlatus Sep 2 '13 at 12:33
  • 1
    The logs are queried asynchronously. You can't query all metadata via Apex Code, either, such as page layouts. – sfdcfox Sep 2 '13 at 16:06
  • @sfdcfox has it exactly. The emails are only sent once your apex finishes executing, and it can in theory take hours and a number of retries before an error surfaces. SMTP is an "eventual delivery" protocol that you can't count on performing synchronously. – ca_peterson Sep 2 '13 at 21:54
  • So I can't make asynchronous call via Apex Code to receive at least some metadata like T or R or D? – Patlatus Sep 3 '13 at 7:46
  • As @sfdcfox said, no, there's no programmatic access to email logs. Even if there were, though, it wouldn't give you what you want - there is simply NO reliable way to tell the true end-user delivery status of SMTP email. – jkraybill Sep 3 '13 at 8:37
2

I think what you can do it's to activate Deliverability. Go to Setup > Email Management > Deliverability. Here is the doc The thing with this method is your EmailMessage must to be linked to a real Contact or Person Account ID --> you need an email template. I can not remember how Salesforce response when you send a email the first time. But I think if you send an email again you will get a exception such as INVALID_EMAIL_ADDRESS.

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setUseSignature(false);
email.saveAsActivity = false;
email.setTargetObjectId(contactId); 
email.setFileAttachments(emailFileAttachments);
email.setTemplateId(emailTemplate.Id);
  • Actually I have already activated "deliverability". – Patlatus Sep 3 '13 at 7:24
  • I mean I have checkbox Activate bounce management checked. Am I missing anything? – Patlatus Sep 3 '13 at 7:25
  • You have to use Messaging.SingleEmailMessage, an emailTemplate and a target Object Id(contact or personaccount). I Updated my answer – Cloud Ninja Sep 3 '13 at 9:46

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.