+ Start a Discussion
raj29matraj29mat 

Apex email services

Hi All,
 
   I was able to create cases for contact from the information from an email send using apex
email services.I have created an apex class and associated an email service for the same.
I have a requirement to save attachments contained in those emails to a local folder in
system and provide a reference to the same while creating the case from email?
Anybode has the apex class code for the same.
 
 
Thanks and Regards
Raji
guneetsahaiguneetsahai
Hi Raji,

You might find the method copied below useful. It would store attachments (text and binary) from an Inbound email object and associate with any SObject (which could be a Case in your problem).

Hope this helps

Thanks
Guneet Sahai
http://guneetsahai.com


        /**
         * Stores attachments in the incoming email into SF System
         */
        private static void storeAttachments (Messaging.InboundEmail email, String parentId)
        {
                System.debug ('Calling MAin storeAttachments');       
                Messaging.InboundEmail.BinaryAttachment[] bAttachments = email.binaryAttachments;
                Messaging.InboundEmail.TextAttachment[]   tAttachments = email.textAttachments;
                if (bAttachments != null)
                {
                        for (Messaging.InboundEmail.BinaryAttachment mailAttachment : bAttachments)
                        {
                                String attachmentName   = mailAttachment.fileName;
                                Blob   body             = mailAttachment.body;
                                Attachment attachment   = new Attachment ();
                       
                                // check attachment name if null                       
                                if(attachmentName == null || attachmentName == '' )
                                {
                                    attachmentName = 'noname';             
                                }
                       
                                attachment.name         = attachmentName;
                                attachment.body         = body;
                                attachment.parentId     = parentId;
                                System.debug ('Storing Binary Attachment  -> ' + attachmentName + ' for email with id = ' + parentId);
                                insert attachment;
                                System.debug ('Stored Binary Attachment  -> ' + attachmentName + ' for email with id = ' + parentId);
                        }              
                }




                if (tAttachments != null)
                {
                        for (Messaging.InboundEmail.TextAttachment mailAttachment : tAttachments)
                        {
                                String attachmentName   = mailAttachment.fileName;
                                String body             = mailAttachment.body;
                                Attachment attachment   = new Attachment ();
                                // check attachment name if null                       
                       
                                if(attachmentName == null || attachmentName == '' )
                                {
                                      attachmentName = 'noname';             
                                }
                                attachment.name         = attachmentName;
                                attachment.body         = Blob.valueOf (body); // EncodingUtil.base64Decode (body);
                                attachment.parentId     = parentId;

                                insert attachment;
                                System.debug ('Stored Text Attachment  -> ' + attachmentName + ' for email with id = ' + parentId);
                        }
                }
        }