Sending Email (Html Code) - using SMTP

sunnyksunnyk Posts: 274Member
Hello, We created a New report and wrote the attached Code to send Email certain data to one user.

Attached is the SMTP setting on our NAV server.

However, we executed the report, we got the error
Microsoft Dynamics NAV

The SMTP mail system returned the following error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM []".

Best Answer


  • thomasronnthomasronn Posts: 1Member
    The user in smtp setup can't send on behalf of the sender you have in SMTPMail.CreateMessage()

    Try set the sender in SMTP.Message() to NAV_Notification[what's behind red box] to test if can can send at all. if success then set up permissions to send as or send on behalf in office365
  • JohnHunterJohnHunter Posts: 45Member
    Try "SMTP Mail Setup" / "Test Email Setup" ?
  • sunnyksunnyk Posts: 274Member
    Yes, here is the code that we wrote, and we rae taking sender from SMTP setup,
    Purch. Rcpt. Line - OnPreDataItem()
    //SETRANGE("Posting Date",WORKDATE);
    Recipients:= '[email protected]';
    Subject := 'Expense Receipt Notification' + ': ' + FORMAT(WORKDATE);
    Body := 'Following Items are received on :' + FORMAT(WORKDATE);
    SMTPMail.CreateMessage('XYZ',SMTPSetup."User ID",Recipients,Subject,'',TRUE);
    SMTPMail.AppendBody('<h1>'+ Body + '</h1>');
    SMTPMail.AppendBody('<table border="1">');
    SMTPMail.AppendBody('<th><b>Document No.</th>');
    SMTPMail.AppendBody('<th><b>Order No.</th>');
    SMTPMail.AppendBody('<th><b>Item No.</th>');
    SMTPMail.AppendBody('<th><b>Unit Cost(LCY)</th>');
    Purch. Rcpt. Line - OnAfterGetRecord()
    SMTPMail.AppendBody('<td>' + FORMAT("Document No.") + '</td>');
    SMTPMail.AppendBody('<td>' + FORMAT("Order No.") + '</td>');
    SMTPMail.AppendBody('<td>' + FORMAT("No.") + '</td>');
    SMTPMail.AppendBody('<td>' + FORMAT(Description) + '</td>');
    SMTPMail.AppendBody('<td>' + FORMAT(Quantity) + '</td>');
    SMTPMail.AppendBody('<td>' + FORMAT("Unit Cost (LCY)") + '</td>');
    SMTPMail.AppendBody('<td>' + FORMAT("Unit Cost (LCY)" * Quantity) + '</td>');
    Purch. Rcpt. Line - OnPostDataItem()
    SMTPMail.AppendBody('Thanks ');
    SMTPMail.AppendBody(' ');
    SMTPMail.AppendBody('This is system generated mail. Please do not reply to this mail id.');
    if i replace [email protected] to [email protected] in SMTP setup and update my password, this works. but how this NAV_notifications is able to send PO and SO approval emails within NAV and not like this?
  • HannesHolstHannesHolst Posts: 119Member
    When your Email address works, then is either the password wrong or there is a problem with the settings of the Mail-account in O365.
  • sunnyksunnyk Posts: 274Member
    The password is correct, because when I test nav_notification from SMTP setup using test email setup and send email to itself i.e. nav_notification it's working but when I choose other and give some other email address, it fails. But how this particular id Nav_notification is able to send Purchase orders/Sales Orders approval and rejection notifications to users. Also, when I Use my Id on SMTP setup and try to send mail using Test Email setup to any other address, that also fails, but using the code it works.
  • sunnyksunnyk Posts: 274Member
    Sorry, but I didn't get what you meant by Same "Side".
  • sunnyksunnyk Posts: 274Member
    if you meant, that i use same Nav_notifications for both sender & recipient, yes it works even through code.
  • sunnyksunnyk Posts: 274Member
    This is resolved. Instead of using SMTP user ID(which is actually an office 365 EMail ID), i use a User id from User setup whose email is SMTP UserID.
  • mgnmgn Posts: 32Member
    Has anyone found a way to use a html editor in a NAV page?
    I found the following posts, has anyone experience with these?
  • lubostlubost Posts: 492Member
    Don't do universal application from NAV. 3rd party applications are often easy to implement for particular tasks.
  • mgnmgn Posts: 32Member
    lubost wrote: »
    Don't do universal application from NAV. 3rd party applications are often easy to implement for particular tasks.

    Do you have any suggestions?
Sign In or Register to comment.