+ Start a Discussion
Krishna KatveKrishna Katve 

Unreadable email attachments sent via apex Messaging.sendEmail

Hi All,

We are sending record attachments via apex sendEmail email to salesforce users.
But one of our user is getting email attachment file in unreadable format.
I tried to open the same file by going to Salesforce record and can see the file is good and readable. Also tried to send these record attachments via developer console to myself and all the files are coming good.

Please advise

Thanks
Krishna

Tammer SalemTammer Salem
Hi Krishna,
if it's one user have you checked if that user supports the mime type for that attachment? (for example if you send pdf to someone that doesn't have adobe installed it won't open - or associate with the correct application). What type of file is it, and have you ensured the user has the correct application to support that attachement?
Krishna KatveKrishna Katve
Hi Tammer,

Thanks for you quick response.

The file which we are sending via email attachment is a simple text file.

Thanks
Krishna
Tammer SalemTammer Salem
No problem at all - and is the attachment sent as *.txt in the name? (because I suspect that if it isn't the user can't open it). You might want to take a look at the EmailFileAttachment class (specifically the setFileName() an setContentType() methods). https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_attachment.htm
Krishna KatveKrishna Katve

yes the file ext .txt is there.
One more thing actual file size in records notes and attachment​ is less than email attachment.
File in records notes and attachment is 766 bytes and same file sent as email attachment bacame more than 1 mb of unreadable content.
Code Snippet:
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toaddresses);  
mail.setTargetObjectId(userRec.Id);
mail.setWhatId(recordId);
mail.setTemplateId(templateId);
mail.setSaveAsActivity(false);
                 
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
              
List<Attachment> aList  = [select Name, Body,BodyLength from Attachment where ParentId = :recordId];
           
for(Attachment a : aList){    
    System.debug('----a.name'+a.name+'----'+a.BodyLength);
    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
    efa.setFileName(a.Name);
    efa.setBody(a.Body);
    fileAttachments.add(efa);                   
}
Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 
Tammer SalemTammer Salem
I think you're missing a setFileAttachments(fileNames) method. So after your for loop try adding:
mail.setFileAttachements(fileAttachments);


Tammer SalemTammer Salem
Hello Krishna - did that help? Or are you still experiencing the problem?
Krishna KatveKrishna Katve
Hi Tammer,

mail.setFileAttachements(fileAttachments) is aready there, MIssed to add it into this thread.

I dont know if its resolved or not yet because it is hard to reproduce. We had only 2 occurences for this kind of issue.
I have raised case with Salesforce regarding this. Hoping i should get some resolution.

Thanks
Krishna