Solved

Reading outlook .msg files

Posted on 2014-10-14
3
621 Views
Last Modified: 2014-10-29
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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        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++)
            {
                try
                {
                    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;
                    item.Close(Outlook.OlInspectorClose.olDiscard);
                }
                catch (Exception)
                {   
                    //throw;
                }
            }
        }
    }
}

Open in new window


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.".

Open in new window

What is the difference between Microsoft.Office.Interop.Outlook._MailItem and Microsoft.Office.Interop.Outlook.MailItem? 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?
0
Comment
Question by:bbkevin
3 Comments
 
LVL 13

Assisted Solution

by:frankhelk
frankhelk earned 150 total points
ID: 40381634
About Q1: I presume that instantiating objects from the Outlook classes (esp. the "app" object) is almost the same amount of activity than running Outlook itself ... it might even start some background processes to provide some of the services. I think that some component in that bundle activates its icon. I fear the only way to circumvent that would be to explore Outlook's msg file format (see i.e. HERE) and write your own, lean dissector. That would eleminate Q3 as well, I think ;-)
0
 
LVL 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 50 total points
ID: 40381790
Aspose Email component can process msg files without even having Outlook installed: http://www.aspose.com/.net/email-component.aspx
0
 
LVL 13

Accepted Solution

by:
Alexei Kuznetsov earned 300 total points
ID: 40386279
Q1: Creating an instance of Outlook.Application class includes running outproc server (outlook.exe). This is why you have Outlook tray icon. You can't change this behavior.

Q2: _MailItem is an interface while MailItem is a class that implements _MailItem. You can safely cast the object to _MailItem.

Q3: item.ReleaseComObject()

Advice1: Move Outlook.Application object creation and getting Session out of the "for" loop:
private void button1_Click(object sender, EventArgs e)
{
    var app = new Microsoft.Office.Interop.Outlook.Application();
    var session = app.Session;
    ...

Open in new window

Use session.OpenSharedItem() inside the loop.

Advice2: Use textBox1.AppendText() method instead of "Text +=". This will significantly improve the performance on large number of items.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Learn more about how the humble email signature can be used as more than just an electronic business card. When used correctly, a signature can easily be tailored for different purposes by different departments within an organization.
Outlook Free & Paid Tools
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now