Sending the content of an HTML in a mail

Hi everyone,

I need to send the content of an URL into the body of an email. In the marketing setup window, I am using CU397

I’ve added an URL. I need to send the content located into that URL. Is this possible?

For example:

olMailItem.HTMLBody := '<a href="http://xxxxx.com/boletin/boletin.html"/a>';       //If we want to send in HTML format
olMailItem.BodyFormat := 2;
olMailItem.Display(ShowNewMailDialogOnSend);    //If we want to display outlook  dialog window
olMailItem.Send;

Of course this is not working, but, could be an approach? Or it’s impossible to do this?

Thank you very much

You will have to download the content of the page, before you can send it as HTML.

Which version of NAV are you running?

In AL you can do a HttpClient.Get() and parse the content of the response to the HTMLBody.

Hi Rasmus, thnaks for your answer. I am running in NAV 2017.

Which type is httpClient variable? A DOTNET?

Thanks again

In 2017, there is a codeunit encapsulating HTTP functionality. It is codeunit 1297 “Http Web Request Mgt.”.

Your code downloading the page content would look similar to this:

TempBlob.Blob.CREATEINSTREAM(ResponseInStream);
HttpWebRequestMgt.Initialize('http://somewebsite.com');
HttpWebRequestMgt.SetUserAgent('Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136');
HttpWebRequestMgt.GetResponse(ResponseInStream,HttpStatusCode,ResponseHeaders);

UserAgent string here imitates the Microsoft Edge browser. Probably, you won’t need this, but some servers can reject an HTTP request if they don’t find a user agent header.

Variables used in the snippet:

TempBlob: Record TempBlob
HttpWebRequestMgt: Codeunit "Http Web Request Mgt."
ResponseInStream: InStream
HttpStatusCode:DotNet System.Net.HttpStatusCode
ResponseHeaders: DotNet System.Collections.Specialized.NameValueCollection

Resulting HTML will be streamed in the ResponseInStream, you can read its data into mail body (I don’t know its type, so can’t suggest anything specific).

But note that this way, you can only send static page content. If the page you read, contains any scripts dynamically updating DOM structure, they will fail almost certainly.

Thanks for your answe Alexander, the code you’ve posted is similar to the one I’ve used yesterday to solve this issue:

//descargamos el fichero HTMl de la URL
lHttpWebRequest := lHttpWebRequest.Create(pURL);
lHttpWebRequest.Method :='GET';
lHttpWebRequest.KeepAlive := TRUE;
lHttpWebRequest.AllowAutoRedirect := TRUE;
lHttpWebRequest.UseDefaultCredentials := TRUE;
lHttpWebRequest.Timeout := 60000;

lTempBLob.INIT;
lTempBLob.Blob.CREATEINSTREAM(lResponseInStream);
lWebRequestHelper.GetWebResponse(lHttpWebRequest,lHttpWebResponse,lResponseInStream,lHttpStatusCode,lResponseHeaders,GUIALLOWED);
lTempBLob.INSERT;
lTempBLob.CALCFIELDS(Blob);

lFileMgt.BLOBExport(lTempBLob,'C:\Users\Public\Documents\boletin\boletin.html',TRUE);


MyFile.OPEN('C:\Users\Public\Documents\boletin\boletin.html');
MyFile.CREATEINSTREAM(StreamInTest);
WHILE NOT StreamInTest.EOS DO BEGIN
  StreamInTest.READTEXT(Buffer);
  WholeBody := WholeBody + Buffer;
END;
MyFile.CLOSE;


olMailItem.HTMLBody := WholeBody;       //If we want to send in HTML format
olMailItem.BodyFormat := 2;
olMailItem.Display(ShowNewMailDialogOnSend);    //If we want to display outlook  dialog window
olMailItem.Send;

Variables:

StreamInTest	InStream		
Buffer	Text		
lHttpWebRequest	DotNet	System.Net.HttpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
lTempBLob	Record	TempBlob	
lResponseInStream	InStream		
lWebRequestHelper	Codeunit	Web Request Helper	
lHttpWebResponse	DotNet	System.Net.HttpWebResponse.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
lHttpStatusCode	DotNet	System.Net.HttpStatusCode.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
lResponseHeaders	DotNet	System.Collections.Specialized.NameValueCollection.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
lFileMgt	Codeunit	File Management	

Thanks again!

Hi again,

I’ve just found another problem with this…

Is it possible to add to the body of the mail content that it’s no in HTML, apparte from the already added HTML?

Thank you very much

You mean adding a “plain text” and a “html” version of the same email?

Yes, that’s it. Seems that It isn’t allowed…

Yes, it’s “either - or”. Either you send HTML content, or plain text. You can, of course, extract text content from your html (with xPath, fror example), but then you’ll have to wrap it into another html tag inside the mail body - some block imitating the plain text.

If it’s worth the efort or not, depends on what you want to see in the end.

Thanks Aklexander, that’s what I expected… Now I have to see if it’s worth or not, as you’ve said.

THnaks again, really appreciated!!!

Hello again,

I’ve just want to improve a little my development. The HTML page is opened from “file:///C:/Users/Administrador.SRV-DC01/Appdata/Local/Temp/2/Microsoft%20Dynamics%20NAV/22012/boletinv14.html”, and I need to close it manually. As you can see, it is the temporary file, no the one I download to the server.

Which sentence is the one that forces the open? How can I do this without openning the file into the navigator?

Thank you very much

solution: Must change this

lFileMgt.BLOBExport(lTempBLob,'C:\Users\Public\Documents\boletin\boletin.html',TRUE);

To this

lFileMgt.BLOBExport(lTempBLob,'C:\Users\Public\Documents\boletin\boletin.html',FALSE);