Solved

Reading outlook .msg files

Posted on 2014-10-14
3
827 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 (Outlook MVP) 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
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…
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: …

695 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