[SalesForce] Convert application/octet-stream in E-mail Processing class

Context: e-mails are sent with text attachments from a Dynamics SL/SQL Database to a Salesforce e-mail processor. The text attachments contain a formatted file with data to be processed and imported into Salesforce.

However, despite having the '.txt' file extension, Salesforce processes them as binaryAttachments, complete with a MIMEType of application/octet-stream. The 'toString' method fails due to the MIMEType, and EncodingUtil.base64Encode(Blob body) spits out gibberish.

The files are not corrupted. I have Salesforce sending me alerts on failure, and the attachments open fine on my local machine.

Does anyone know if there is a way to change the 'mimeTypeSubType' value of an attachment so that the toString() method will operate? Or is there another way to extract plain text (ascii encoded) content from an application/octet-stream attachment?

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    if(email.textAttachments != null && !email.textAttachments.isEmpty() && email.textAttachments.size() >= 1){
        System.debug(email.textAttachments.size());
        System.debug(email.textAttachments[0].mimeTypeSubType);
        System.debug(email.textAttachments[0].body);
        processFile(email.textAttachments[0].body, email.subject);
    }else if(email.binaryAttachments != null &&!email.binaryAttachments.isEmpty() && email.binaryAttachments.size() >= 1){
        //processFile(email.binaryAttachments[0].body.toString(), email.subject);
        System.debug(String.valueOf(email.binaryAttachments[0].body));
        System.debug(email.binaryAttachments[0].fileName);
        System.debug(email.binaryAttachments.size());
        System.debug(email.binaryAttachments[0].mimeTypeSubType);
        email.binaryAttachments[0].mimeTypeSubType = 'text/csv';
        System.debug(email.binaryAttachments[0].body);
        System.debug(EncodingUtil.base64Encode(email.binaryAttachments[0].body));
        processFile(EncodingUtil.base64Encode(email.binaryAttachments[0].body), email.subject);
    }else 
        return result;

    return result;
}

Best Answer

There are multiple things that can cause this behavior.

1) You might have set the email service to accept attachments in only 'Binary attachments only'. If you that is the case then your issue may resolved when you set it to 'text attachments only'

2) The option 'Convert Text Attachments to Binary Attachments' might be checked.

3) There is known issue with some client (including outlook) which is due to MIME type not getting set properly. You can try sending test email email with gmail and check if works. http://help.salesforce.com/HTViewSolution?id=000193349

Can you try above options and let us know if it resolve your issue?

When I run your code in my dev org it gave proper output So now it make me suspicious about configuration or email client as stated above.

22:56:41.0 (11210148)|SYSTEM_METHOD_ENTRY|[5]|List<Messaging.InboundEmail.TextAttachment>.isEmpty()
22:56:41.0 (11245194)|SYSTEM_METHOD_EXIT|[5]|List<Messaging.InboundEmail.TextAttachment>.isEmpty()
22:56:41.0 (11261358)|SYSTEM_METHOD_ENTRY|[5]|List<Messaging.InboundEmail.TextAttachment>.size()
22:56:41.0 (11267037)|SYSTEM_METHOD_EXIT|[5]|List<Messaging.InboundEmail.TextAttachment>.size()
22:56:41.0 (11276564)|STATEMENT_EXECUTE|[5]
22:56:41.0 (11278654)|STATEMENT_EXECUTE|[6]
22:56:41.0 (11284723)|SYSTEM_METHOD_ENTRY|[6]|List<Messaging.InboundEmail.TextAttachment>.size()
22:56:41.0 (11305254)|SYSTEM_METHOD_EXIT|  [6]|List<Messaging.InboundEmail.TextAttachment>.size()
22:56:41.0 (11461203)|SYSTEM_METHOD_ENTRY|[6]|System.debug(ANY)
22:56:41.0 (11477121)|USER_DEBUG|[6]|DEBUG|1
22:56:41.0 (11484874)|SYSTEM_METHOD_EXIT|[6]|System.debug(ANY)
22:56:41.0 (11490304)|STATEMENT_EXECUTE|[7]
22:56:41.0 (11504075)|HEAP_ALLOCATE|[7]|Bytes:28
22:56:41.0 (11516923)|SYSTEM_METHOD_ENTRY|[7]|System.debug(ANY)
22:56:41.0 (11540265)|USER_DEBUG|[7]|DEBUG|text/plain
22:56:41.0 (11546063)|SYSTEM_METHOD_EXIT|[7]|System.debug(ANY)
22:56:41.0 (11550799)|STATEMENT_EXECUTE|[8]
22:56:41.0 (11559314)|HEAP_ALLOCATE|[8]|Bytes:28
22:56:41.0 (11570610)|SYSTEM_METHOD_ENTRY|[8]|System.debug(ANY)
22:56:41.0 (11589259)|USER_DEBUG|[8]|DEBUG|07853426
22:56:41.0 (11594914)|SYSTEM_METHOD_EXIT|[8]|System.debug(ANY)
Related Topic