I am working on a class with an invocable method to send an email with pdf attachment. Currently I am able to send the email correctly, however the attachment comes through as a .bin in outlook365 or noname in gmail. My class is as follows:

public class SendVF_Email_InvocableMethod {

    @InvocableMethod(label = 'Send an email from apex class'description= 'sends an email')
    public static void sendEmailWithAttachment(List<id> listofInvoice) {
        for (Id Invoiceid : listofInvoice) {

            PageReference pref = page.selected_invoice_pdf;
            pref.getParameters().put('id', (Id) Invoiceid);

            Attachment attachment = new Attachment();
            Blob b = pref.getContentAsPDF();
            attachment.Body = b;
            attachment.Name = Datetime.now().format('yyyy-MM-dd HH:mm') + ' ' + 'Invoice' + '.pdf';
            attachment.IsPrivate = false;
            attachment.ParentId = Invoiceid;
            insert attachment;

            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            semail.setSubject('Invoice Issued');
            String[] emailIds = new String[]{};
            for (Invoice__c i : [
            SELECT Billing_Contact_Email__c
            FROM Invoice__c]){
            String s = String.join(emailIds, ', ');

            String[] ccAddresses = new String[]{'someaddress@email.com'};
            for (Invoice__c i2 : [
            SELECT Billing_Contact_Email_2__c
            FROM Invoice__c]){
            String s2 = String.join(ccAddresses, ', ');

            semail.setHTMLBody('<p>Hi' + Invoice__c.Billing_Contact_Name__c +',</p><p>Attached please find your invoice from Company for your review. If you have any questions please contact billing@email.com<br /> <br />Thank you for your business!<br /> <br />Sincerely,<br />analytic.li Billing Team<br /> <br />P: (XXX) XXX-XXXX<br />E: billing@email.com</p>');
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});

  • 1
    I think you are missing attach.setFileName(name of your file.pdf). This will make sure that the file attached is of the specified type. – Jayant Das Jul 25 '18 at 15:18
  • Great that it worked. I will put that as an answer if it helps anyone. – Jayant Das Jul 25 '18 at 18:03

Looking at your code, you are missing the call to set the file name. Providing the filename will make sure the right extension is mentioned and that type of file is sent out.

Adding this line will work: attach.setFileName(name of your file.pdf). So your code will look more like as below:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.