web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Email with attachment is not working or Report as PDF is not working

(0) ShareShare
ReportReport
Posted on by 13

Hi

I have a codunit where I wanted to create a report as PDF and attached it to the email.  It is a custom table and custom report.

can anyone suggest a mistake here?

//-----------------------------------------------

             AttachmentTempBlob.CreateOutStream(OutStr);
            Rep50100.GetArgs(mailRec."Customer No", mailRec."Order No", mailRec."Price Date", WorkDate(), '');
            REPORT.SAVEAS(50100, '', REPORTFORMAT::Pdf, OutStr, RecRef);
            AttachmentTempBlob.CreateInStream(AttachmentInStream);
            EmailMessage.AddAttachment(Filename, 'PDF', AttachmentInStream);
            EmailMessage.Create("Send to", Subject, 'Price Notification');
            Email.Send(EmailMessage, Enum::"Email Scenario"::"Gas Price Email");
//---------------------------------------------
Please suggest the corrections
Warm regards
I have the same question (0)
  • Suggested answer
    Govinda Kumar Profile Picture
    2,304 Moderator on at
    RE: Email with attachment is not working or Report as PDF is not working

    Hi hafiz,

    this is the simple code, you test and modify it according to your requirements

            ReportParameters := ReportExample.RunRequestPage();
            TempBlob.CreateOutStream(OutStr);
            Report.SaveAs(Report::"Standard Sales - Invoice", ReportParameters, ReportFormat::Pdf, OutStr);
            TempBlob.CreateInStream(InStr);
    
            EmailMessage.Create('youremail@gmail.com', 'This is the subject', 'This is the body');
            EmailMessage.AddAttachment('FileName.pdf', 'PDF', InStr);
            Email.Send(EmailMessage, Enum::"Email Scenario"::Default);

    Hope it helps

    Regards

  • Hafiz@d365 Profile Picture
    13 on at
    RE: Email with attachment is not working or Report as PDF is not working

    Thank you

    I wanted to send a record as recordset to report 50100. it is a single record report, that is being formatted in the report, which is our attachment.

    this following syntax may not work, last time I tried

     Report.SaveAs(Report::"Standard Sales - Invoice", ReportParameters, ReportFormat::Pdf, OutStr);

  • Suggested answer
    Saurav.Dhyani Profile Picture
    14,380 Super User 2025 Season 2 on at
    RE: Email with attachment is not working or Report as PDF is not working

    Hi,

    Hope this helps.

  • Suggested answer
    Gianfranco C. Profile Picture
    370 on at
    RE: Email with attachment is not working or Report as PDF is not working

    hi, try this code:

            DataTypeMgt.GetRecordRef(mailRec,RecordRef);  //codeunit "Data Type Management"
    
            AttachmentTempBlob.CreateOutStream(OutStr);
            Report.SaveAs(ReportID, '', ReportFormat::Pdf, OutStr, RecordRef);
            AttachmentTempBlob.CreateInStream(InStr);
            EmailMessage.AddAttachment(Filename, 'PDF', InStr);
            EmailMessage.Create("Send to", Subject, 'Price Notification');
            Email.Send(EmailMessage, Enum::"Email Scenario"::"Gas Price Email");

  • ShanAbeywicrema Profile Picture
    948 on at
    RE: Email with attachment is not working or Report as PDF is not working

    Saurav,

    I am using following code to generate pdf , then attach and send email. It is working fine. But problem is few pdf are corrupted. But when I send these pdf individually (not as bulk) then it is working. Again, corrupted file is working fine in all pdf readers except acrobat reader..

    if SalesInvoiceHdr.FindFirst() then

               repeat

                   clear(RecRef);

                   SalesInvHdr.Reset();

                   SalesInvHdr.SetRange("No.", SalesInvoiceHdr."No.");

                   if SalesInvHdr.FindFirst() then

                       RecRef.GetTable(SalesInvHdr);

                   tempBlob.CreateOutStream(outStreamReport);

                   tempBlob.CreateInStream(inStreamReport);

                   body := '';

                   emailMsg.Create('shan.abeywickrema@abcsd.com.au', 'Your invoice ' + SalesInvHdr."No." + ' has been processed', body, true);//SalesInvoiceHdr."Sell-to E-Mail"

                   Report.SaveAs(Report::"Posted Proforma Invoice", '', format::Pdf, outStreamReport, recRef);

                   //txtB64 := cnv64.ToBase64(inStreamReport);

                   emailMsg.AddAttachment('Sales Invoice ' + SalesInvHdr."No." + '.pdf', 'pdf', inStreamReport);

                   email.Send(emailMsg);

               until SalesInvoiceHdr.Next() = 0;

    Do you find any issue with this code?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,377

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 2,696 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,512 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans