Solved

Reading outlook .msg files

Posted on 2014-10-14
3
691 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 14

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 70

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 14

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
Read this checklist to learn more about the 15 things you should never include in an email signature.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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 …

770 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