There is a very nice Email-sender built into Delphi 7 (it may also be 
available in earlier versions) as part of Indy.

You can find a write-up (with some omissions) at 
http://forums.about.com/n/pfx/forum.aspx?msg=8220.1&nav=messages&webtag=ab-delphi

Here is my wrapper for creating and sending Emails.  Just call the 
procedures in the order they appear here (as often as necessary).  Be sure 
to change the text strings in EmailInit to reflect your own email account.

Rainer


[Begin Code]

unit EmailUnit;

interface

uses
  inifiles, Windows, Messages, SysUtils, Variants, Classes, Graphics, 
Controls, Forms,
  Dialogs, IdComponent, IdTCPConnection, IdTCPClient, IdMessageClient,
  IdSMTP, ComCtrls, StdCtrls, Buttons, ExtCtrls,
  IdBaseComponent, IdMessage, IdEmailAddress;

type
  TEmail = class (TForm)
    MailMessage: TIdMessage;
    SMTP:        TIdSMTP;
  end;

var
  Email:    TEmail;
  procedure EmailInit (FromName, FromAddr, Subject: string; Body: TStrings);
  procedure EmailTo (ToName, ToAddr: string);  {May be called more than 
once}
  procedure EmailAttach (FileName: string);    {May be called more than 
once}
  procedure EmailSend;

implementation

{$R *.dfm}

procedure EmailInit (FromName, FromAddr, Subject: string; Body: TStrings);
begin with Email do begin

  // SMTP Host
  SMTP.Host :='Email SMTP Server';
  SMTP.Port := 25;

  // User Name and Password
  SMTP.AuthenticationType := atLogin;   // or atNone if your host doesn't 
casre who you are
  SMTP.Username := 'Email User Name';
  SMTP.Password := 'Email Password';

  // Message Parts
  MailMessage.Clear;
  MailMessage.From.Name := FromName;
  MailMessage.From.Address := FromAddr;
  MailMessage.Recipients.Clear;
  MailMessage.Subject := Subject;
  MailMessage.Body.Text := Body.Text;
end end;

procedure EmailTo (ToName, ToAddr: string);
var
  EI: TIdEMailAddressItem;
begin with Email do begin
  EI := MailMessage.Recipients.Add;
  EI.Name := ToName;
  EI.Address := ToAddr;
end end;

procedure EmailAttach (FileName: string);
begin with Email do begin
  if FileExists(FileName)
    then TIdAttachment.Create(MailMessage.MessageParts, FileName)
    else ShowMessage (format ('Attachment "%s" not found', [FileName]));
end end;

procedure EmailSend;
begin with Email do begin
  try
    try
      SMTP.Connect(1000);
      SMTP.Send(MailMessage);
    except on E:Exception do
      ShowMessage ('Email Error: ' + E.Message);
    end;
  finally
    if SMTP.Connected then SMTP.Disconnect;
  end;
end end;

end.

[End of Code] 

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to