使用以下代码,winmail.dat文件的rtf正文作为附件添加到保存的电子邮件中,而不是正文:
using (Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
{
MimeKit.MimeMessage mimeMessage = MimeKit.MimeMessage.Load(stream);
int i = 1;
foreach (MimeKit.MimePart attachment in mimeMessage.Attachments)
{
if (attachment.GetType() == typeof(MimeKit.Tnef.TnefPart))
{
MimeKit.Tnef.TnefPart tnefPart = (MimeKit.Tnef.TnefPart)attachment;
MimeKit.MimeMessage tnefMessage = tnefPart.ConvertToMessage();
tnefMessage.WriteTo(path + $"_tnefPart{i++}.eml");
}
}
}我怎么才能解决这个问题?
查看Attachments,它不存在于其中,但是附件和body.rtf文件都存在于BodyParts中。这样我就可以得到这样的body.rtf文件:
int b = 1;
foreach (MimeKit.MimeEntity bodyPart in tnefMessage.BodyParts)
{
if (!bodyPart.IsAttachment)
{
bodyPart.WriteTo(path + $"_bodyPart{b++}.{bodyPart.ContentType.MediaSubtype}");
}
}附带注意: body.rtf文件不是真正的rtf,因为它以以下内容开头:
内容-类型: text/rtf;name=body.rtf (新项目)
发布于 2017-02-16 00:46:20
获取Content-Type头的原因是因为您正在编写MIME信封和内容。
你需要做的是:
int b = 1;
foreach (MimeKit.MimeEntity bodyPart in tnefMessage.BodyParts)
{
if (!bodyPart.IsAttachment)
{
var mime = (MimeKit.MimePart) bodyPart;
mime.ContentObject.DecodeTo(path + $"_bodyPart{b++}.{bodyPart.ContentType.MediaSubtype}");
}
}https://stackoverflow.com/questions/42139666
复制相似问题