I have written an Apex Email Service and would like to setup a rule in gmail to forward certain messages to the Email Service.

The first step when creating a forwarding address in gmail is the address first needs to be verified. This is accomplished by requesting a verification code which is sent to the address.

My question is how do I get this email so I can access the verification code?

I have enabled error routing so that failures are sent to me. I have modified the email service to always return a failure, and when I send an email to the service directly I get the failure message. But I request the verification code through google and receive nothing.

I have Accept Email From: mydomain.com, gmail.com, google.com, which may be excessive, because I sent a forward request to a colleague to inspect the email and it originates from mydomain.com

  • 1
    My 2p : Any joy with printing the contents of any inbound email to system.debug an copying verification code off there, if the email from google is making it that far? Also hopefully no attachments from google else i think you need to explicitly specify your email service can accept attachments. Commented Nov 8, 2012 at 8:28
  • +1 for @techtrekker, just do system.debug for inbound email body to print the code. Commented Nov 8, 2012 at 9:43

2 Answers 2


Wow debugging email services is hard! After alot of trial and error I was able to solve my problem.

Thanks for the suggestions @techtrekker and @Abhinav but placing a system.debug in the service did not help as the class was not being called.

The reason for this was the 'Accept Email From' needs to contain the domain of the original sender. For example [email protected] sends me an email and my gmail rule forwards it to the email service 'Accept Email From' must contain the salesforce.com domain for the email to be processed.

Additionally, after a bit more testing it appears the 'Accept Email From' does not require mydomain.com

Edit: As suggested by @tanuj leaving 'Accept Email From' blank means all emails are processed, which makes getting the verification code is easier.

  • But how did you retrieve the verification code for the forwarding rule to work in gmail?
    – user14998
    Commented Dec 22, 2014 at 21:50
  • I added a debug statement to the service, which printed out the plainTextBody to the debug log, from there I had the code. Commented Dec 23, 2014 at 8:17
  • Daniel what is the Apex Email service to route your emails? What is the apex code ? Commented Sep 9, 2015 at 17:56
  • Can you please add the debug statement to the service to your answer ?
    – Jarvis
    Commented Jun 18, 2018 at 7:27

You can also capture the entire email body into task description and read the verification code from there.

global class EmailService1 implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, messaging.InboundEnvelope envelope)  
  Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
  Task tObj1=new Task();
  tObj1.Subject = email.Subject;
  tObj1.Description = email.plainTextBody;
  tObj1.WhoId = objCt.ID;
  insert tObj1;
  return result;
  • This is great so long as the email service is firing, but in my case the security settings were preventing the code from executing Commented Nov 9, 2012 at 23:58
  • True. In the beginning, 'Accept Email From' can be kept blank so that all mails are received. Once the verification is done, accepted domains can be added into it. Commented Nov 10, 2012 at 7:35
  • Another great suggestion, I have added this to my answer Commented Nov 10, 2012 at 23:19

You must log in to answer this question.

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