如何使用TIdIMAP4登录Gmail帐户并获取邮箱中的邮件数量? [英] How to login to a Gmail account and get number of messages in a mailbox with TIdIMAP4?

查看:258
本文介绍了如何使用TIdIMAP4登录Gmail帐户并获取邮箱中的邮件数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I login to a Gmail account and get number of messages in the INBOX mailbox with TIdIMAP4 component ?

推荐答案

要获取Gmail收件箱中的邮件总数,您需要先连接到Gmail IMAP服务器凭据,选择Gmail的收件箱邮箱,并为所选邮箱读取 TotalMsgs 属性。

To get the total number of messages in your Gmail's inbox, you need to, first connect to the Gmail IMAP server with your credentials, select Gmail's inbox mailbox and for that selected mailbox read the value of the TotalMsgs property.

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

In code it may looks like follows (this code requires OpenSSL, so don't forget to put the libeay32.dll and ssleay32.dll libraries to a path visible to your project; you can download OpenSSL libraries for Indy in different versions and platforms 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;

您可以选择下载 演示项目 ,其中包括 OpenSSL v1.0.1c 用于32位应用程序的i386平台的库(在Delphi 2009中编译)。

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).

这篇关于如何使用TIdIMAP4登录Gmail帐户并获取邮箱中的邮件数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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