2

I'm working with Outlook and C# and my school exercise is to convert the body of an email in pdf without using extra software. In my case I would like to keep the email text format so to solve my problem I've considered to convert the email body in a RTF file, then open this RTF file with the Word application by C# and save it as PDF.

I would like to know how to obtain a RTF file from an MailItem. I've found on the web that it is possible to convert the MailItem body in RTF format using the BodyFormat property but i don't get how to create then a RTF.

Jongware
  • 22,200
  • 8
  • 54
  • 100
atomicBee
  • 79
  • 3
  • 8

3 Answers3

5

You can do without saving file as rtf format.

            Outlook.MailItem mi = selection[1] as Outlook.MailItem;               
            mi.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
            string fullPath = Path.Combine("SaveLocation", mi.Subject + ".pdf");
            //mi.SaveAs(fullPath, Outlook.OlSaveAsType.olRTF);                
            Word.Document doc = mi.GetInspector.WordEditor;
            doc.SaveAs2(fullPath, FileFormat: Word.WdSaveFormat.wdFormatPDF);

use using Word = Microsoft.Office.Interop.Word; in header

Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40
4

The answer provided by @JoshH is good. In fact it helped me, but the only thing is that it doesn't include the embedded files present in the email body like images. If you want to do that, set the body format to HTML instead of 'FormatRichText':

MailItem.BodyFormat = OlBodyFormat.olFormatHTML;
MailItem.SaveAs("FilePath", OlSaveAsType.olRTF)

That being said, the code I provided doesn't include the attachments made to the email itself, that is, not in the body.

I hope this helps someone.

SystemFailure
  • 99
  • 1
  • 13
1

Should be as simple as:

MailItem.BodyFormat = OlBodyFormat.olFormatRichText;
MailItem.SaveAs("FilePath", OlSaveAsType.olRTF)

MailItem.BodyFormat Property:

http://msdn.microsoft.com/en-us/library/office/ff869979(v=office.15).aspx

MailItem.SaveAs Method:

http://msdn.microsoft.com/en-us/library/office/ff868727(v=office.15).aspx

jnhunter
  • 40
  • 1
  • 3