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.
6h4v8uja8cml.png

Attached is the SMTP setting on our NAV server.
xpi41dfpt0x9.png

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 [KL1PR0601CA0015.apcprd06.prod.outlook.com]".

Best Answer

Answers

  • 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
    Hi,
    Yes, here is the code that we wrote, and we rae taking sender from SMTP setup,
    Purch. Rcpt. Line - OnPreDataItem()
    CLEAR(SMTPMail);
    SMTPSetup.GET;
    //SETRANGE("Posting Date",WORKDATE);
    Recipients:= '[email protected]';
    Subject := 'Expense Receipt Notification' + ': ' + FORMAT(WORKDATE);
    Body := 'Following Items are received on :' + FORMAT(WORKDATE);
    CLEAR(SMTPMail);
    SMTPMail.CreateMessage('XYZ',SMTPSetup."User ID",Recipients,Subject,'',TRUE);
    SMTPMail.AppendBody('Hello,');
    SMTPMail.AppendBody('<br><br>');
    SMTPMail.AppendBody('<h1>'+ Body + '</h1>');
    SMTPMail.AppendBody('<br><br>');
    SMTPMail.AppendBody('<table border="1">');
    SMTPMail.AppendBody('<tr>');
    
    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>Description</th>');
    SMTPMail.AppendBody('<th><b>Quantity</th>');
    SMTPMail.AppendBody('<th><b>Unit Cost(LCY)</th>');
    SMTPMail.AppendBody('<th><b>Amount(LCY)</th>');
    SMTPMail.AppendBody('</tr>');
    
    Purch. Rcpt. Line - OnAfterGetRecord()
    SMTPMail.AppendBody('<tr>');
    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>');
    SMTPMail.AppendBody('</tr>');
    
    Purch. Rcpt. Line - OnPostDataItem()
    SMTPMail.AppendBody('</table>');
    SMTPMail.AppendBody('<br><br>');
    SMTPMail.AppendBody('Thanks ');
    SMTPMail.AppendBody(' ');
    SMTPMail.AppendBody('<br><br>');
    SMTPMail.AppendBody('<hr>');
    SMTPMail.AppendBody('This is system generated mail. Please do not reply to this mail id.');
    SMTPMail.Send;
    
    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
    Hi,
    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
    Hello,
    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: 31Member
    Has anyone found a way to use a html editor in a NAV page?
    I found the following posts, has anyone experience with these?
    https://www.hougaard.com/wysiwyg-editing-control-for-dynamics-nav-2013-and-2015/
    https://mibuso.com/downloads/pawnhtmltexteditor-v1.2
  • lubostlubost Posts: 483Member
    Don't do universal application from NAV. 3rd party applications are often easy to implement for particular tasks.
  • mgnmgn Posts: 31Member
    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.