Options

Send Email in NAV 2016 with generated Email Body

LilithyanLilithyan Member Posts: 32
Hello all,

we had the (mis-)fortune, that one of our customers took a look at the NAV 2017 Demo and saw the functionality to define an email Body via the Report Selection and the Custom Report Selection. The reaction of course was "Hey, this is cool! We want this in our database, too!".
But...! The customer has a NAV 2016 database (CU4) and currently doesn't want to upgrade to NAV 2017 for one functionality. So they asked us, if it were possible to replacate/copy the one from NAV 2017.

So far, it doesn't seem possible to me, as the the whole functionality is build upon the premiss that the email body is created as html (REPORT.SAVEASHTML). This sadly does not (!) work in NAV 2016 (they took it out cause... reasons?), but it is back in NAV 2017.

Bonusheadache: The whole thing uses new C/AL Functions called IMPORTSTREAMWITHURLACCESS and GETDOCUMENTURL.

Sooooo... is in your opinion any possiblity to make at least the html-functionality work in NAV 2016? Or would you recomend someting along the lines of Extended Text that then gets shoved into the email body?

Bonusquestion: Is it actually possible (like in the good ol' days of classic Client) to only (!) make a "technical" upgrade from NAV 2016 Client to NAV 2017 Client (without upgrading the objects)? Would/Could that be (part) of the solution?

Ciao,

Lils

Comments

  • Options
    vremeni4vremeni4 Member Posts: 323
    Hi,

    There are few options to resolve this .

    1. you can upgrade to NAV 2017. I did a few upgrades to NAV 2017 and in general it does not take that much time. especially as NAV 2016 to NAV 2017 is similar (except cu 80 and 90) With powershell script, you can compare the objects in less then a day, Another day to do data upgrade. In general with testing this can be done in few days time. Except there is a lot of customisation, then it might take longer.
    2. You can install a fake or email NAV 2017 instance and use it from NAV 2016 to send the emails e.g. web services.
    3. You can write a small .NET dll that will do this, There is a lot of code around (google) that does this. So you copy past the code from google in Visual studio test it a bit, create a DLL and use it.
    4. There is some code that was written for NAV 2009 (I used it a lot in NAV 2009 to send these type of html email) that allows you to create an html email in NAV code. You can try to search for it in download section or on google.
    5. I doubled checked in NAV 2016 and SAVEASHTML is available, also SAVEASPDF , SAVEASWORD ...
    Both functions IMPORTSTREAMWITHURLACCESS and GETDOCUMENTURL can be just copied in NAV 2016 and they will work fine.
    In my opinion this should be completely doable in NAV 2016

    I hope this helps.
  • Options
    LilithyanLilithyan Member Posts: 32
    Hi,

    thanks for the info.

    As to the SAVEASHTML. We did multiple tests. While you can add it in the code an not get a compiling error, you'll get the following error when running a function with it:

    Microsoft Dynamics NAV

    The SAVEASHTML() method is obsolete.
    OK

    And when I copy the function with the two other calls I get the following error when compiling:

    Microsoft Dynamics NAV Development Environment
    You have specified an unknown variable.

    IMPORTSTREAMWITHURLACCESS

    Define the variable under 'Global C/AL symbols'.
    OK

    The function in NAV 2017 is called ImageBase64ToUrl and can be found in Codeunit 9520 (Mail Management). Though I'm not even sure the part were teh two calls are made is used much....

    Ciao,

    Lils
  • Options
    vremeni4vremeni4 Member Posts: 323
    Hi,

    You are right the command SAVEASHTML() does not work in NAV 2016.
    Very annoying, I was not aware of it, good they brought it back in NAV 2017.

    Anyhow I looked at the table 77 Report selection and you can still use
    SaveReportAsPDF
    There are converters that you can download and use to convert PDF into HTML.
    for example
    https://code.msdn.microsoft.com/windowsapps/Convert-PDF-file-to-HTML-abb731a5

    I also looked at the function
    ImageBase64ToUrl(BodyText : Text) : Text
    SearchText := '(.*<img src=\")data:image\/[a-z]+;base64,([a-zA-Z0-9\/+=]+)(\".*)';
    Regex := Regex.Regex(SearchText);
    WHILE Regex.IsMatch(BodyText) DO BEGIN
      Base64 := Regex.Replace(BodyText,'$2',1);
      MemoryStream := MemoryStream.MemoryStream(Convert.FromBase64String(Base64));
      MediaId := IMPORTSTREAMWITHURLACCESS(MemoryStream,CREATEGUID,1);
      BodyText := Regex.Replace(BodyText,'$1' + GETDOCUMENTURL(MediaId) + '$3',1);
    END;
    EXIT(BodyText);
    
    This code is only used if there is an image in the body of the email.
    In other words if report does not have an image you do not need this function.
    What it does, It will convert the image into Base64 characters so that the image can be sent as part of the email.
    Take a look at this blog
    https://devio.wordpress.com/2011/01/13/embedding-images-in-html-using-c/
    The variable MediaID only gives it correct link
    e.g. in html file the line
    <img alt="My Image" src="HeaderLogo.png" />
    
    will be replaced with something like
    <img alt="My Image" src="data:image/jpeg;base64,/9j/4S/+RXhpZgAATU0AKgAAAAgACAESAAMAENkDZ5u8/61a+X...more encoding" />
    

    In other words, you can just remove the two commands and write your own code to replace the image in the body with the image in base64 format
    <img alt="My Image" src="data:image/jpeg;base64,/9j/4S/+RXhpZgAATU0AKgAAAAgACAESAAMAENkDZ5u8/61a+X...more encoding" />
    

    It is a bit of work but doable. (if you use .NET fucntion for bse64 conversion, might not be too difficult)

    I hope this helps.
  • Options
    LilithyanLilithyan Member Posts: 32
    Thank you for the help.

    A colleague and I managed to get a work around going. Instead of using SAVEASHTML, we use the SAVEASWORD. When the file is then streamed into the BLOB field for the Body, we simply call up the DotNet for the PDF Writer (Microsoft.Dynamics.Nav.PdfWriter.WordToPdf) an use the method ConvertToHtml. Works astonishingly well.

    Thanks again!

    Ciao,

    Lils
  • Options
    Visi_VVisi_V Member Posts: 8
    Hello @Lilithyan

    Do you got a simple sample code, on how you use the ConvertToHtml function?


    /Visi

Sign In or Register to comment.