c# - 如何使用 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 内容编码。我在这里检查了文档,但没有找到正确的解码器。
谢谢。
解决方案
为什么不使用mimePart.Content.DecodeTo()
而不是尝试自己手动完成?
推荐阅读
- c# - 我可以使用 System.Text.Json 使用私有构造函数反序列化 Json 吗?
- c# - 如何反序列化不使用 BinaryFormatter 的泛型对象?
- python - 使用'any'来查找字符串是否包含列表的一部分,然后将找到的部分变成变量?
- c - DHT11传感器连接ESP8266时保持打印255温度和湿度
- javascript - .addRole &.removeRole 的问题
- list - Prolog 将某些项目添加到列表中
- react-native - 错误 无法安装应用程序。确保您已设置 Android 开发环境
- excel - 将单元格背景颜色设置为其包含的 RGB 值。如何?
- scala - Scala:使用if语句对列表项进行模式匹配时,简单模式的非法开始
- regex - 括号之间的正则表达式可选组文本,但在结果中排除括号