You need to sign in to do that
Don't have an account?
apexdevSunil
Apex class to separate an attachment from the Email sent to Salesforce from web mail
Hi
Iam sending an email with attachment from Gmail to SalesForce
I want to develop an Apex class which retreives that Email and separate the attachment from that email
Any help or suggestion on this is greatly appreciated
Sunil
I would use an inbound email service to do this.
Your apex could would then process each email that comes in.
There are 2 types of attachments that can be accessed from the inbound email, textAttachments and binaryAttachments.
they are passed in as arrays of objects.
Your code would loop through each type of attachment you are interested and process the individual results.
For example, you could restrict your inbound service to only accept binary attachments (like PDF's, Spreadsheets, etc).
Then you would loop through the array of binaryAttachments, and extract each one, possibly saving it is a document, by converting the body (a blob) into a document object, and then saving that document in a specified folder.
Look in the documenation for Inbound email service, and for Inboundemail.binaryattachment for more details.
Here are links to some documentation around Email Services, including sample code processing an Inbound Email:
Force.com Email Services
Apex Inbound Email Class documentation
Hi
I have written an Apex class which implements the handleInboundEmail method of Messaging.InboundEmailHandler.
How to invoke this Apex class when ever the salesforce receives a mail
The purpose of the above Apex class is process all the mails that come to salesforce.
I want to get the attachments of the email then I want to insert the attachment into an Opportunity
Here is the code what Iam doing
Any help on this is greatly is appreciated
Thanks
sunil
Looks like you are pretty close. The only major thing missing is you never inserted the attachment.
A few more lines in this section would do it:
attachment.name = attachmentName; attachment.body = body; attachment.parentId = opp.id; try{ insert attachment; }catch(DMLException e){ system.debug('ERROR Inserting attachment:'+e.getDMLMessage(0)); }
Thanks Rae!
your code is helpful and I got it
Thanks
Sunil
Hi,
I have the similar problem. I want to related attachment's parentId to case in place of InboundEmaiResult. Also attached the code for reference. It is working fine if I test it using testmethod but when I try to execute it by sending manual mail it fails. Please guide.
global class EmailToCase implements Messaging.Inboundemailhandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope env){ Case caseObj = new Case(); caseObj.Description = email.htmlBody; caseObj.Priority = 'Medium'; caseObj.Origin = 'Email'; caseObj.Subject = email.subject; caseObj.SuppliedEmail = email.fromAddress; try { insert caseObj; }catch(QueryException e) { System.debug(e); } System.debug(caseObj); List<Attachment> attachObjList = new List<Attachment> (); if(email.binaryAttachments != null && email.binaryAttachments.size() > 0) { for(Messaging.InboundEmail.BinaryAttachment binaryAttachObj : email.binaryAttachments) { Attachment attachmentObj = new Attachment(); attachmentObj.Body = binaryAttachObj.body; //attachmentObj.ContentType = binaryAttachObj.mimeTypeSubType; attachmentObj.Name = binaryAttachObj.fileName; attachmentObj.ParentId = caseObj.Id; attachObjList.add(attachmentObj); } } if(email.textAttachments != null && email.textAttachments.size() > 0) { for(Messaging.InboundEmail.Textattachment textAttachObj : email.textAttachments) { Attachment attachmentObj = new Attachment(); attachmentObj.Body = Blob.valueOf(textAttachObj.body); //attachmentObj.ContentType = textAttachObj.mimeTypeSubType; attachmentObj.Name = textAttachObj.fileName; attachmentObj.ParentId = caseObj.Id; attachObjList.add(attachmentObj); } } try { insert attachObjList; }catch(QueryException e) { System.debug('Attachment Error ' + e); } System.debug(attachObjList); return null; } }
Test Method
// Create a new email, envelope object and Attachment Messaging.InboundEmail email = new Messaging.InboundEmail(); Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); Messaging.InboundEmail.BinaryAttachment inAtt = new Messaging.InboundEmail.BinaryAttachment(); email.subject = 'test'; env.fromAddress = 'user@acme.com'; // set the body of the attachment inAtt.body = blob.valueOf('test'); inAtt.fileName = 'my attachment name'; inAtt.mimeTypeSubType = 'plain/txt'; email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] {inAtt }; // call the class and test it with the data in the testMethod EmailToCase emailServiceObj = new EmailToCase(); emailServiceObj.handleInboundEmail(email, env );