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...
Unanswered

Help :How made Code unit compatible for AL for sending mail

(0) ShareShare
ReportReport
Posted on by 238

Hi experts 

I have code unit from NAV 2016 which custom code unit .I used CAL to AL converter To get AL file .but now it showing all errors as many functionality,table ,CU related to mail depreciated  from NAV to BC. I am not getting how to move forward Any help will Really appreciated Thanks in Advance!
I have shared code unit Which Converted in AL  

codeunit 50001 "Send Mail"
{
    trigger OnRun()
    begin
    end;
    var
        SMTPMail: Codeunit "400";
        SMTPSetup: Record "409";
    procedure SendMailToCust(CustNo: Code[20])
    var
        Customer: Record 18;
        CustLedgEntry: Record 21;
        SalesPerson: Record 13;
    begin
        SMTPSetup.GET;
        Customer.GET(CustNo);
        Customer.TESTFIELD("E-Mail");   
        SMTPMail.CreateMessage(SMTPSetup."Email Sendor Name", SMTPSetup."Email Sendor Email",
                               Customer."E-Mail", 'Pending Payments', '', TRUE);

        SalesPerson.GET(Customer."Salesperson Code");
        IF SalesPerson."E-Mail" <> '' THEN
            SMTPMail.AddCC(SalesPerson."E-Mail");
        SMTPMail.AppendBody('Dear Sir / Madam,');
        SMTPMail.AppendBody('

'); SMTPMail.AppendBody('Please find Pending Payments List.'); SMTPMail.AppendBody('

'); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); CustLedgEntry.SETCURRENTKEY("Customer No.", Open, Positive, "Due Date", "Currency Code"); CustLedgEntry.SETRANGE(CustLedgEntry."Customer No.", CustNo); CustLedgEntry.SETRANGE(Open, TRUE); CustLedgEntry.SETRANGE(CustLedgEntry."Document Type", CustLedgEntry."Document Type"::Invoice); IF CustLedgEntry.FINDFIRST THEN REPEAT CustLedgEntry.CALCFIELDS("Amount (LCY)"); CustLedgEntry.CALCFIELDS("Remaining Amt. (LCY)"); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); SMTPMail.AppendBody(''); UNTIL (CustLedgEntry.NEXT = 0); SMTPMail.AppendBody('
Invoice No.Invoice DateDue DateInvoice AmountBalance Amount
' FORMAT(CustLedgEntry."Document No.") '' FORMAT(CustLedgEntry."Posting Date") '' FORMAT(CustLedgEntry."Due Date") '' FORMAT(CustLedgEntry."Amount (LCY)") '' FORMAT(CustLedgEntry."Remaining Amt. (LCY)") '
'); SMTPMail.AppendBody('

'); SMTPMail.AppendBody('Regards,'); SMTPMail.AppendBody('
'); SMTPMail.AppendBody('Admin'); SMTPMail.AppendBody('

'); SMTPMail.AppendBody('
'); SMTPMail.AppendBody('This is a system generated mail. Please do not reply to this email ID.'); SMTPMail.Send; end; procedure SendTestMailtoCust(CNo: Code[20]) var Cust: Record Customer; begin Cust.GET(CNo); SMTPSetup.GET; SMTPMail.CreateMessage(SMTPSetup."Email Sendor Name", SMTPSetup."Email Sendor Email", Cust."E-Mail", 'Test Mail', 'This the the mail Body', TRUE); SMTPMail.Send; end; procedure SendMailToCustWithAttachment(CustNo: Code[20]) var Customer: Record Customer; CustLedgEntry: Record 21; SalesPerson: Record 13; // StatementINPDF: Codeunit "50000"; begin SMTPSetup.GET; Customer.GET(CustNo); Customer.TESTFIELD("E-Mail"); SMTPMail.CreateMessage(SMTPSetup."Email Sendor Name", SMTPSetup."Email Sendor Email", Customer."E-Mail", 'Sales Statistics', '', TRUE); SalesPerson.GET(Customer."Salesperson Code"); IF SalesPerson."E-Mail" <> '' THEN SMTPMail.AddCC(SalesPerson."E-Mail"); SMTPMail.AppendBody('Dear Sir / Madam,'); SMTPMail.AppendBody('

'); SMTPMail.AppendBody('Please find Attached Sales Statistics.'); SMTPMail.AppendBody('

'); SMTPMail.AppendBody('Regards,'); SMTPMail.AppendBody('
'); SMTPMail.AppendBody('Admin'); SMTPMail.AppendBody('

'); SMTPMail.AppendBody('
'); SMTPMail.AppendBody('This is a system generated mail. Please do not reply to this email ID.'); SMTPMail.AddAttachment(SMTPSetup."Path to Save Report", ''); SMTPMail.Send; end; }
[View:/cfs-file/__key/communityserver-discussions-components-files/758/SendMail.Codeunit.al:320:240

I have the same question (0)
  • AJ-21080818-0 Profile Picture
    60 on at
    RE: Help :How made Code unit compatible for AL for sending mail

    Hi,

    There is Email and "Email Message" codeunits that can be used instead of your outdated codeunit.

    Below is a sample of simple email sending birthday message. Check their methods if you can append lines in the body.

    var

    Email: Codeunit Email;

    EmailMessage: Codeunit "Email Message";

    Subject:='Hello';

    ToRecipients:='johnsmith@abc.com';

    company:=ReportsCode.getcompname;

    Body:='Dear ' + 'John Smith, <br><br>' + 'On behalf of the company wish you very happy birthday! <br><br>'+ company+'<br> HR Department';

    EmailMessage.Create(ToRecipients,Subject, Body, true);

    Email.Send(EmailMessage);

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,705

#2
Sumit Singh Profile Picture

Sumit Singh 2,015

#3
YUN ZHU Profile Picture

YUN ZHU 2,004 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans