Question

I'm using Microsoft.Office.Interop.Outlook to extract e-mail attachments:

var MAPI = new Application().GetNamespace("MAPI");
var ExampleFolder = MAPI.GetDefaultFolder(OlDefaultFolders.olFolderSentMail)

foreach (dynamic i in ExampleFolder.Items)
  if (i.Attachments.Count > 0)
    ; // DoSomething();

Unfortunately this is extremely slow.

  • Is there any faster way to check for attachments?
  • Is it possible to filter/sort e-mails by date: loop through the last n items only?
Was it helpful?

Solution

sure, you can sort the collection using Items.Sort. You can also use Items.Find/FindNext or Items.Restrict to look for items with attachments only. The property you need is PR_HASATTACH (DASL name http://schemas.microsoft.com/mapi/proptag/0x0E1B000B)

OTHER TIPS

@Kiquenet (I can't add a comment below yours), here is the code to get items with attachments from Items.Restrict:

//fanti's code
var MAPI = new Application().GetNamespace("MAPI");
var ExampleFolder = MAPI.GetDefaultFolder(OlDefaultFolders.olFolderSentMail)

To filter by a date, just add "AND"s or "OR"s like this (Urn way):

var itemsWithAttachmentAndDate = ExampleFolder.Items.Restrict("@SQL= urn:schemas:httpmail:hasattachment = True"
 + " AND urn:schemas:httpmail:datereceived <= '" + DateTime.Now.AddMonths(-3) + "'");

To loop through the last n items only:

int n = 3;
for (int i = itemsWithAttachmentAndDate.Count - 1; i > n; i--)
{
    //current item: itemsWithAttachmentAndDate[i] //Beware: "dynamic" typed!
    ; //DoSomething();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top