Outlook Object Model - Detecting Mailboxes

2019-03-04 08:20发布

问题:

I have a Delphi 2006 BDS application with the following code to iterate outlook mailboxes and then the Inbox and Sent Items within the mailbox:

  try
    nameSpace := outlook.GetNameSpace('MAPI');
    // load the mailboxes
    mailbox := NameSpace.Folders;

    for i := 1 to mailbox.Count do
      if Pos('MAILBOX', UpperCase(mailbox.Item[i].Name)) > 0 then
      begin
        rootNode := trvwOutlookFolders.Items.AddChildObject(nil, mailbox.Item[i].Name, nil);

        for j := 1 to mailbox.Item[i].Folders.Count do
          if (Pos('INBOX', UpperCase(mailbox.Item[i].Folders[j].Name)) > 0) or
             (Pos('SENT ITEMS', UpperCase(mailbox.Item[i].Folders[j].Name)) > 0) then
          begin
        // do processing
          end;

      end;

  finally
    outlook := Unassigned;
  end;
end;

The code works fine under Outlook 2007 but doesn't in 2010 because the mailboxes do not contain the word 'Mailbox'. Therefore I am after an alternative method of extracting JUST the mailboxes (not public folders etc) from within Outlook and their subsequence Inbox and sent items folders. Any ideas?

回答1:

In Outlook folders can be typed and have a DefaultItemType property. Replacing

if Pos('MAILBOX', UpperCase(mailbox.Item[i].Name)) > 0 then

with

if (mailbox.Item[i].DefaultItemType = olMailItem) then

should give you the folders that by default store only mail messages.

Mail messages can of course be stored in untyped folders as well, but as olMailItem has

olMailItem = $00000000;

as its value, it is the default for all untyped folders as well. So basically any untyped folder by default stores mail items.