如何选择邮箱 [英] How to choose a mailbox

查看:96
本文介绍了如何选择邮箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MS Outlook 中有两个邮箱.我需要从其中一个接收邮件.如何选择特定的邮箱?我安装了 Office 365.Logon('', '', true, true); ?

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); ?

我的代码

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;

推荐答案

我已经修复了你的代码.

I have fixed your code.

这个想法是在两个级别迭代文件夹:邮箱和邮箱中的文件夹.

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

已使用 Oultook 2019 进行测试.也应适用于其他版本.

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;

这篇关于如何选择邮箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆