+ Start a Discussion
Admin OffshoreAdmin Offshore 

Read Email Attachment In Apex Class


I want to read Email Inbound attachment file  in apex class and create new object's records.
Since the file is in ANSI Format it is considered as binary attachment even if the file is a text file.

I want to read the body of the file.

I have checked that Apex Class version 19.0  can able to read the Blob by Blob.toString() method, but greater version cannot able to read. It is throwing exception as "BLOB is not a valid UTF-8 string".

I want to use ANSI file foramt as attachment since these files are coming from different system automatically.

Please help me to read out the content of email inbound attachment file.
Did you try using EncodigUtil class of salesforce to.If not go through below link for the same :-

Sai Ram ASai Ram A
Try this 

public static Attachment CreateAttachment(Id ParentRecordId)
Blob bodyOfAttachment = EncodingUtil.base64Decode('JVBERi0xLjIgCiXi4');
Attachment att = new Attachment(Name = 'Test.pdf', ParentId = ParentRecordId, Body = bodyOfAttachment);
System.debug('============body = '+att.body);
insert att;
return att;


Hope this Helps you!!
Admin OffshoreAdmin Offshore
Hi Vinit

I have tried EncodigUtil class, but it is not working properly.Since the file is actually a text file and while receiving, email service of salesforce is considered it as a binary file.
As per the scenario the result of EncodingUtil is strange. 

for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
   bodystr += EncodingUtil.base64Encode(bAttachment.body);
   System.debug(LoggingLevel.INFO, bodystr);

Debug is showing following result:
17:11:49.073 (73039962)|USER_DEBUG|[25]|INFO|IjE0LzAzLzI0IDA3OjM0OjU5IiwieGJ1Y28wMDEiLCJNTVAyUzE0NFsyNjg0M106IFtJRCA2ODI3MTAgbG9jYWw1LmluZm9dIDI

Admin OffshoreAdmin Offshore
Hi BLearn

I want to read the attachment file in email receive by salesforce email service. 
Attachment file is an ANSI format and text file.
Try below code :--
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
	    Attachment attachment = new Attachment();
        // attach to the newly created contact record
        attachment.ParentId = contact.Id;
        attachment.Name = bAttachment.filename;
        attachment.Body = bAttachment.body;
        insert attachment;
If this helps,please mark it as best answer to help others :)
HEllo Vinit, ur code works for reading attachment. But how can we create records from the attachment which we get from email service? Can u guide me.
Mohit Bansal6Mohit Bansal6
Hi Bittus

As you got the attachment and its body, now you can traverse the content of body. You can split each line of code with '\n' and as per content you can perform your action.

Mohit Bansal

In case, you face any issue, drop me message on forum or Skype me @mohit_bansal17, if you need any help.

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
Hi Mohit

I did not get what u said. actually i need to generate all the records which we may have in attachment that we get from inbound mail.
Here is my code from which i got the attachment using inbound mail. And am not understanding how to fulfil the requirement.

global class ContactEmailservice implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    Contact contact = new Contact();
    contact.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
    contact.LastName = email.fromname.substring(email.fromname.indexOf(' '));
    contact.Email = envelope.fromAddress;
    insert contact;

    System.debug('====> Created contact '+contact.Id);

    Attachment attachment = new Attachment();
    attachment.ParentId = contact.Id;
    attachment.Name = email.subject;
    attachment.Body = Blob.valueOf(email.HtmlBody);
    insert attachment;
    return result;