You need to sign in to do that
Don't have an account?
Aubrey
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
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
Thanks for your reply!
Actually, I did try both attachment.Body and attachment.body.
But I got the same error message.
Thanks
Aubrey
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.
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
Hi Deepak
Did you figure out why the attachments were getting corrupted? I am having the same issue.
Thanks
Hi,
Sorry , but issue is not yet resolved.
Thanks,
Deepak
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
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
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);
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.