Mimekit将rtf作为附件而不是正文添加 [英] Mimekit adds the rtf as an attachment and not the body
问题描述
使用以下代码,将winmail.dat文件的rtf正文作为附件添加到已保存的电子邮件中,而不是正文中:
Using the following code the rtf body of the winmail.dat file is added as an attachment to the saved email not the body:
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
中不存在该文件,但是在BodyParts
中存在附件和body.rtf文件.所以我可以这样获得body.rtf文件:
Looking into the Attachments
it is not present there but the attachments and the body.rtf file are present in the BodyParts
. So I can get the body.rtf file like this:
int b = 1;
foreach (MimeKit.MimeEntity bodyPart in tnefMessage.BodyParts)
{
if (!bodyPart.IsAttachment)
{
bodyPart.WriteTo(path + $"_bodyPart{b++}.{bodyPart.ContentType.MediaSubtype}");
}
}
旁注:body.rtf文件不是真正的rtf,因为它以以下内容开头:
Side note: Is the body.rtf file isn't true rtf because it starts with the following:
Content-Type:文本/rtf;名称= body.rtf
Content-Type: text/rtf; name=body.rtf
(新行)
推荐答案
获得Content-Type
标头的原因是因为您正在编写MIME信封以及内容.
The reason that you are getting the Content-Type
header is because you are writing the MIME envelope as well as the content.
您需要做的是这样
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}");
}
}
这篇关于Mimekit将rtf作为附件而不是正文添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!