Friday, February 17, 2006

Making life easier with Indy

Let's take a look at the networking in Delphi.
One of the best library for writing network applications in Delphi is with no doubt Indy. It has almost everything someone wished for internt programming.

In the begining because today Google makes the net goes round ;) , let see how can use pop3 for retrieving mails from your Gmail account.

Drop on your form: a button, a listbox, one TIdPOP3, one TIdSSLIOHandlerSocketOpenSSL and one TIdMessage. Make the set the components that .dfm look like this:

object Form1: TForm1
Left = 0
Top = 0
Width = 420
Height = 378
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 16
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object ListBox1: TListBox
Left = 32
Top = 72
Width = 345
Height = 265
ItemHeight = 13
TabOrder = 1
end
object pop: TIdPOP3
IOHandler = IdSSLIOHandlerSocketOpenSSL1
AutoLogin = True
Host = 'pop.gmail.com'
Username = 'youruser@gmail.com'
UseTLS = utUseImplicitTLS
Password = 'yourpassword'
Port = 995
SASLMechanisms = <>
Left = 208
Top = 16
end
object msg: TIdMessage
AttachmentEncoding = 'MIME'
BccList = <>
CCList = <>
Encoding = meDefault
FromList = <>
Recipients = <>
ReplyTo = <>
ConvertPreamble = True
Left = 304
Top = 24
end
object IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL
Destination = 'pop.gmail.com:995'
Host = 'pop.gmail.com'
MaxLineAction = maException
Port = 995
DefaultPort = 0
SSLOptions.Method = sslvSSLv2
SSLOptions.Mode = sslmUnassigned
SSLOptions.VerifyMode = []
SSLOptions.VerifyDepth = 0
Left = 160
Top = 32
end
end

This is a simple application and i only show here how to retrieve the number of messages from the inbox and the subject. For everything eles use your imagination :D

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL,
IdSSLOpenSSL, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdPOP3;

type
TForm1 = class(TForm)
Button1: TButton;
pop: TIdPOP3;
msg: TIdMessage;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var n,i:integer;
begin
pop.Connect;
n:=pop.CheckMessages;
listbox1.Items.Add(format('There are %d messages',[n]));
for i:=0 to n-1 do
begin
pop.Retrieve(i,msg);
listbox1.Items.Add(format('%d - %s',[i,msg.Subject]));
application.ProcessMessages;
end;
pop.Disconnect;
end;

end.

I hope this helps you and if you got any questions feel free to ask.
Good luck !

1 comment:

Anonymous said...

thanks, pal.