I need to create a system to allow customers to submit documents for approval automatically. The design is:

  • A button on the lead/contact page to send a request for a document.
  • That buttons sends an email asking the user to reply directly with their attachment, with the Reply-To header set to the address of the inbound email service*.
  • At the same time, the Message-ID header is saved** on the lead/contact.
  • When the lead replies, their email client automatically sets the reply to field to the inbound email service address due to the header.
  • The reply email arrives at the inbound email service and if the Message-ID*** header matches one saved on a lead, the attachments are processed and attached to that lead.

I have run in to two problems and a query here though:

1 - * - How do I get the address of the inbound email service in Apex? I can't hard code it because as soon as I deploy from a sandbox to a live environment the address will need to be changed.

2 - ** - How do I retrieve the Message-ID header? I know how to do this when working with an inbound email, but can't find any way to do it for an outbound one.

3 - *** - Does anyone know off-hand whether a Message-ID header will stay the same in a reply? I heard it would because that's how email clients group threads together.

If the answer to any of my questions is "it can't be done", what alternatives do I have?

  • why not just put the Lead ID into the outbound email subject line and use that to match on when the email is sent back and processed by your email service? and don;t you want to use friendly email addresses like [email protected] that you then use your email server to forward to the salesforce inbound email handler?
    – cropredy
    Commented Jul 29, 2015 at 18:10
  • I thought about that but heard it was bad practice, at least with email headers the client user can't (easily) edit them. I suppose it looks like the only way though.
    – Adam
    Commented Jul 30, 2015 at 8:03
  • well - SFDC uses a formula to generate thread ids for email-to-case; you could simulate the same with a "Lead thread Id" to do the matching if you don;t want to expose the lead id directly (that is, you are obfuscating it). Google to see how thread ids are constructed
    – cropredy
    Commented Jul 30, 2015 at 18:11

2 Answers 2


As an alternative to the message header approach, you could:

  1. Place the Lead Id in the subject or body of the outbound email
  2. When the inbound email reply comes in, your email handler looks for the Lead Id and matches to the Lead in the database

If you don't like placing the LeadIds in the 'clear', you can obfuscate them similar to the way SFDC uses threadIds to identify the Case for email conversations. The Thread ID is constructed this way to give you an idea. The nice thing about threadIds is their visual format discourages an email recipient from deleting it. ThreadIds are also somewhat more reliable to look for than SObjectIds in text

  • I went with just including the lead ID in the subject as I'm working on a schedule, if the inbound email processor can't find a substring in the subject that matches the regex pattern for a SF ID it sends a reply back telling the user to make sure they don't modify the subject. If I have more time I may implement this method though,
    – Adam
    Commented Jul 31, 2015 at 8:18

I came across this question by searching for 'Retrieve Inbound Email Service address in Apex' which is the first part of your question. I wanted to share the solution I found to this question in case someone else is attempting to get the generated email address for an inbound email service.

EmailServicesFunction emailFunction = [SELECT Id FROM EmailServicesFunction WHERE FunctionName='ServiceName'];
EmailServicesAddress emailServicesAddress = [SELECT Id, LocalPart, EmailDomainName FROM EmailServicesAddress WHERE FunctionId = :emailFunction.Id];
System.debug('Email Service Address: ' + emailServicesAddress.LocalPart + '@' + emailServicesAddress.EmailDomainName);

In the above snippet ServiceName is the String value displayed for Email Service Name on Email Service page layout.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .