dif
dif

Reputation: 2493

Parsing a MIME-Message using anmar.SharpMimeTools

I'm trying to parse a MIME-Message, using SharpMimeTools and some sample Mime Messages from Hunny Software. I managed to create a new Message from a file and save the Decoded Body to a file (it's a png-Image), but the created file is corrupted. Mostly the the example file and the one I've exracted look the same, but there are differences.

The files can be found here:

An excerpt of the Hex-View of the files:
Original:

89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52
00 00 00 1b 00 00 00 1b 08 03 00 00 00 ba 0a 04
67 00 00 03 00 50 4c 54 45 ff ff ff 00 00 08 00
00 10 00 00 18 00 00 00 00 08 29 00 10 42 00 10
4a 00 08 31 00 10 52 08 21 73 08 29 7b 08 29 84
08 21 6b 00 18 5a 00 08 39 08 21 63 10 39 9c 18
42 a5 18 42 ad 18 42 b5 10 39 a5 10 31 94 00 18

Extracted:

3f 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52
00 00 00 1b 00 00 00 1b 08 03 00 00 00 3f 0a 04   
67 00 00 03 00 50 4c 54 45 3f 3f 3f 00 00 08 00   
00 10 00 00 18 00 00 00 00 08 29 00 10 42 00 10  
4a 00 08 31 00 10 52 08 21 73 08 29 7b 08 29 3f
08 21 6b 00 18 5a 00 08 39 08 21 63 10 39 3f 18
42 3f 18 42 3f 18 42 3f 10 39 3f 10 31 3f 00 18

... and finally, this is the code I'm using:

public void MIMETest()
{
    FileStream fileStream = new FileStream(@"D:\m0013.txt", FileMode.Open);
    SharpMimeMessage m = new SharpMimeMessage(fileStream);
    fileStream.Close();
    parseMessage(m);            
}

public void parseMessage(SharpMimeMessage message)
{
    if (message.IsMultipart)
    {
        foreach (SharpMimeMessage subMessage in message)
        {
            parseMessage(subMessage);
        }
    }
    else
    {
        System.IO.File.WriteAllText(@"D:\Extracts\" + message.Name,
            message.BodyDecoded, message.Header.Encoding);
    }
}

Do you have any suggestions how to solve this problem?

Upvotes: 0

Views: 1302

Answers (1)

Ben
Ben

Reputation: 35613

You are writing out binary files using WriteAllText. You cannot expect to write out a PNG using a text writer.

WriteAllText should only be used for text content-types. For other content-types you should use WriteAllBytes.

Also, in your code, you are writing the text using the original text encoding it was transmitted with. You probably want to just use UTF-8 regardless of what the original was.

Upvotes: 2

Related Questions