function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
venturec3venturec3 

Converting Email Attachment body to Blob so that I can add a file attachment.

Have any of you worked with the EncodingUtil.base64Decode() method? I have a client Carte Financial who is wanting to send an email automatically when a new file attachment has been added. The Body of the file attachment is in Base64 format. But all the examples that I've researched on the Salesforce blogs don't work. My current code is:

 

    for(Attachment attch : attchSubset) {

          

           // Create the attachment

           Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();

           efa.setFileName(attch.Name);

 

Get an error here stating that there is an invalid Base64 character and can’t convert it. If I just add the attachment with the Body in its Salesforce format, it doesn’t show properly.

           // Decrypt the attachment body and then set it.

           Blob body = EncodingUtil.base64Decode(attch.body.ToString());

           efa.setBody(body);

                   

           Messaging.Singleemailmessage mail = new Messaging.Singleemailmessage();

           list<String> emailAddr = new list<String>();

           emailAddr.add(mapContact.get(attch.ParentId).Email);

           mail.setToAddresses(emailAddr);

           mail.setPlainTextBody('Hi!');

           mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

          

           // Send the Email Message

           Messaging.SendEmailResult[] r = Messaging.sendEmail(new  Messaging.Singleemailmessage[] {mail});

    }

Best Answer chosen by Admin (Salesforce Developers) 
venturec3venturec3

I figured it out.

 

I was retrieving the body of the attachment AFTER an insert. When I switched this over to a BEFORE insert, I was able to retrieve the body in legible format.

 

Thanks for the help and replies.

All Answers

Ritesh AswaneyRitesh Aswaney

So

efa.setBody(attch.body);

 

doesn't work ?

venturec3venturec3

The text file (mime type = text/plain) returns as binary in the body of the text file for some reason when using the following line of code for adding the attachment to an email.

 

I also tried setting the content type, but that still doesn't work.

 

           Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();

           efa.setFileName(attch.Name);

          efa.setContentType(attch.ContentType);

           efa.setBody(attch.Body);

 

Unsure at this point why the attachment body isn't converting to text on it's own.

AdyAdy

This worked for me:

 

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.Body = attch.Body;
efa.FileName = attch.Name;
efa.ContentType = attch.ContentType;

 

at the end add it to the EmailFileAttachment array.

venturec3venturec3

I figured it out.

 

I was retrieving the body of the attachment AFTER an insert. When I switched this over to a BEFORE insert, I was able to retrieve the body in legible format.

 

Thanks for the help and replies.

This was selected as the best answer