Search code examples
delphioutlookms-office

How to choose a mailbox


I have two mailboxes in MS Outlook. I need to get mail from one of them. How to select a specific mailbox? I have Office 365 installed. Something needs to be set in Logon ('', '', true, true); ?

My code

zOutlook := TOutlookApplication.Create(nil);
try
  zOutlook.ConnectKind := ckNewInstance;
  try
    zOutlook.Connect;
    try
      zNameSpace := zOutlook.GetNamespace('MAPI');
      zNameSpace.Logon('', '', true, true);
      try
        zInbox := zNameSpace.GetDefaultFolder(olFolderInbox);
        zInboxUnread := zInbox.Items.Restrict('[Unread]=true');
        FCodeSite.Send('zInboxUnread.Count ' + IntToStr(zInboxUnread.Count));
        for i := 1 to zInboxUnread.Count do
        begin
            //..
        end;
      finally
        zNameSpace.Logoff;
      end;
    finally
      zOutlook.Disconnect;
    end;

  except
    on E: SysUtils.Exception do
    begin
      FCodeSite.SendError(E.Message);
    end;
  end;
finally
  zOutlook.Free;
end;

Solution

  • I have fixed your code.

    The idea is to iterate the folders at two levels: the mailboxes and the folders in the mailboxes.

    Tested with Oultook 2019. Should work with other versions as well.

    procedure TForm1.Button1Click(Sender: TObject);
    var
        zOutlook     : TOutlookApplication;
        zNameSpace   : _NameSpace;
        zInbox       : MAPIFolder;
        zFolder      : MAPIFolder;
        zInboxUnread : _Items;
        I            : Integer;
        Found        : Boolean;
        MailBoxName  : String;
        InboxName    : String;
    begin
        MailBoxName := 'francois.piette@company.com';
        InboxName   := 'Boîte de réception';
        zOutlook := TOutlookApplication.Create(nil);
        try
          zOutlook.ConnectKind := ckNewInstance;
          try
            zOutlook.Connect;
            try
              zNameSpace := zOutlook.GetNamespace('MAPI');
              zNameSpace.Logon('', '', true, true);
              try
                Found := FALSE;
                for I := 1 to zNameSpace.Folders.Count do begin
                    zFolder := zNameSpace.Folders.Item(I);
                    if SameText(zFolder.Name, MailBoxName) then begin
                        Found := TRUE;
                        break;
                    end;
                end;
                if not Found then
                    Exit;
    
                Found := FALSE;
                for I := 1 to zFolder.Folders.Count do begin
                    zInbox := zFolder.Folders.Item(I);
                    if SameText(zInbox.Name, InboxName) then begin
                        Found := TRUE;
                        break;
                    end;
                end;
                if not Found then
                    Exit;
                zInboxUnread := zInbox.Items.Restrict('[Unread]=true');
                for i := 1 to zInboxUnread.Count do begin
                    //..
                end;
              finally
                zNameSpace.Logoff;
              end;
            finally
              zOutlook.Disconnect;
            end;
          except
            on E: System.SysUtils.Exception do begin
              Memo1.Lines.Add(E.Message);
            end;
          end;
        finally
          zOutlook.Free;
        end;
    end;