Save Eml to .html and .txt

Dim MyMessage As MailMessage = MailMessage.Load(blah, opt)
How to save the message (MyMessage.Save) as the following 3 formats?

  1. Save HTML format single file (.html with embedded Base64 encoded related files)
    Is HtmlSaveOptions.ResourceRenderingMode = ResourceRenderingMode.EmbedIntoHtml the same as my requirement???

  2. Save HTML And Related Files in a folder beside the html file:
    ie, the file is saved as D:\index.htm , related files are under: D:\index_files
    Tried ResourceRenderingMode.SaveToFile and ResourceRenderingMode.SubstituteFromFile with no luck!

  3. Save Plain Text as .txt with this logic:
    If loaded email message has plainText part, save that as .txt
    If loaded email message has NO plainText part, generate text from HTML or RTF part and save.
    (Found this but not going to use Words)
    C# API to Export EML to TEXT | products.aspose.com

Thanks :slight_smile:

@australian.dev.nerds

Your understanding is correct.

Please share the input EML along with problematic and expected output HTML files for testing.

Please share the input EML along with problematic and expected output TXT files for testing.

Hello,
Thanks, not email specific question, simply asking:

  1. save MailMessage to html with images and resources in a folder beside the html file (not embedding the images and resources to the HTML)

  2. save MailMessage to plain text as .txt if MailMessage.Save allows it?
    If not, I will just save the MailMessage.Body myself.
    Thanks

@australian.dev.nerds

You can use use this property to achieve your requirement. If this property does not work at your end, please share the input and output files here for testing.

Please read the following article to achieve your requirement.
Export EML to TEXT via .NET

Well those 2 properties saved the .html files but not the resources folder beside it as I mentioned.

This page:

The following descriptions need to be clarified:

1st of all, the help files and documents should be written by a native English speaker; or at least use a spell AND grammar checker like Grammarly, I’ve seen mistakes around the documents, plus inability to describe the meanings.

SaveToFile (2) Saving resources to file and inserting in html src tag with path to this file.

  • Resources to file or files? (I mean hopefully you’re not zipping them all in one file?) Resources of html page can be multiple picture and css, etc files, anyhow, not mentioned where to save or how to set the save path?

SubstituteFromFile (3) Using custom file as icon and inserting in html src tag with path to this file.

  • When saving a html use custom file as icon?! What is the author talking about? :smiley:

Combining these descriptions and a sample email request makes me wonder; just please take a look at the snapshot to see how is .html expected to be saved, worldwide!
Untitled.png (18.9 KB)

I also mentioned the same link in my 1st post in this thread, not useful for me as it uses Aspose Words/Doc, not going to enter that area…

Can Use:
File.WriteAllText(Target, MyMessage.Body)
But thought there might be:
MyMessage.Save(Target, Kinda Text Format option)

Hey, seems that Message.Body will generate and add URI for Img tags and A href, can this be disabled?

@australian.dev.nerds

As requested earlier, please share the input and output documents along with simplified code example that you are using. We will use the same document for testing. Thanks for your cooperation.

Unfortunately, we did not get your requirement. Could you please share some more detail on it?

Please check the following code example to get an idea about this option.

public void test()
{
    var options = new HtmlSaveOptions();
    options.ResourceHtmlRendering += SetIconCaption;
    options.ResourceRenderingMode = ResourceRenderingMode.SubstituteFromFile;

    string fileName ="Message1.MSG";
    var mailMessage = MailMessage.Load(fileName);
    mailMessage.Save(fileName + ".html", options);
}

private static void SetIconCaption(object sender, ResourceHtmlRenderingEventArgs e)
{
    AttachmentBase attachment = sender as AttachmentBase;
    e.Caption = attachment.ContentType.Name;
    if (attachment.ContentType.Name.EndsWith(".pdf"))
e.PathToResourceFile = "pdf_icon.png";
    else if (attachment.ContentType.Name.EndsWith(".docx"))
e.PathToResourceFile = "word_icon.jpg";
    else if (attachment.ContentType.Name.EndsWith(".jpg"))
e.PathToResourceFile = "jpeg_icon.png";
    else
e.PathToResourceFile = "not_found_icon.png";
}

We have logged this feature request in our issue tracking system as EMAILNET-40804.

I guess ResourceRenderingMode is shared with some other products/parts so options 2 and 3 are not related to the saving html body of mime emails, interesting options but not related to my specific usage and confused me as hell :smiley:

Thanks for the clarification, anyway still don’t know how to extract the html and related files to disk from .eml
(I have no use for it, just am reviewing and testing things)

@australian.dev.nerds

Please read the following articles about extracting content from email message.
Extracting Message Contents from Emails
Extracting Embedded Objects

If you share your input EML and expected output files, we will then provide you more detail about this query.