leon
leon

Reputation: 21

Microsoft.Office.Interop.Outlook Release Memory

I try to read some outlook msg files with Microsoft.Office.Interop.Outlook Dll. I know Outlook must be installed on machine.

I use this code to read the msg file. This works fine.

Microsoft.Office.Interop.Outlook._Application app = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.MailItem fld = (Microsoft.Office.Interop.Outlook.MailItem)app.Session.OpenSharedItem(filename);

But if I open the same msg-file an exception is thrown, becouse the file is already opened. I think the gc is not clearing the objects.

How can I release the objects?

Upvotes: 1

Views: 536

Answers (1)

Marçal Torroella
Marçal Torroella

Reputation: 143

Probably you solved your problem nowadays, but you could try this:

    while (System.Runtime.InteropServices.Marshal.ReleaseComObject(mailItem) != 0) { }
    mailItem = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();

Upvotes: 2

Related Questions