Sending Email (Html Code) - using SMTP

sunnyksunnyk Member Posts: 280
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 Member Posts: 2
    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 Member Posts: 45
    Try "SMTP Mail Setup" / "Test Email Setup" ?
  • sunnyksunnyk Member Posts: 280
    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:= 'myName@CorpXY.com';
    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 NAV_Notifications@abc.com to myname@abc.com 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 Member Posts: 119
    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 Member Posts: 280
    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 Member Posts: 280
    Sorry, but I didn't get what you meant by Same "Side".
  • sunnyksunnyk Member Posts: 280
    if you meant, that i use same Nav_notifications for both sender & recipient, yes it works even through code.
  • sunnyksunnyk Member Posts: 280
    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 Member Posts: 39
    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 Member Posts: 623
    Don't do universal application from NAV. 3rd party applications are often easy to implement for particular tasks.
  • mgnmgn Member Posts: 39
    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.