SalesForce-An Introduction to Email Services

global class EmailDemoReceive implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(
Messaging.InboundEmail email, Messaging.Inboundenvelope envelope)

{
 
   Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
   Account account;
   system.debug('recived email from ...........'+email.fromName);
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];
  }  
result.success = true;
system.debug('------Process Done------------'+email.plainTextBody);
}
catch (Exception e) {

      result.success = false;

      result.message = 'Oops, I failed.';

    }
return result;
                                               
    }
}

Setup:


Email Service

Next, you need to define the email service. This establishes an email address, which you can then tie to the Apex class that you’ve just written. Define the email service by logging into your environment:
  1. Log into Force.com and select on Setup->App Setup->Develop->Email Services
  2. Select New Email Service
You’ll see something like the following screen:

Creating a new Email Service
Filling this in is straightforward:
  1. Give a name for Email Service Name, like say “EmailDemoReceive”
  2. For Apex Class, specify the Apex class you just built
  3. You can leave the rest of the fields at their default values for starters
  4. Hit Save
That’s it, you’ve created your first email service. But you still need to generate an email address on which Force.com will listen for incoming emails. To do that, select New Email Address and fill as follows:
  1. Choose an id for Email address. This can be anything, like say "invoices" if you plan to process invoices with this address.
  2. Check Active.
  3. Set Context User – this specifies the context under which the Apex class will run. It defaults to you, but you’re free to pick another user from your organization.
  4. For Accept Emails From, give a comma-separated list of addresses (a@b.com) or domains (b.com) that Email Services can accept emails from. If you leave it blank, all addresses will work.
  5. Hit Save.

Completing Email Service Setup
You will see a new email address that Force.com will monitor: emails received on this address will trigger the associated Apex code.

An Email Service, ready to receive
That’s it! Any email sent to the above email address will now result in the Apex class being executed. 

For More Info:

Comments

Popular posts from this blog

Approval Process

Sending Mass Emails From Salesforce

Spring Batch 2.0 -Basic Concepts