+2 votes

Hi!

I searched the examples that you provide for the Mail assembly, but I could not find an example of MSG to HTML conversion. Do you have such an example on how to use the methods for VB?

by

1 Answer

0 votes
 
Best answer

.msg is a file format that is used to represent emails. Each email can include body in several formats: plain text, html, rtf and others. More than one format can be used for a single mail message, and it is not required for an email to have an HTML content.

The code to parse Outlook's msg file is simple:

VB.NET:

Using converter As New MsgConverter("c:\outlook1.msg")
    If converter.Type = MsgType.Note Then
        Dim email As IMail = converter.CreateMessage()

        Console.WriteLine("Subject: {0}", email.Subject)
        Console.WriteLine("Sender name: {0}", email.Sender.Name)
        Console.WriteLine("Sender address: {0}", email.Sender.Address)

        Console.WriteLine("Html: {0}", email.Html)

        Console.WriteLine("Attachments: {0}", email.Attachments.Count)
        For Each attachment As MimeData In email.Attachments
            attachment.Save("c:\" + attachment.SafeFileName)
        Next
    End If
End Using

C#:

using (MsgConverter converter = new MsgConverter(@"c:\outlook1.msg"))
{
    if (converter.Type == MsgType.Note)
    {
        IMail email = converter.CreateMessage();

        Console.WriteLine("Subject: {0}", email.Subject);
        Console.WriteLine("Sender name: {0}", email.Sender.Name);
        Console.WriteLine("Sender address: {0}", email.Sender.Address);

        Console.WriteLine("Html: {0}", email.Html);

        Console.WriteLine("Attachments: {0}", email.Attachments.Count);
        foreach (MimeData attachment in email.Attachments)
        {
            attachment.Save(@"c:\" + attachment.SafeFileName);
        }
    }
}

Reference:
https://www.limilabs.com/blog/reading-outlook-msg-file-format-net

by (301k points)
...