Skip to main content

Notifications

Business Central forum
Suggested answer

Issue with sending email

Posted on by 50

I have tried to schedule a job in job queue for sending an email, But the mail does not send.

Here is mycode

codeunit 50101 MyCodeunit
{
    trigger OnRun()
    begin
        Salesperson.Reset();
        Salesperson.SetFilter("E-Mail", '<>%1', '');
        If Salesperson.FindSet() then
            repeat
                RecSalesInv.Reset();
                RecSalesInv.SetRange("Salesperson Code", Salesperson.Code);
                RecSalesInv.SetRange("Posting Date", WorkDate());
                if RecSalesInv.FindSet() then
                    if RecSalesInv.Count > 0 then
                        Smail(RecSalesInv.Count, "Salesperson"."E-Mail", RecSalesInv);
            until Salesperson.Next() = 0
    end;


    procedure Smail(InvCount: Integer; SentTo: Text; var RecSalesInv_p: Record "Sales Invoice Header")
    var
        TemplineMail: Record "Email Item" temporary;
        TemplineScanario: Enum "Email Scenario";
        SubjectTxt: Label 'Daily invoice report';

        BodyTxt: Label 'Dear %1, <br><br> You have a posted %2 invoices on %3 <br><br><b>Total Invoice Amount :</b> %4 .<br> Please find the details below<br><br>';
        StyleText: Text;
        HeaderTxt: Text;
        coText: Text;
        EndText: Text;
        TableText: Text;
        TotalText: Text;
        TotalInvoiceAmount: Decimal;
    begin
        TemplineMail.Initialize();
        TemplineMail.Subject := SubjectTxt;
        RecSalesInv_p.SetAutoCalcFields("Amount Including VAT");
       

       
        StyleText := '<style>table, th, td {border:1px solid black;}</style>';
        HeaderTxt := '<table><b><tr><th>Document No.</th><th>Customer Name</th><th>Total Amount</th></b></tr>';
        TableText := '';
        TotalText := '';
        TotalInvoiceAmount := 0;
        If RecSalesInv_p.FindSet() then
            repeat
                TableText := '<tr><td>' + RecSalesInv_p."No." + '</td><td>' + RecSalesInv_p."Sell-to Customer Name" + '</td><td>' + Format(RecSalesInv_p."Amount Including VAT") + '</td></tr>';
                TotalText += TableText;
                TotalInvoiceAmount := TotalInvoiceAmount + RecSalesInv_p."Amount Including VAT";
            until RecSalesInv_p.Next() = 0;
        EndText := '</table> <br><br>Regards,<br><br>' + CompanyName;
       
        TemplineMail.SetBodyText(coText + StyleText + HeaderTxt + TotalText + EndText);
        TemplineMail."Send to" := SentTo;
        TemplineMail.SendAsHTML(true);
        TemplineMail.Send(true, TemplineScanario::Default);
        Sleep(1000);
    end;

    var
        myInt: Integer;
        RecSalesInv: Record "Sales Invoice Header";
        Salesperson: Record "Salesperson/Purchaser";
}
Categories:
  • venkadachalam Profile Picture
    venkadachalam 50 on at
    RE: Issue with sending email

    Thank you for your reply,

    I tried using No of minute between runs, but the mail does not send.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,023 Moderator on at
    RE: Issue with sending email

    Hi,

    Did you setup the Jop queue scheduling with No. of minutes. see below

    pastedimage1669215059363v1.png

  • venkadachalam Profile Picture
    venkadachalam 50 on at
    RE: Issue with sending email

    In Job Queue Entries, When I tried runsonce(foreground), the mail was sent. But while schedule a job, the mail does not send.

    Please advice

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,469 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans