8

如何INBOX使用组件登录到 Gmail 帐户并获取邮箱中的邮件数量TIdIMAP4

4

2 回答 2

12

要获取 Gmail 收件箱中的邮件总数,您需要首先使用您的凭据连接到 Gmail IMAP 服务器,选择 Gmail 的收件箱邮箱,然后为该选定邮箱读取TotalMsgs属性值。

在代码中它可能如下所示(此代码需要 OpenSSL,所以不要忘记将libeay32.dllssleay32.dll库放在对您的项目可见的路径;您可以下载不同版本和平台的 Indy 的 OpenSSL 库from here):

uses
  IdIMAP4, IdSSLOpenSSL, IdExplicitTLSClientServerBase;

function GetGmailMessageCount(const UserName, Password: string): Integer;
var
  IMAPClient: TIdIMAP4;
  OpenSSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  Result := 0;
  IMAPClient := TIdIMAP4.Create(nil);
  try
    OpenSSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    try
      OpenSSLHandler.SSLOptions.Method := sslvSSLv3;
      IMAPClient.IOHandler := OpenSSLHandler;
      IMAPClient.Host := 'imap.gmail.com';
      IMAPClient.Port := 993;
      IMAPClient.UseTLS := utUseImplicitTLS;
      IMAPClient.Username := UserName;
      IMAPClient.Password := Password;
      IMAPClient.Connect;
      try
        if IMAPClient.SelectMailBox('INBOX') then
          Result := IMAPClient.MailBox.TotalMsgs;
      finally
        IMAPClient.Disconnect;
      end;
    finally
      OpenSSLHandler.Free;
    end;
  finally
    IMAPClient.Free;
  end;
end;

procedure TForm1.ConnectButtonClick(Sender: TObject);
begin
  ShowMessage('Total count of messages in inbox: ' +
    IntToStr(GetGmailMessageCount('UserName@gmail.com', 'Password')));
end;

You may optionally download a demo project which includes OpenSSL v1.0.1c libraries for i386 platform for 32-bit applications (compiled in Delphi 2009).

于 2012-11-28T12:34:00.327 回答
0

kwadratens, you need to use

OpenSSLHandler.SSLOptions.Method := sslvTLSv1;

于 2021-10-11T22:17:09.850 回答