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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Email multiple attachments automatically

(0) ShareShare
ReportReport
Posted on by 241

8787.jpg6052.jpg

Hi everyone, how are you all?

I'm trying to send an email having multiple attachments to the selected employees to inform them which items are going to be expired after 1, 7, 10, 15, or 30 days depending upon the grace time of an item. I added a field of 'grace time' on the item card page.  Everything is working fine but the only error is when I sent them an email they receive corrupted pdf files. I have tested these files by automatically saving them into C drive. After sending an e-mail, files are corrupted in the e-mail and not opening but the same files are opening into C drive perfectly and data (item records) is also saved in the files. Code and output are also attached. Can you please help me in identifying the problem?

PROCEDURE ReportSendMail(EmailSetup: record EmailSetup)  //passing a record type variable of email setup page where I have selected the                                                                                                                //specific employees 
var
        _CurrentDate: Date;
        _CurrentDatePlusthirtyDays: Date;
        _CurrentDatePlusFiteenDays: Date;
        _CurrentDatePlusTenDays: Date;
        _CurrentDtePlusSavenDays: Date;
        _CurrentDatePlusOneDay: Date;
        _Today: Date;
        _employee: Record Employee;
        _subject: Text;
        _body: Text;
    begin
        _subject := 'Item Expiration';
        _body := 'Dear all recipients, <br>Attached items are about to be expired in 1, 7, 10, 15, or 30 days.';
        _CurrentDate := System.Today();
        CLEAR(Mail);
        i := 1;
        EmailSetup.SetFilter(EmployeeId, _employee."No.");    
        if EmailSetup.FindSet() then begin
            repeat
                if (EmailSetup."Company E-Mail" <> ''then begin
                    ToAddr.Add(EmailSetup."Company E-Mail");
                end;
            until EmailSetup.Next() 0;
        end;                                                 
        SmtpConf.GET();
        Mail.CreateMessage('Business Central Mailing System', SmtpConf."User ID", ToAddr, _subject, _body);
        Evaluate(_CurrentDatePlusthirtyDays, Format((_CurrentDate30))// For thirty days
        Evaluate(_CurrentDatePlusFiteenDays, Format((_CurrentDate15))// For fifteen days 
        Evaluate(_CurrentDatePlusTenDays, Format((_CurrentDate10));    // For ten days
        Evaluate(_CurrentDtePlusSavenDays, Format((_CurrentDate7));    // For 7 days
        Evaluate(_CurrentDatePlusOneDay, Format((_CurrentDate1));      // For 1 day
        _Today := System.Today;
        ItemExp(_CurrentDatePlusthirtyDays, 30);
        ItemExp(_CurrentDatePlusFiteenDays, 15);
        ItemExp(_CurrentDatePlusTenDays, 10);
        ItemExp(_CurrentDtePlusSavenDays, 7);
        ItemExp(_CurrentDatePlusOneDay, 1);
        ItemExp(_Today, 0);
        // For mail send
        Mail.Send();
    end;

    local procedure ItemExp(var transactionDate: Date; Days: Integer)
    begin
        tbl_item.SetFilter("Grace Time", '%1', transactionDate);
        if tbl_item.FindSet() then begin
            Clear(Name);
            FullName[i] := 'C:\Temp\DDCExpreIn ' + Format(Days'.pdf';
            Message(FullName[i]);
            TempBlob.CreateOutStream(outStreamReportAtc[i], TEXTENCODING::UTF8);
            TempBlob.CreateInStream(inStreamReportAtc[i], TEXTENCODING::UTF8);
            // Use the Report.SaveAs method to save the report as a PDF file 
            if Exists('C:\Temp\DDCExpreIn 30.pdf'then
                Erase('C:\Temp\DDCExpreIn 30.pdf');
            if Exists('C:\Temp\DDCExpreIn 15.pdf'then
                Erase('C:\Temp\DDCExpreIn 15.pdf');
            if Exists('C:\Temp\DDCExpreIn 10.pdf'then
                Erase('C:\Temp\DDCExpreIn 10.pdf');
            if Exists('C:\Temp\DDCExpreIn 7.pdf'then
                Erase('C:\Temp\DDCExpreIn 7.pdf');
            if Exists('C:\Temp\DDCExpreIn 1.pdf'then
                Erase('C:\Temp\DDCExpreIn 1.pdf');
            if Exists('C:\Temp\DDCExpreIn 0.pdf'then
                Erase('C:\Temp\DDCExpreIn 0.pdf');

            Content.Create('TestFile' + tbl_item.Description + '.pdf');
            Content.CreateOutStream(outStreamReport);
            Clear(Rpt_CheckItemExpiration);
            Rpt_CheckItemExpiration.SetTableView(tbl_item);
            Rpt_CheckItemExpiration.SaveAsPdf(FullName[i]);
            Content.Close;
            //Clear(Mail);
            Mail.AddAttachmentStream(inStreamReportAtc[i], FullName[i]);
            i := i + 1;
        end;
    end;

    var
        Rpt_CheckItemExpiration: Report CheckItemExpiration;
        tbl_item: Record Item;
        RecdRef: RecordRef;
        Name: Text;
        FullName: Array[10of Text;
        outStreamReport: OutStream;
        inStreamReport: InStream;
        Parameters: Text;
        tempBlob: Codeunit "Temp Blob";
        // TempBlobb: Record TempBlob temporary;
        Base64EncodedString: Text;
        Mail: Codeunit "SMTP Mail";
        SmtpConf: Record "SMTP Mail Setup";
        _CurrentDate: Date;
        _CurrentDatePlusthirtyDays: Date;
        _CurrentDatePlusFiteenDays: Date;
        _CurrentDatePlusTenDays: Date;
        XmlParameters: Text;
        Content: File;
        TempFile: File;
        EmailID: Text;
        ToAddr: List of [Text];
        i: Integer;
        ii: Integer;
        outStreamReportAtc: Array[10of OutStream;
        inStreamReportAtc: Array[10of InStream;
        txt: Text;
I have the same question (0)
  • Suggested answer
    Marco Mels Profile Picture
    on at

    Hello,

    It could be that these files are also located / stored before sending in %appdata%\local\temp and that the system account does not have access to these PDF files.

    Apart from that, we currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

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 2,577

#2
YUN ZHU Profile Picture

YUN ZHU 888 Super User 2025 Season 2

#3
Jainam M. Kothari Profile Picture

Jainam M. Kothari 778 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans