Load, Parse Or View MS Outlook MSG Files From A File Or Stream


SUBMITTED BY: Guest

DATE: June 12, 2013, 1:12 a.m.

FORMAT: Text only

SIZE: 2.5 kB

HITS: 857

  1. //Loading MSG Files
  2. //[C#]
  3. //Create an instance of MapiMessage from file
  4. MapiMessage msg = MapiMessage.FromFile(@"d:\Test.msg");
  5. //Get subject
  6. Console.WriteLine("Subject:" + msg.Subject);
  7. //Get from address
  8. Console.WriteLine("From:" + msg.SenderEmailAddress);
  9. //Get body
  10. Console.WriteLine("Body" + msg.Body);
  11. //Get recipients information
  12. Console.WriteLine("Recipient: " + msg.Recipients);
  13. //Get attachments
  14. foreach (MapiAttachment att in msg.Attachments)
  15. {
  16. Console.Write("Attachment Name: " + att.FileName);
  17. Console.Write("Attachment Display Name: " + att.DisplayName);
  18. }
  19. [VB.NET]
  20. 'Create an instance of MapiMessage from file
  21. Dim msg As MapiMessage = MapiMessage.FromFile("d:\Test.msg")
  22. 'Get subject
  23. Console.WriteLine("Subject:" + msg.Subject)
  24. 'Get from address
  25. Console.WriteLine("From:" + msg.SenderEmailAddress)
  26. 'Get body
  27. Console.WriteLine("Body" + msg.Body)
  28. 'Get recipients information
  29. Console.WriteLine("Recipient: " & msg.Recipients.ToString())
  30. 'Get attachments
  31. For Each att As MapiAttachment In msg.Attachments
  32. Console.Write("Attachment Name: " & att.FileName)
  33. Console.Write("Attachment Display Name: " & att.DisplayName)
  34. Next att
  35. //Loading MSG files from Stream
  36. //[C#]
  37. byte[] bytes = System.IO.File.ReadAllBytes(@"c:\test.msg");
  38. using (MemoryStream stream =
  39. new System.IO.MemoryStream(bytes))
  40. {
  41. stream.Seek(0, System.IO.SeekOrigin.Begin);
  42. //Create an instance of MapiMessage from file
  43. MapiMessage msg = MapiMessage.FromStream(stream);
  44. //Get subject
  45. Console.WriteLine("Subject:" + msg.Subject);
  46. //Get from address
  47. Console.WriteLine("From:" + msg.SenderEmailAddress);
  48. //Get body
  49. Console.WriteLine("Body" + msg.Body);
  50. }
  51. [VB.NET]
  52. Dim bytes() As Byte = System.IO.File.ReadAllBytes("c:\test.msg")
  53. Dim stream As MemoryStream = New System.IO.MemoryStream(bytes)
  54. stream.Seek(0, System.IO.SeekOrigin.Begin)
  55. 'Create an instance of MapiMessage from file
  56. Dim msg As MapiMessage = MapiMessage.FromStream(stream)
  57. 'Get subject
  58. Console.WriteLine("Subject:" + msg.Subject)
  59. 'Get from address
  60. Console.WriteLine("From:" + msg.SenderEmailAddress)
  61. 'Get body
  62. Console.WriteLine("Body" + msg.Body)

comments powered by Disqus