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. – techtrekker Nov 8 '12 at 8:28
  • +1 for @techtrekker, just do system.debug for inbound email body to print the code. – Abhinav Gupta Nov 8 '12 at 9:43

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 chatty@salesforce.com 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 Dec 22 '14 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. – Daniel Blackhall Dec 23 '14 at 8:17
  • Daniel what is the Apex Email service to route your emails? What is the apex code ? – Salesforce Learner Sep 9 '15 at 17:56
  • Can you please add the debug statement to the service to your answer ? – Jarvis Jun 18 '18 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 – Daniel Blackhall Nov 9 '12 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. – Tanuj Kumar Sharma Nov 10 '12 at 7:35
  • Another great suggestion, I have added this to my answer – Daniel Blackhall Nov 10 '12 at 23:19

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.