Solved

Reading outlook .msg files

Posted on 2014-10-14
3
777 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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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: …
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…

733 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