Solved

Reading outlook .msg files

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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