2

Leading on from my previous question, if I set the body of a MimeMessage the attachments, bodyparts, from and all the details are removed. How can I get around this?

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;
}
Community
  • 1
  • 1
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69

1 Answers1

0

This is because you are setting the body to html only, you need to set it to be mixed and re-add the attachments:

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;
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69