Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

email - How to decode ContentEncoding.SevenBit and ContentEncoding.EightBit MimePart with C# MailKit

I have this piece of code:

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

and I need to know what decoder I have to use with SevenBit and EightBit content-encoding. I've checked the documentation here but I didn't find the correct decoder.

Thanks.

question from:https://stackoverflow.com/questions/65849002/how-to-decode-contentencoding-sevenbit-and-contentencoding-eightbit-mimepart-wit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why not use mimePart.Content.DecodeTo() instead of trying to manually do it yourself?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...