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:
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:
- Log into Force.com and select on Setup->App Setup->Develop->Email Services
- Select New Email Service
You’ll see something like the following screen:
Filling this in is straightforward:
- Give a name for Email Service Name, like say “EmailDemoReceive”
- For Apex Class, specify the Apex class you just built
- You can leave the rest of the fields at their default values for starters
- 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:
- Choose an id for Email address. This can be anything, like say "invoices" if you plan to process invoices with this address.
- Check Active.
- 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.
- 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.
- Hit Save.
You will see a new email address that Force.com will monitor: emails received on this address will trigger the associated Apex code.
That’s it! Any email sent to the above email address will now result in the Apex class being executed.
For More Info:
Comments