Mimekit adds the rtf as an attachment and not the body

坚强是说给别人听的谎言 提交于 2019-12-25 16:08:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!