Free Support Forum - aspose.com

How to Assign HTMLBody From HTML File

Hello Team,

I’m trying to search and highlight text content in HTMLBody using Aspose.Email and I believe there is no direct function to do so, I tried some workaround to achieve this functionality but I didn’t get the expected result, so now what I have done I took HTMLBody Content from MSG File and then I load that content in Aspose.Word and search the text and highlight and its working like a charm for me, now issue is I’m not able to reassign that HTML content which is processed by Aspose.Word maybe because the structure has been changed, my goal is to search and highlight content in msg file with having HTMLBody

Any insight would be highly appreciated,

Thanks

@zinzuwadia.hotmail,

You may try using following sample code on your end. You can export the Message to default HTML and then make changes using Aspose.Words. Later you can make modifications and save the file back to HTML and import that in body of message.

           public static void TestHTMLBody()
        {
            MailMessage mailMessage = MailMessage.Load("test.msg");
            mailMessage.Save("ExtractedBody.html", SaveOptions.DefaultHtml);
            //
            //Now Load using Aspose.Words and perform desired action and save the new file BodyModifiedByAsposeWords.html as modified html
            //
            mailMessage.HtmlBody = File.ReadAllText("BodyModifiedByAsposeWords.html");
            mailMessage.Save("saved.msg", SaveOptions.DefaultMsgUnicode);
        }

Thanks for the quick reply, your suggested approach is working :innocent:

But there is still one issue I’m facing if there is an image in HTMLBody then I lost that image content at the time of reassignment of HTML Body,

Please find attached files for the same.

TestFiles.zip (34.0 KB)

@zinzuwadia.hotmail,

The file OUTPUT-ModifiedByAsposeWord,html has been created by Aspose.Words? The file ORIGINALSavedAsHtml.html has been created using Aspose.Email and has image inside it. The image seems to lose when you saved the file using Aspose.Words. I request you to please share the complete sample code that you have used so that I may investigate the issue further on our end.

OUTPUT-ModifiedByAspsoeWord.html has been created using Aspose.Words, what i have observed when we load HTML file into Aspose.Word it extract the image at physical location

        Aspose.Words.License licenseDocx = new Aspose.Words.License();
        licenseDocx.SetLicense("Aspose.Total.lic");

        Aspose.Email.License licenseEML = new Aspose.Email.License();
        licenseEML.SetLicense("Aspose.Total.lic");



        string msgFile = @"W:\Aspose\ORIGINAL.msg";
        string htmlFile = Path.ChangeExtension(msgFile, ".html");

        MailMessage mailMessage = MailMessage.Load(msgFile);
        mailMessage.Save(htmlFile, Aspose.Email.SaveOptions.DefaultHtml);

        Document doc = new Document(htmlFile);
        doc.Document.Range.Replace("Trilokbhai", "Nilesh");
        string htmlOutPutFile = @"W:\Aspose\ORIGINALByAsposeWord.html";
        doc.Save(htmlOutPutFile, SaveFormat.Html);

        string finalMsg = @"W:\Aspose\FinalORIGINAL.msg";
        mailMessage.HtmlBody = File.ReadAllText(htmlOutPutFile);
        mailMessage.Save(finalMsg, Aspose.Email.SaveOptions.DefaultMsgUnicode);

Please check attached zip file it contains input source file and Program.cs file

AsposeTest.zip (14.4 KB)

Thanks

@zinzuwadia.hotmail

We have tested the scenario using the latest version of Aspose.Words for .NET 20.8 and Aspose.Email for .NET 20.7.0. The output generated by Aspose.Words is correct.

The image is lost when HTML body of .msg is set. You can export HTML with Base64 images using Aspose.Words to get the desired output. Please check the following code example.

string msgFile = MyDir + @"ORIGINAL.msg";
string htmlFile = Path.ChangeExtension(msgFile, ".html");

MailMessage mailMessage = MailMessage.Load(msgFile);
mailMessage.Save(htmlFile, Aspose.Email.SaveOptions.DefaultHtml);

Document doc = new Document(htmlFile);
doc.Document.Range.Replace("Trilokbhai", "Nilesh");
string htmlOutPutFile = MyDir + @"ORIGINALByAsposeWord.html";

Aspose.Words.Saving.HtmlSaveOptions options = new Aspose.Words.Saving.HtmlSaveOptions();
options.ExportImagesAsBase64 = true;
doc.Save(htmlOutPutFile, options);

string finalMsg = MyDir + @"FinalORIGINAL.msg";
mailMessage.HtmlBody = File.ReadAllText(htmlOutPutFile);
mailMessage.Save(finalMsg, Aspose.Email.SaveOptions.DefaultMsgUnicode);

We have noticed that the image has black background in FinalORIGINAL.msg. We are investigating this issue and will get back to you soon

Can we achieve this same functionality using Aspose.HTML, i mean instead of Aspose.Word we use Aspose.HTML

@zinzuwadia.hotmail

Please try using following code snippet in order to replace the text using Aspose.HTML for .NET:

using (var document = new Aspose.Html.HTMLDocument(htmlFile))
{
 document.DocumentElement.InnerHTML = document.DocumentElement.InnerHTML.Replace("Trilokbhai", "Nilesh");
 document.Save(htmlOutPutFile);
}

Thanks For the quick reply, its working fine , is it possible to find any word and highlight that word using aspose.html,

@zinzuwadia.hotmail

In order to highlight the word, you can use inline style like in following code snippet:

using (var document = new Aspose.Html.HTMLDocument(htmlFile))
{
 document.DocumentElement.InnerHTML = document.DocumentElement.InnerHTML.Replace("Trilokbhai", "<span style='background-color: yellow;'>Nilesh</span>");
 document.Save(htmlOutPutFile);
}