首页 > 解决方案 > 如何使用 C# MailKit 解码 ContentEncoding.SevenBit 和 ContentEncoding.EightBit MimePart

问题描述

我有这段代码:

private string DecodeMimePart(MimePart mimePart)
{
    var decodedString = "";

    IMimeDecoder mimeDecoder = mimePart.ContentTransferEncoding switch
    {
        ContentEncoding.SevenBit => throw new NotImplementedException(),
        ContentEncoding.EightBit => throw new NotImplementedException(),
        ContentEncoding.Binary => new HexDecoder(),
        ContentEncoding.Base64 => new Base64Decoder(),
        ContentEncoding.QuotedPrintable => new QuotedPrintableDecoder(),
        ContentEncoding.UUEncode => new UUDecoder(),
        _ => throw new Exception($"ContentTransferEncoding '{mimePart.ContentTransferEncoding}' not supported"),
    };
        using var streamReader = new StreamReader(mimePart.Content.Stream, true);
        decodedString = streamReader.ReadToEnd();
        var encodedStringBytes = Encoding.UTF8.GetBytes(decodedString);
        var decodedStringBytes = new byte[4096];
        int decodedBytesNumber = mimeDecoder.Decode(encodedStringBytes, 0, encodedStringBytes.Length, decodedStringBytes);

        decodedString = Encoding.UTF8.GetString(decodedStringBytes, 0, decodedBytesNumber);

    return decodedString;
}

我需要知道我必须使用什么解码器来处理 SevenBit 和 EightBit 内容编码。我在这里检查了文档,但没有找到正确的解码器。

谢谢。

标签: c#emailmailkitdecodercontent-encoding

解决方案


为什么不使用mimePart.Content.DecodeTo()而不是尝试自己手动完成?


推荐阅读