Find your content:

Search form

You are here

Gmail Forwarding Address verification code for Apex Email Service


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:,,, which may be excessive, because I sent a forward request to a colleague to inspect the email and it originates from

Attribution to: Daniel Blackhall

Possible Suggestion/Solution #1

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 sends me an email and my gmail rule forwards it to the email service 'Accept Email From' must contain the domain for the email to be processed.

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

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

Attribution to: Daniel Blackhall

Possible Suggestion/Solution #2

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;

Attribution to: Tanuj Kumar Sharma
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content