none
Converting a MemoryStream / Byte array into an Outlook MailItem object RRS feed

  • Question

  • Hi,

    if I load a saved .msg file into my application using something like File.ReadAllBytes() is there any way to convert/cast this into a MailItem using a class that references Microsoft.Office.Interop.Outlook? It would be fantastic to be able to access the large amount of properties this opens up in this way, I can't see any obvious way of doing it though. Is it possible to do with Redemption or similar?

    Monday, June 27, 2011 3:08 PM

Answers

  • You can't do it as a stream as far as I know. However, using the Outlook object model and a saved MSG file you can use Application.CreateItemFromTemplate() to import the MSG as an Outlook item.
     
    You can do the same type of thing using Redemption using RDOMail.Import(), but it's probably not necessary in a case like that. Redemption also has an RDOSession.GetMessageFromMsgFile() and CreateMessageFromMsgFile(), but that also takes a file name/path and not a stream.
     
    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Thommo09" <=?utf-8?B?VGhvbW1vMDk=?=> wrote in message news:241b0332-25f2-49d7-957b-a7f3959061b0...

    Hi,

    if I load a saved .msg file into my application using something like File.ReadAllBytes() is there any way to convert/cast this into a MailItem using a class that references Microsoft.Office.Interop.Outlook? It would be fantastic to be able to access the large amount of properties this opens up in this way, I can't see any obvious way of doing it though. Is it possible to do with Redemption or similar?


    Ken Slovak MVP - Outlook
    • Marked as answer by Thommo09 Monday, June 27, 2011 7:22 PM
    Monday, June 27, 2011 6:16 PM

All replies

  • You can't do it as a stream as far as I know. However, using the Outlook object model and a saved MSG file you can use Application.CreateItemFromTemplate() to import the MSG as an Outlook item.
     
    You can do the same type of thing using Redemption using RDOMail.Import(), but it's probably not necessary in a case like that. Redemption also has an RDOSession.GetMessageFromMsgFile() and CreateMessageFromMsgFile(), but that also takes a file name/path and not a stream.
     
    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Thommo09" <=?utf-8?B?VGhvbW1vMDk=?=> wrote in message news:241b0332-25f2-49d7-957b-a7f3959061b0...

    Hi,

    if I load a saved .msg file into my application using something like File.ReadAllBytes() is there any way to convert/cast this into a MailItem using a class that references Microsoft.Office.Interop.Outlook? It would be fantastic to be able to access the large amount of properties this opens up in this way, I can't see any obvious way of doing it though. Is it possible to do with Redemption or similar?


    Ken Slovak MVP - Outlook
    • Marked as answer by Thommo09 Monday, June 27, 2011 7:22 PM
    Monday, June 27, 2011 6:16 PM
  • Hi Ken,

    many thanks for your quick response.

    You are absolutely correct, I found Application.CreateItemFromTemplate() and using it gives me the MailItem object from a .msg - I've marked your response as answer accordingly.

    As an aside though, not all the properties of this object are populated as they would be if was looking at a MailItem from my inbox during a bona fide Outlook session, which I was kind of expecting but hoping wouldn't be the case. I was experimenting with ConversationId. If I save a mail to my desktop and load it from there into, say, a Winforms app using Application.CreateItemFromTemplate() I get ConversationId as null. I can see the ConversationId on the original mail in the inbox (obviously a totally different object) by interrogating my inbox during an Outlook session.

    Interestingly if I delete the mail from Outlook completely then drag it back into my inbox from my desktop it still retains it's original ConversationID and it's place amonst the other mails in the conversation. So Outlook is doing something that I can't do by simply loading the .msg dynamically from the desktop. Is there a MAPI property in com that I can't see in VSTO that is retaining this information?

    It's not the first time you've helped me out with Outlook on this Forum, it's much appreciated.

    Monday, June 27, 2011 7:50 PM
  • If you have access to Redemption, that would preserve a lot of the properties in which you're interested.
     
    You would need to add an RDOMail item to the folder where you want the item added using RDOFolder.Items.Add(). Then use the RDOMail.Import() method and finally call Save() on the item. That should preserve ConversationIndex.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Thommo09" <=?utf-8?B?VGhvbW1vMDk=?=> wrote in message news:c8392ad7-4feb-4477-bfc9-73d5a3cf3dcc...

    Hi Ken,

    many thanks for your quick response.

    You are absolutely correct, I found Application.CreateItemFromTemplate() and using it gives me the MailItem object from a .msg - I've marked your response as answer accordingly.

    As an aside though, not all the properties of this object are populated as they would be if was looking at a MailItem from my inbox during a bona fide Outlook session, which I was kind of expecting but hoping wouldn't be the case. I was experimenting with ConversationId. If I save a mail to my desktop and load it from there into, say, a Winforms app using Application.CreateItemFromTemplate() I get ConversationId as null. I can see the ConversationId on the original mail in the inbox (obviously a totally different object) by interrogating my inbox during an Outlook session.

    Interestingly if I delete the mail from Outlook completely then drag it back into my inbox from my desktop it still retains it's original ConversationID and it's place amonst the other mails in the conversation. So Outlook is doing something that I can't do by simply loading the .msg dynamically from the desktop. Is there a MAPI property in com that I can't see in VSTO that is retaining this information?

    It's not the first time you've helped me out with Outlook on this Forum, it's much appreciated.


    Ken Slovak MVP - Outlook
    Monday, June 27, 2011 8:38 PM