1
votes

There are a few Stackoverflow questions about this but none of them really offer a solution.

The Scenario - I'm creating an Outlook AddIn in VS2013. The user selects emails, hits my AddIn button and the emails are sent to a webservice to be stored in a database and linked to a client. Anyone, in any location will be able to open the email to view it.

Currently I am using the MailItem.SaveAs(filePath) function, then using File.ReadAllBytes(filePath) to create a byte array that can be sent to the webservice.

I delete the file as soon as I create the byte[]:

for (int x = 0; x < Emails.Count; x++)
{
    //TODO: RMc - is there a better way than saving to disk? unable to convert MailItem directly to stream...!
    Guid g = Guid.NewGuid();
    string filePath = "c:\\temp\\" + g.ToString() + ".msg";
    Emails.ElementAt(x).SaveAs(filePath);

    byte[] by = File.ReadAllBytes(filePath);
    File.Delete(filePath);//no longer needed now we have byte array

    //this is where I create a list of objects with all required data to send to web service

}

Writing the file to disk is slow - it creates a *.msg file that may never actually get used if no one wants to view it. So I would like to be able to save the MailItem object to a byte array directly - and then I could save that to the database and only create the *.msg file if a user requires it.

The MailItem object appears to by dynamic, and so I think this is the problem.

Can anyone offer a solution or an alternative way of achieving what I have described?

1
Does the byte array have to be in MSG format? - 1.618
Hi, preferably it would be a byte array of the MailItem. - Percy

1 Answers

0
votes

Messages in the Outlook Object Model and Extended MAPI are not streamable. You can save a message in a particular format, but it will not be an exact copy. MSG format will be the closest you can get.

Use MailItem.SaveAs(..., olMsg) to save a message as an MSG file.