0

Will I need to verify my email in order to activate my email service?

I can't imagine that Gmail would allow an external domain to collect emails without permission...Yet salesforce hasn't sent me any type of forwarding verification request...

Furthermore, how can I monitor that my service is working?

2 Answers 2

1

Add a system.debug in your apex class for the subject and search for the text receiving mail from gmail in your debug log:

Here is a sample code below how I got the confirmation code

Enable the bounce back option to your mail so that you can track it easily.

Apex class:

global class EmailDemoReceive implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                     Messaging.Inboundenvelope envelope) {
Account account;
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
System.debug('############'+result);

try {
// Look for account whose name is the subject and create it if necessary
if ([select count() from Account where Name = :email.subject] == 0) {
 account = new Account();
 account.Name = email.subject;
 System.debug('############'+account.Name );
 insert account;
} else {
account = [select Id from Account where Name = :email.subject];
}
// Convert cc'd addresses to contacts
for (String address : email.ccAddresses) {
 Contact contact = new Contact();
 Matcher matcher = Pattern.compile('<.+>').matcher(address);

// Parse addresses to names and emails
if (matcher.find()) {
String[] nameParts = address.split('[ ]*<.+>')[0].replace('"', '').split('[ ]+');

contact.FirstName = nameParts.size() > 1 ? nameParts[0] : '';
contact.LastName = nameParts.size() > 1 ? nameParts[nameParts.size()-1] : nameParts[0];
contact.Email = matcher.group().replaceAll('[<>]', '');
 } else {
contact.LastName = address;
contact.Email = address;
}

// Add if new
if ([select count() from Contact where Email = :contact.Email] == 0) {
 contact.AccountId = account.Id;
 insert contact;
 }
 }
// Save attachments, if any
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
Attachment attachment = new Attachment();

attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
 attachment.ParentId = account.Id;
insert attachment;
 }
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
 Attachment attachment = new Attachment();

 attachment.Name = bAttachment.fileName;
 attachment.Body = bAttachment.body;
 attachment.ParentId = account.Id;
 insert attachment;
  }
  // Turn email body into note
  Note note = new Note();

  note.Title = email.fromName + ' (' + DateTime.now() + ')';
  note.Body = email.plainTextBody;
  note.ParentId = account.Id;
  insert note;
  result.success = true;
} catch (Exception e) {
  result.success = false;
  //result.message = "Oops, I failed";
}

return result;
 }
 }

Debug log:

[Account: (#220084905)  Forwarding Confirmation - Receive Mail from [email protected] 0017A000002OV3e]

220084905- Confirmation code

7
  • I will give this a try and let you know how it works. Thank you! Sep 9, 2015 at 21:12
  • WIll the Try clause not need some catch/finally block? Sep 9, 2015 at 21:18
  • I posted the complete code to handle the attachments and the body of mail but for confirmation all we need is subject. Sep 9, 2015 at 21:31
  • Okay thank you, so once this code in in my apex class, should I resend the verification email? Where will it log the result, or what do I do to make it log the result? Sep 9, 2015 at 21:39
  • Enable your debug logs also add this apex class to your email service and make it active. Now resend the verification email Sep 9, 2015 at 21:41
1

Yes, it is definitely doable. Follow the steps below:
1. Note down the email ID of the email service (a long email address like: saveattachmenttorecord@xxxxxxxxxxxxxxxxxxxx-njhbea2.xxx.apex.salesforce.com)
2. In GMail, go to:
a. Settings -> 'Forwarding POP/IMAP' -> Add a forwarding address -> Enter the above Email address
b. Now under forwarding, you can select Forward a copy to -> Select the email you just added

To Know if your email service is working, you can:
- Send a email notification to yourself
- Store a log in form of records
- Check if the logic inside the service is reflecting in your org


For verifying the long email address, you could use following for the first time: I would suggest may be some other way...but this was a quick solution i could guess...

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

    Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
    String[] sendingTo = new String[]{'[email protected]'};
    semail.setToAddresses(sendingTo);
    semail.setSubject('Verification!');
    semail.setPlainTextBody(email.subject);
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    return result;
}
5
  • Thank you for your answer! I have asked Gmail to forward a copy of all incoming mail to the long email address that salesforce has provided. Although I still need verification from THAT email to enable forwarding. Where in salesforce would I approve this movement? Sep 9, 2015 at 18:05
  • updated answer...
    – ipradhansk
    Sep 9, 2015 at 18:23
  • Okay so this has to be done within the Apex class? There is no way to "click" verify? Sep 9, 2015 at 18:30
  • 1
    You do not verify an inbound email service address. You only have to verify it if you are trying to use it as an Org wide email address. If you do that, then you will have to write code to verify it.
    – Eric
    Sep 9, 2015 at 18:52
  • @Eric Won't I need to forward my emails from my business Gmail to this address in order for the Email service to run? In order to forward from Gmail I would need verification from the salesforce-generated email address, no? Sep 9, 2015 at 18:55

You must log in to answer this question.

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