You need to sign in to do that
Don't have an account?

Read Email Attachment In Apex Class
Hi
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.
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.
http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_classes_restful_encodingUtil.htm
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!!
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.
Code
---------
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
bodystr += EncodingUtil.base64Encode(bAttachment.body);
System.debug(LoggingLevel.INFO, bodystr);
}
Debug
-----------
Debug is showing following result:
17:11:49.073 (73039962)|USER_DEBUG|[25]|INFO|IjE0LzAzLzI0IDA3OjM0OjU5IiwieGJ1Y28wMDEiLCJNTVAyUzE0NFsyNjg0M106IFtJRCA2ODI3MTAgbG9jYWw1LmluZm9dIDI
I want to read the attachment file in email receive by salesforce email service.
Attachment file is an ANSI format and text file.
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.
Regards
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.
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;
}
}