Skip to main content

Notifications

Microsoft Dynamics NAV forum
Unanswered

I want to sent mail with templates i have already create template table and import template in template list so i want to sent wail according to the template, i have write a code unit for sent mail can you please chek where i do mistake

Posted on by 133

Codes for sent mail

Documentation()

OnRun()

SentMailWithTemplate(CustNo : Code[20])
SMTPSetup.GET;
Customer.GET(CustNo);
Customer.TESTFIELD("E-Mail");
Customer.CALCFIELDS("Balance (LCY)");
IF Customer."Balance (LCY)" = 0 THEN
EXIT;
SMTPMail.CreateMessage('',SMTPSetup."User ID",Customer."E-Mail",'Test1','',TRUE);
IF SalesPerson."E-Mail"<>'' THEN
SMTPMail.AddCC(SalesPerson."E-Mail");
SetTokenToValueOfField(NotificationBody,'%CustomerName%',RecRef,'Name');
SetTokenToValueOfField(NotificationBody,'%CustomerAddress%',RecRef,'Address');
SetTokenToValueOfField(NotificationBody,'%ContactNo%',RecRef,'Contact');

CustLedgEntry.RESET;
CustLedgEntry.SETCURRENTKEY("Customer No.",Open,Positive,"Due Date","Currency Code");
CustLedgEntry.SETRANGE("Document Type",CustLedgEntry."Document Type"::Payment);
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('<tr>');
SMTPMail.AppendBody('<td>'+FORMAT(CustLedgEntry."Document No.")+'</td>');
SMTPMail.AppendBody('<td>'+FORMAT(CustLedgEntry."Posting Date")+'</td>');
SMTPMail.AppendBody('<td>'+FORMAT(CustLedgEntry."Due Date")+'</td>');
SMTPMail.AppendBody('<td>'+FORMAT(CustLedgEntry."Amount (LCY)")+'</td>');
SMTPMail.AppendBody('<td>'+FORMAT(CustLedgEntry."Remaining Amt. (LCY)")+'</td>');
SMTPMail.AppendBody('</tr>');
UNTIL CustLedgEntry.NEXT=0;

SMTPMail.AppendBody('</table>');
SMTPMail.AppendBody('<br><br>');
SMTPMail.AppendBody('Regards,');
SMTPMail.AppendBody('<br>');
SMTPMail.AppendBody('Ajay Pandey');
SMTPMail.AppendBody('<br><br>');
SMTPMail.AppendBody('<HR>');
SMTPMail.AppendBody('This is a system generated mail. Please do not reply to this email ID.');
SMTPMail.Send;

LOCAL SetTokenToValueOfField(VAR NotificationBody : DotNet "System.String";Token : Text;RecRef : RecordRef;FieldName : Text)
Field.SETRANGE(TableNo,RecRef.NUMBER);
Field.SETRANGE(FieldName,FieldName);
IF Field.FINDFIRST THEN BEGIN
FieldRef := RecRef.FIELD(Field."No.");
FieldValue := FORMAT(FieldRef.CAPTION);
END;
NotificationBody := NotificationBody.Replace(Token,FieldValue);

Categories:
  • AjayPandey Profile Picture
    AjayPandey 133 on at
    RE: I want to sent mail with templates i have already create template table and import template in template list so i want to sent wail according to the template, i have write a code unit for sent mail can you please chek where i do mistake

    Codes for import template:-

    Documentation()

    ImportTemplate - OnAction()

    Rec.CALCFIELDS(ImportTemp);

    IF Rec.ImportTemp.HASVALUE THEN

     MESSAGE('Template is alredy Uploaded');

     IF NOT CONFIRM('Do you want to ReUpload?') THEN

       EXIT;

    FileName := Filemgm.BLOBImportWithFilter(TempBlob, 'Upload File', '', 'All Files (*.*)|*.HTML*', '.HTML');

    TempBlob.Blob.CREATEINSTREAM(Ins);

    Rec.ImportTemp.CREATEOUTSTREAM(Outs);

    COPYSTREAM(Outs, Ins);

    IF TempBlob.Length()>5000000 THEN

     ERROR('File size must be less than or equal to 3MB')

    ELSE BEGIN

     Rec.MODIFY(TRUE);

     MESSAGE('File Uploaded');

    END;

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 227,979 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans