There is an email support interface in Delphi 7.  Below are my routines to 
access it.  Call EmailInit once; Call EmailTo as often as desired.  Call 
EmailAttach as often as desired (or not at all).  Call EmailSend.  Be sure 
to edit the appropriate constants in EmailInit to reflect your SMTP server, 
username, and password.  If your server does not require authentication, 
change the atLogin specification.

Rainer von Saleski


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}
Uses DataUnit;

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

  // SMTP Host
  SMTP.Host := 'your email SMTP server';
  SMTP.Port := 25;

  // User Name and Password
  SMTP.AuthenticationType := atLogin;
  SMTP.Username := 'your email username';
  SMTP.Password := 'your 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 listing --

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

Reply via email to