How to log in to your Gmail account and get the number of messages in your mailbox using TIdIMAP4?

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

+7
source share
1 answer

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

In the code, it may look like this (OpenSSL is required for this code, so do not forget to put the libeay32.dll and ssleay32.dll libraries in the path visible to your project, you can download the 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; 

You can download the demo project , which includes OpenSSL v1.0.1c for the i386 platform for 32-bit applications (compiled in Delphi 2009).

+12
source

All Articles