设置mimemessage的主体会删除其他详细信息

时间:2017-02-09 17:27:23

标签: c# mailkit mimekit

从我的previous question开始,如果我设置MimeMessage的正文,附件,正文部分和所有细节都会被删除。我怎么能绕过这个?

foreach (MimeKit.MimeEntity bodyPart in tnefMessage.BodyParts)
{
    if (!bodyPart.IsAttachment)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            bodyPart.WriteTo(ms);

            ms.Flush();
            ms.Position = 0;
            using (StreamReader sr = new StreamReader(ms))
            {
                //Read in the contents until we get to the rtf
                string line;
                while (!(line = sr.ReadLine()).StartsWith("{") && !line.StartsWith("\\")) { }

                tnefMessage.Body = new MimeKit.TextPart("plain")
                {
                    Text = RTFToText($"{line}{sr.ReadToEnd()}")
                };
            }
        }
    }
}

static string RTFToText(string rtf)
{
    string text = string.Empty;
    System.Threading.Thread thread = new System.Threading.Thread(() =>
    {
        using (System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox())
        {
            rtb.Rtf = rtf;
            text = rtb.Text;
        }
    });
    thread.SetApartmentState(System.Threading.ApartmentState.STA);
    thread.Start();
    thread.Join();

    return text;
}

1 个答案:

答案 0 :(得分:0)

这是因为您只将主体设置为html,您需要将其设置为混合并重新添加附件:

Multipart multipart = new Multipart("mixed");
multipart.Add(new MimeKit.TextPart("plain")
{
    Text = RTFToText($"{line}{sr.ReadToEnd()}")
});

foreach (MimeEntity attachment in tnefMessage.Attachments)
{
    multipart.Add(attachment);
}

tnefMessage.Body = multipart;