如何使用 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,encodedStringBytes.Length,decodedStringBytes);

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

    return decodedString;
}

而且我需要知道我必须使用什么解码器来进行 SevenBit 和 EightBit 内容编码。我查看了文档 here,但没有找到正确的解码器。

谢谢。

解决方法

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