woensdag 24 juni 2009

Connecting with gmail through IMAP with Delphi and Indy

I have benefited a lot from the info, insights and code snippets that are unselfishly posted by (Delphi) developers. Now is the time for me to do something back. Well, I hope that my contribution might be helpful to some (or someone).

After my fair share of googling and wrestling with the Indy components TIdIMAP4 and TIdSSLIOHandlerSocketOpenSSL, trying to achieve an IMAP connection with my gmail account, I experienced that euphoric feeling we've all come to love so much. Soooo addictive. Anyway, I didn't find anything on the internet that I could just copy/paste, haha just kiddin', and, hey!, of course you may doubt the way I searched. Well, actually, it's true. About the first part anyhow.

I did find some info on using POP with gmail but not so much on using IMAP. The tricky part was the SSL stuff. Well, in hindsight it wasn't tricky at all. After hours of struggling, you discover that it wasn't difficult at all. It's the aaaargh and yes! moment. Know the feeling? Earlier, I had succeeded in an IMAP connection to MS Exchange. After reading up on the info I found, I understood that I needed SSL because gmail requires an SSL connection.

Anyway, I finally succeeded in making an IMAP connection to gmail, so I thought I will share how.

I will show how to connect to your gmail account through IMAP and to enumerate all folders of your gmail account. Of course, in the end you will want to retrieve your e-mails but I will leave the digging into the Indy TIdIMAP4 component to achieve that, to you. :)

This is what I used:
Delphi 7 (yes, 7! Still unbeatable at creating win32 apps!)
Indy Tiburon
OpenSSL 0.9.8k

Here is how to create a simple application to connect to a gmail account and enumerate all folders.

File > New > Application
Drop the following components on the form:
Button
Listbox
IdIMAP4 (from the Indy Clients components tab)
IdSSLIOHandlerSocketOpenSSL (from the Indy I/O Handlers components tab)

Set the following properties of the IdIMAP4 component:

Host -> 'imap.gmail.com'
IOHandler -> IdSSLIOHandlerSocketOpenSSL1
Password -> your password
Port -> 993
Username -> your_username
Use TLS -> utUseImplicitTLS

Set the following property of the IdSSLIOHandlerSocketOpenSSL component:

SSLOptions.Method -> sslvSSLv3

Add the following code to the OnClick event of the button that will show the folders in the listbox:

ListBox1.Clear;
IdIMAP41.Connect;
try
IdIMAP41.ListMailBoxes(ListBox1.Items);
finally
IdIMAP41.Disconnect;
end;

After setting these properties, the relevant parts of your DFM file should look like this:

object IdIMAP41: TIdIMAP4
IOHandler = IdSSLIOHandlerSocketOpenSSL1
Port = 993
Host = 'imap.gmail.com'
UseTLS = utUseImplicitTLS
SASLMechanisms = <>
MilliSecsToWaitToClearBuffer = 10
Left = 304
Top = 60
end

object IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL
Destination = 'imap.gmail.com:993'
Host = 'imap.gmail.com'
MaxLineAction = maException
Port = 993
DefaultPort = 0
SSLOptions.Method = sslvSSLv3
SSLOptions.Mode = sslmUnassigned
SSLOptions.VerifyMode = []
SSLOptions.VerifyDepth = 0
OnStatusInfo = IdSSLIOHandlerSocketOpenSSL1StatusInfo
Left = 640
Top = 28
end

Run the application and hit the button to enumerate all folders of your gmail account.

I would like to thank all the unselfish and altruistic Indy developers for giving the Delphi community such an invaluable networking library. Once I start making some money, I should not forget to show the Indy ppl my appreciation for their good work...

5 opmerkingen:

  1. Don't know if this is still being read by the blogger, but many thanks for this post! Made me save some time.

    BeantwoordenVerwijderen
  2. been struggling what to choose. Java gave me power when it comes to Core but when it comes to GUI it sucks.

    Delphi on the other hand has both .. but lacks community and support. Anyway, thanks for your guidance :)

    BeantwoordenVerwijderen
  3. Delphi lacks community? Delphi lacks support? Really? Haven't seen a better quality support than that by the Delphi community, to be honest.

    BeantwoordenVerwijderen
  4. Very clear explanition.
    Short and to the point.
    I like the idea to show the relevant parts of the dfm file.

    Thanks a lot,

    BeantwoordenVerwijderen
  5. You rock! This line:
    Use TLS -> utUseImplicitTLS

    Finally got the imap demo working for me. God knows what the original demo author was doing but it wasn't anything productive.

    Thanks :-)

    BeantwoordenVerwijderen