How can I login to a Gmail account and get number of messages in the INBOX
mailbox with TIdIMAP4
component ?
Asked
Active
Viewed 5,865 times
8

TLama
- 75,147
- 17
- 214
- 392
-
Is this a gmail question, or an imap question? – David Heffernan Nov 28 '12 at 11:52
-
2OK, so the gmail part is not the issue. Presumably could be any imap server that you communicate. – David Heffernan Nov 28 '12 at 11:55
2 Answers
12
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.
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;
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).

TLama
- 75,147
- 17
- 214
- 392
-
How can you loop through email's to get subject or body of "i" email in folder "inbox". This doesn't seem to work.. IMAPClient.MailBox.MessageList.Messages[i].Body – Nov 28 '12 at 13:46
-
You need to retrieve them first. Ideally retrieve just envelopes by [`RetrieveAllEnvelopes`](http://www.indyproject.org/docsite/html/TIdIMAP4_RetrieveAllEnvelopes@TIdMessageCollection.html) method. This gives you a collection of fields where you'll have except others the subject and message ID. This message ID you can then use in one of the `UID` prefixed methods. But this is definitely topic for the new question. – TLama Nov 28 '12 at 14:10
-
I tried to use your code. I copied it 1:1 and every time I try to connect I receive "SSL3_GET_RECORD connection: wrong version number". I have tried ALL downloadable OpenSSL libraries replacing them in the bin folder used by the project. I use RAD Studio 10 Seattle Version 23.0.2 Indy 10.6.2.5. Maybe You have any thougths what's wrong? Thank You – kwadratens Feb 23 '20 at 11:15
0
kwadratens, you need to use
OpenSSLHandler.SSLOptions.Method := sslvTLSv1;
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 12 '21 at 00:18