1

Here i was made one Inbound Email Service..For that i made one Apex Class,here the code of the 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();

   try {
       if ([select count() from Account where Name = :email.subject] == 0) {
       account = new Account();
       account.Name = email.subject;
       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;
}

        // 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;
      }
    }

after that i was made Email service,here is the snapshot for that... enter image description here

whenever i received a mail As a ToAddress of Above Encoded mail address in red portion...then and then Account Record is created.....

But here i want to use user's real Email Address Instead of Encoded email address...

whenever the mail is received from user's real email address,Apex class is run and Account record is created ....that exactly here i want.....

If any solution then please respond me......

  • Hello Please Help me if any solution? – Vivek113 Mar 28 '14 at 11:24
  • Have you tried forwarding the user's email address (within your own email environment) to the @[...].apex.salesforce.com address and seeing if your class is used to process the email? – tedweinberg Mar 28 '14 at 13:41
  • thanks for your response....here i am sending mail from my email environment to emaildemoreceive@1ukqw305qq4famwgmq6l9wktxcn5t92wrg1fk7mly5aq07c457.9-otdneam.ap1.apex.salesforce.com.......then account record is created....but i want to send user's email address like abc@gmail.com and account record will be create that was not created here...that i exactly want here..... – Vivek113 Mar 29 '14 at 5:09
1

whenever i received a mail As a ToAddress of Above Encoded mail address in red portion...then and then Account Record is created.....

But here i want to use user's real Email Address Instead of Encoded email address...

I suspect you may have a misunderstanding on what an inbound email service is. All email must be sent to the encoded email address for processing by your inbound service.

I also see an issue in your code. You're converting your "cc'd" addresses to contacts instead of your "from" addresses.

  } else {
  account = [select Id from Account where Name = :email.subject];
  }
  // Convert FROM addresses to contacts (There's only 1 FROM address!)
  for (String address : email.fromAddress) {
     Contact contact = new Contact();
     contact.FirstName = email.fromname.substring(0,email.fromname.indexOf` `));
     contact.LastName = email.fromname.substring(email.fromname.indexOf` `));
     contact.Email = envelope.fromAddress;
  }

You'll note that I've omitted your matcher code in my response. I don't really think its necessary, but feel free to implement it in your code if you wish.

As @tedweinberg also mentioned in his comment, your class' security settings will affect the email addresses you can receive email from. If an address doesn't pass at least one of the security protocols you've enabled, it will be rejected and won't be processed. Hope this gets you up and running.

-2

use

EM.ToAddress = email.toAddresses[0];

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.