
20.11.2009, 16:30
|
Прохожий
|
|
Регистрация: 20.11.2009
Сообщения: 3
Репутация: 10
|
|
Отправка файла на почту
Вобщем делаю программу для отпарвки файла на почту. Тема, от кого, текст письма отправляется, а файл нет... Отправка у меня в отдельном потоке.
Вот код:
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdMessageClient, IdSMTP, IdMessage, TSendThread;
type
TForm1 = class(TForm)
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
GroupBox1: TGroupBox;
Label3: TLabel;
Edit3: TEdit;
Label4: TLabel;
Memo1: TMemo;
Label5: TLabel;
Edit4: TEdit;
Label6: TLabel;
Edit5: TEdit;
OpenButton: TButton;
OpenDialog1: TOpenDialog;
Button1: TButton;
Label7: TLabel;
Label8: TLabel;
Button2: TButton;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
procedure OpenButtonClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
so:TSendObj;
public
{ Public declarations }
end;
var
Form1: TForm1;
M: TIdMessage;
ATT: Tidattachment;
implementation
{$R *.dfm}
function GetFileSize(FileName: String): Integer;
var
FS: TFileStream;
begin
try
FS := TFileStream.Create(Filename, fmOpenRead);
except
Result := -1;
end;
if Result <> -1 then Result := FS.Size;
FS.Free;
end;
procedure TForm1.OpenButtonClick(Sender: TObject);
begin
if opendialog1.Execute then begin
edit5.Text:=opendialog1.FileName;
label7.Caption:='Размер: '+inttostr(GetFileSize(edit5.Text)div 1024)+'кб';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
M := TIdMessage.Create(Form1);
M.Body.Add(Memo1.Text);
M.From.Text := '"'+edit1.text+'" <'+edit2.Text+'>';
M.Recipients.Add;
M.Subject := edit3.text;
m.Recipients.EMailAddresses:=edit4.text;
IdSMTP1.AuthenticationType := atlogin;
IdSMTP1.Host := edit6.Text;
IdSMTP1.Username := edit7.Text;
IdSMTP1.Password := edit8.Text;
m.IsEncoded:=true;
ATT:=TIdAttachment.Create(m.MessageParts,edit5.Text);
so:=TSendObj.Create(true);
so.Resume;
so.Priority:=tpLower;
att.free;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
so.Terminate;
end;
end.
Поток:
Код:
unit TSendThread;
interface
uses
Classes, SysUtils, IdMessage;
type
TSendObj = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
implementation
uses Unit1;
procedure TSendObj.Execute;
begin
try
form1.IdSMTP1.Connect();
form1.label8.Caption:='Статус: Отправляется';
if form1.IdSMTP1.Connected then begin
form1.IdSMTP1.Send(unit1.M);
form1.Label8.Caption:='Статус: Отправлено';
end
else
form1.Label8.Caption:='Статус: Не удалось отправить';
finally
form1.IdSMTP1.Disconnect;
end;
end;
end.
|