I got thousand of msg files in a single folder. I would like to read msg files using c# to get the subject of those emails.
I first added a reference to the Microsoft Outlook 14.0 Object Library into the application, and then wrote the code below:
using Outlook = Microsoft.Office.Interop.Outlook;
public partial class Form1 : Form
private void button1_Click(object sender, EventArgs e)
string files = Directory.GetFiles(@"C:\test", "*.msg", SearchOption.AllDirectories);
for(int i=0; i < files.Length; i++)
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
var item = app.Session.OpenSharedItem(files[i]) as Outlook.MailItem;
textBox1.Text += item.Subject + Environment.NewLine;
Q1. When the application is running, Outlook tray icon will appear. Is there any other options/library able to access the msg files without showing the icon?
Q2. One warning in the error list after built the solution
"Ambiguity between method 'Microsoft.Office.Interop.Outlook._MailItem.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose)' and non-method 'Microsoft.Office.Interop.Outlook.ItemEvents_10_Event.Close'. Using method group.".
What is the difference between Microsoft.Office.Interop.O
MailItem and Microsoft.Office.Interop.O
lItem? Which one should I use? Can I cast the item as Outlook._MailItem instead?
Q3. The program seems to use a lot of memory, how to release the object created by OpenSharedItem method properly?