Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

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

Posted on by 83

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
Categories:
  • ShanAbeywicrema Profile Picture
    ShanAbeywicrema 940 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?

  • Suggested answer
    Gianfranco C. Profile Picture
    Gianfranco C. 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");

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

    Hi,

    Hope this helps.

  • Hafiz@d365 Profile Picture
    Hafiz@d365 83 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
    Govinda Kumar Profile Picture
    Govinda Kumar 2,203 Super User 2024 Season 1 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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans