Mega Code Archive

 
Categories / Delphi / Examples
 

Indy mailer

If you have used Delphi for Internet programming, you have probably already learned what Indy components are. Indy components allow you to write TCP/IP applications very easily. The Indy components set includes both client and server components, so you could quickly write your own HTTP server if the need arises. Indy components, or Internet Direct more precisely, are now shipped along with Delphi 6, but these components are open-source and can be downloaded freely from: www.nevrona.com/indy. To demonstrate how easy and straightforward programming with Indy is, I made this mail-client. If you wan't the files, they're here: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, StdCtrls; type TForm1 = class(TForm) Button1: TButton; GroupBox1: TGroupBox; Label1: TLabel; Edit1: TEdit; Label2: TLabel; Edit2: TEdit; Label3: TLabel; Edit3: TEdit; Label4: TLabel; Memo1: TMemo; GroupBox2: TGroupBox; Edit4: TEdit; Edit5: TEdit; Label5: TLabel; Label6: TLabel; procedure Button1Click(Sender: TObject); private Msg: TIdMessage; SMTP: TIdSMTP; procedure SMTPDisconnected(Sender: TObject); { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin Msg:= TIdMessage.Create(Self); SMTP:= TIdSMTP.Create(Self); try SMTP.OnDisconnected:= SMTPDisconnected; Msg.From.Address:= Edit1.Text; // If more then one recipients, separate with commas Msg.Recipients.EMailAddresses:= Edit2.Text; Msg.Subject:= Edit3.Text; Msg.Body.Add(Memo1.Text); SMTP.Host:= Edit4.Text; SMTP.UserId:= Edit5.Text; with SMTP do begin Connect; Send(Msg); Disconnect; end; MessageDlg('Your mail has been sent successfully !', mtInformation, [mbOK], 0); except on E: Exception do MessageDlg('Something went wrong with your EMail !' + #13 + #10 + 'Errormessage: ' + #13 + #10 + E.message, mtError, [mbOK], 0); end; end; procedure TForm1.SMTPDisconnected(Sender: TObject); begin Msg.Free; SMTP.Free; end; end.