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?
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;
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.
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?