9

I'm creating an application that is reading emails received in outlook.

The reading process is something like this:

using Outlook = Microsoft.Office.Interop.Outlook;

var app = new Outlook.Application();
var ns = app.GetNamespace("MAPI");
ns.Logon(null, null, false, false);

var inboxFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
var subfolder = inboxFolder.Folders["MyFolderName"];

foreach (Outlook.MailItem item in subfolder.Items)
{
    // do something
    // item.EntryID
}

I need to get an unique ID for every item in the foreach loop.

There is a EntryID property in Outlook.MailItem that I used and it was working well, but I found out there was a problem with that property: Whenever I moved the email to another folder (inside outlook), This property EntryID changed.

I ran several tests and discovered that the EntryID value changes only a few chars when I move the mail to another folder.

I need an ID that would be unique no matter what folder. Is there an another property from Outlook.MailItem or any substring of EntryID that is always unique?

I'm using:

  • .NET 4.0;
  • Outlook 2010;
  • Microsoft.Office.Interop.Outlook.dll version 14.0.0.0.
Elementary
  • 2,153
  • 2
  • 24
  • 35
Jonny Piazzi
  • 3,684
  • 4
  • 34
  • 81
  • 1
    The EntryID may change, but I assume it is unique regardless of the folder. Exactly what is the problem, are you trying to save a reference to the id someplace? – jac Apr 30 '13 at 22:15

2 Answers2

9

There is no such property. You can create your own property using MailItem.PropertyAccessor or MailItem.UserProperties, but it will stop being unique if a message is copied to another folder as you will now have 2 items with the same id.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • 1
    So Dmitry, you saing there is no unique identifier for a mail. I don't doubt of you, but, just to me understand, how outlook know when a mail was or wasn't download from exchange mail box? – Jonny Piazzi May 01 '13 at 20:44
  • 4
    EntryId is *the* unique identifier. The fact that it changes when a message is moved, simply means that Exchange treats it as a completely different object. Note that the PST store provider does not change the entry id. – Dmitry Streblechenko May 02 '13 at 19:04
3

Unique Id for MailItem can be obtained by combining few email properties that could unlikely be the same for any different emails e.g.

$"{item.SenderEmailAddress}-{item.SentOn.Ticks/TimeSpan.TicksPerSecond}-{item.Size}";

Bohdan
  • 468
  • 5
  • 6