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;
}