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
AubreyAubrey 

Email with attachment problem

Dear Friends

I want to create a html attachment and attach it in email.
I got an exception when setting email attachment
body, the Apex codes like this:
=====================================================================
    // Encode the html string as base64 and then create an attachment  
    Blob html = Crypto.generateDigest('SHA1',Blob.valueOf(htmlBody));
    Attachment attachment = new Attachment();

    // set attachment body - base64, and
name
    attachment.Body = Blob.valueOf(EncodingUtil.base64Encode(html));
    attachment.Name = String.valueOf('Task.html');

    // if WhatId is not null, set the
attachment.ParentId to WhatId
    // else set the
attachment.ParentId to WhoId
    if (newTask.WhatId != null) {
        attachment.ParentId = newTask.WhatId;
    } else {
        attachment.ParentId = newTask.WhoId;
    }
    insert attachment;
   
           // add the
attachment to an email message
line251    Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1];
line252    fileAttachments[0].setBody(attachment.body);
line253    fileAttachments[0].setFileName(attachment.Name);
line254    mail.setFileAttachments(fileAttachments);
=====================================================================
Error message like this:
SendAlertMail: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.SendAlertMail: line 252, column 21

But I can get the attachment.Name after the "insert attachment" operation.
And I can found the created attachment in related Opportunity.
Can anybody give me some advices?

Thanks


JimRaeJimRae
Just a quick thought, but I notice you are using different names for the object.
When you create the attachment body you call it "attachment.Body"
When you add the body to the message you call it "attachment.body".
Might  be a minor thing, but I would check it out.
AubreyAubrey
Dear Jim

Thanks for your reply!
Actually, I did try both attachment.Body and attachment.body.
But I got the same error message.

Thanks
Aubrey
JimRaeJimRae
I think I figured out what your issue was.
Check out this code snippet, and see if it is better:
 
Code:
Blob html = Crypto.generateDigest('SHA1',Blob.valueOf(htmlBody)); 
    Attachment attachment = new Attachment(); 

    // set attachment body - base64, and name 
    attachment.Body = Blob.valueOf(EncodingUtil.base64Encode(html)); 
    attachment.Name = String.valueOf('Task.html'); 

    // if WhatId is not null, set the attachment.ParentId to WhatId 
    // else set the attachment.ParentId to WhoId 
    if (newTask.WhatId != null) { 
        attachment.ParentId = newTask.WhatId; 
    } else { 
        attachment.ParentId = newTask.WhoId; 
    } 
    insert attachment; 
    
           // add the attachment to an email message
Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1];

//create an attachment object to populate with data
Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
fileAttachment.setBody(attachment.body); 
fileAttachment.setFileName(attachment.Name); 

//add attachment object to attachments array
fileAttachments[0]=fileAttachment;

//add attachments to mail message
mail.setFileAttachments(fileAttachments); 

 
AubreyAubrey
Dear Jim

Thanks a lot for your help! It works!
I tried to create an attachment object to populate with data.
But I did it this way:
Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment;
So I got an error, which said there is no
EmailFileAttachment object.
Yours like this:
Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();

Thanks again!
Aubrey

Deepak PansariDeepak Pansari

Hi All,

 

I am using your code. I put a trigger on attachment object, whenever any attachment gets added into Opportunity trigger will send an email with that attachment.

 

I am getting email with attachment but attachment gets corrupted or may be converted into binary code.

 

Attachment types like Txt, Gif files.

 

Plese let me know if i get the files with thier actual type in attachment. 

 

Below is the code

 

----------------------------------- code---------------------

 

Attachment[] newAttachment = Trigger.new;

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

String[] toAddresses = new String[] {'xx@xx.com'};

mail.setToAddresses(toAddresses);

mail.setSubject('Test email');

mail.setHtmlBody('<b>Testing</b>');

Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1];

Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();

fileAttachment.setBody(newAttachment[0].Body);

fileAttachment.setFileName(newAttachment[0].Name); 

fileAttachments[0]=fileAttachment;

mail.setFileAttachments(fileAttachments); 

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

Thanks in advance

Deepak 

KashKash

Hi Deepak

 

Did you figure out why the attachments were getting corrupted? I am having the same issue.

 

Thanks

Deepak PansariDeepak Pansari

Hi,

 

Sorry , but issue is not yet resolved.

 

Thanks,

Deepak 

VjartzmusicVjartzmusic

Hi,

 

If you are doing an after insert please select the record from the database (re-query), if you are using a before insert it will work.

 

Regards

 

Vivek Viswanathan 

VjartzVjartz

Hi,

 

Please re-query the data using if you are using an after insert, if you are using a before insert it should work fine.

 

Regards

 

Vivek

VarunCVarunC

I'm getting the same issue. If I attach more than 1 PDF files as attachment to the Email, it gets attached as HTML file with 400KB size , though the size of PDF file is 2 MB each. If I Attach and Send Only one PDF file at a time then it works fine. PDF file is sent attached as PDF file with correct size ...

 

This really needs to work, I couldn't find anything wrong with my code, here is my code for attaching the Files:

 

Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[] {};
for (Attachment a : [Select Name, Id, Body, BodyLength
From Attachment Where ID IN: att_ids])
{
Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
fileAttachment.setBody(a.Body);
fileAttachment.setFileName(a.Name);
fileAttachments.add(fileAttachment);
}
if ( !fileAttachments.isEmpty() )
mail.setFileAttachments(fileAttachments);

 

 Can anyone suggest corrections here? I need to Read Files From ATTACHMENT object when sending Email, and attach the Read attachments to the Email.

 

 

EDIT: I've found that same code works with Multiple Attachments BUT it breaks with Multiple Attachements if single Attachment size is more than or equal to 2 MB. Can anybody tell me what is the LIMIT on sending Attachments in an Email? Is there any imposed limit on email attachment or Attachment object record? I only knew of 1 LIMIY that is of Max. 10 MB size of Total attachments in an Email.

Message Edited by vchaddha on 12-29-2009 05:50 AM