Apex Email Services
Scenario:-
Email services are automated processes that use Apex classes to process the contents, headers, and attachments of inbound email.In this Example we can create an email service that automatically creates Cases based on Case information.
Step 1:-
------
Create a New Class :- Setup -> Develop -> ApexClasses
global class blocklist_emailhandler implements Messaging.InboundEmailHandler{
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
String myPlainText='',emailaddr='',prior='',url='',catog='',addremove='';
myPlainText = email.plainTextBody;
system.debug(myPlainText+'==================='+email.subject);
// formatting the Email and storing into the variables
if(myPlainText.contains('{') && myPlainText.contains('}')){
string temp=myPlainText.substring(myPlainText.indexOf('{'),myPlainText.indexOf('}'));
if(temp.contains(',')){
for(string s:temp.split(',')){
if(s.contains('Email address')){
if(s.contains('<')){
emailaddr=s.substring(s.indexOf('=')+1,s.indexOf('<')-1);
}
else{
emailaddr=s.substring(s.indexOf('=')+1,s.length());
}
}
else if(s.contains('Priority')){
prior=s.substring(s.indexOf('=')+1,s.length());
}
else if(s.contains('URL')){
url=s.substring(s.indexOf('=')+1,s.length());
}
else if(s.contains('Category')){
catog=s.substring(s.indexOf('=')+1,s.length());
}
else if(s.contains('Add/Remove') || s.contains('Add / Remove')){
addremove=s.substring(s.indexOf('=')+1,s.length());
}
}
}
// try{
System.Debug('**************'+emailaddr);
//Getting Account and Contact using query
Contact con=[Select id,Name,AccountID,Email from contact where AccountID!=null and Email!=null limit 1];
// Owner Should be International - Escalations Queue
QueueSobject que= [Select Id,Queue.Id,Queue.Name from QueueSobject where Queue.Name ='International - Escalations'];
//Creating case with the information sent by Email
Case newcase=new Case();
newcase.AccountID=con.AccountId;
newcase.ContactID=con.id;
newcase.SuppliedEmail=emailaddr;
newcase.SuppliedName=url;
newcase.Priority=prior;
newcase.Type=catog;
newcase.Add_or_Remove__c=addremove;
newcase.Subject=email.subject;
newcase.Status='New';
newcase.Transfer_to__c='Support Queue';
newcase.Ownerid=que.Queue.Id;
insert newcase;
//}
// Catch(Exception e){}
}
return result;
}
}
Check if there is Any Custom Fields , then you have to create those fields in your instance as well.
Step 2:-
---------
Create Email Service :- Setup -> Develop -> Email Services
Here you have to Give the Email that from which email your sending the Emails to Sales force.
And Save
Click on New Email Address Button in Below of the Page
Then You will get one Sales force Email Address.
Step 3:-
--------
Copy that Email Address and And You have to Send one Email the Sales force Email Address,
The Body should be in Proper below format.
Hi,
Bla..bla..bla................
{
Email address=testemail@test.net,
Priority=High,
URL(S)=www.test2.com,
Category=Movies,
Add/Remove=Add
}
Best
bla..bla..bla..........
Here in B/W '{' and '}' the Data is Impotent. So we should maintain the constant Structure.
After Sending Email,if you can check the case object in your sales force instance.
The Case record is created with the Details of which you have sent through the mail.
The Above Task is Done..
So friends here my main intention is to people should know how to use the Email Services in Sales force.
The above information may be useful to you.