问题
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");
}
}
}
How can I fix this?
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}");
}
}
Side note: Is the body.rtf file isn't true rtf because it starts with the following:
Content-Type: text/rtf; name=body.rtf
(new line)
回答1:
The reason that you are getting the Content-Type
header is because you are writing the MIME envelope as well as the content.
What you need to do is this:
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/mimekit-adds-the-rtf-as-an-attachment-and-not-the-body