使用 anmar.SharpMimeTools 解析 MIME 消息

发布于 2025-01-01 11:57:46 字数 2148 浏览 2 评论 0原文

我正在尝试使用 SharpMimeTools 和来自 < 的一些示例 Mime 消息来解析 MIME 消息a href="http://www.hunnysoft.com/mime/samples/index.html" rel="nofollow">Hunny Software。我设法从文件创建新消息并将解码正文保存到文件(它是 png 图像),但创建的文件已损坏。大多数示例文件和我提取的文件看起来相同,但也存在差异。

这些文件可以在这里找到:

文件的十六进制视图摘录:
原文:

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

提取:

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

... 最后,这是我正在使用的代码:

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);
    }
}

您对如何解决这个问题有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唐婉 2025-01-08 11:57:46

您正在使用 WriteAllText 写出二进制文件。您不能指望使用文本编写器写出 PNG。

WriteAllText 只能用于文本内容类型。对于其他内容类型,您应该使用WriteAllBytes

此外,在您的代码中,您正在使用传输时使用的原始文本编码编写文本。无论原始内容是什么,您可能只想使用 UTF-8。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文