Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdAntiFreezeBase, IdAntiFreeze, IdComponent,
IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, IdBaseComponent,
IdMessage;
type
TForm1 = class(TForm)
IdMessage: TIdMessage;
SMTP: TIdSMTP;
IdAntiFreeze1: TIdAntiFreeze;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function SendMail(Smtp:tidsmtp;IdMessage:tIdMessage;Username,Password,Host:string;Port:integer;fromAddress,fromName,recipientsMailAdress,AttachPath,Subject,Text:string):boolean;
var
attach:TidAttachment;
begin
result:=false;
SMTP.AuthenticationType:= atLogin;
SMTP.Username:=Username;
SMTP.Password:=Password;
SMTP.Host:=Host;
SMTP.Port:=Port;
IdMessage.Body.Add(Text);
IdMessage.Subject:=Subject;
IdMessage.From.Address:=fromAddress;
IdMessage.From.Name:=fromName;
IdMessage.Recipients.EMailAddresses:=recipientsMailAdress;
IdMessage.IsEncoded:=true;
attach:=TIdAttachment.Create(idMessage.MessageParts,AttachPath);
try
SMTP.Connect;
if SMTP.Connected then
begin
SMTP.Send(IdMessage);
result:=true;
end
else result:=false;
finally
SMTP.Disconnect;
end;
attach.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
opendialog1.InitialDir :=getcurrentdir;
opendialog1.Title := 'Укажите файл который вы хотите отправить';
if opendialog1.Execute = false then exit;
if SendMail(Smtp,idMessage,'egorka100','5555','smtp.mail.ru',25,'egorka100@mail.ru','Egor','simplemailtest2@mail.ru',opendialog1.FileName,'Hello','Hello world!')= true then showmessage('Your letter has been successfully sent')
else
showmessage('Your letter has not been successfully sent');
end;
end.