
01.12.2009, 17:38
|
 |
Начинающий
|
|
Регистрация: 01.10.2008
Сообщения: 138
Версия Delphi: 7
Репутация: 21
|
|
вот кода для отправки письма с вложением файла:
Код:
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdComponent, IdTCPConnection, IdTCPClient,
IdMessageClient, IdSMTP, IdBaseComponent, IdMessage, Buttons, ExtCtrls,
XPMan;
type
TMainForm = class(TForm)
Label1: TLabel;
ledHost: TLabeledEdit;
ledFrom: TLabeledEdit;
ledTo: TLabeledEdit;
ledCC: TLabeledEdit;
ledSubject: TLabeledEdit;
ledAttachment: TLabeledEdit;
Memo2: TMemo;
btnSendMail: TBitBtn;
Button1: TButton;
LabeledEdit1: TLabeledEdit;
LabeledEdit2: TLabeledEdit;
MailMessage: TIdMessage;
SMTP: TIdSMTP;
AttachmentDialog: TOpenDialog;
XPManifest1: TXPManifest;
procedure Button1Click(Sender: TObject);
procedure btnSendMailClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.Button1Click(Sender: TObject);
begin
if AttachmentDialog.Execute
then ledAttachment.Text := AttachmentDialog.FileName;
end;
procedure TMainForm.btnSendMailClick(Sender: TObject);
begin
// установка SMTP
SMTP.Host := ledHost.Text;
SMTP.Port := 25;
// установка сообщения
Smtp.AuthenticationType := atLogin; // atNone
Smtp.Username := LabeledEdit1.Text;
Smtp.Password := LabeledEdit2.Text;
MailMessage.From.Name := ledFrom.Text;
MailMessage.Subject := ledSubject.Text; // тема
MailMessage.From.Address := ledFrom.Text; // адрес отправителя
MailMessage.Recipients.EMailAddresses := ledTo.Text + ',' + ledCC.Text; // получатель + копия
MailMessage.Body.Text := Memo2.Text; // текст сообщения
if FileExists(ledAttachment.Text) then
TIdAttachment.Create(MailMessage.MessageParts,ledAttachment.Text);
try
try
SMTP.Connect(1000);
Application.ProcessMessages;
SMTP.Send(MailMessage);
MessageBox(0, 'Письмо успешно отправленно', 'Информация', 0);
except on E:Exception do
begin
MessageBox(0, 'Письмо не было отправленно', 'Информация', 0);
end;
end;
finally
if SMTP.Connected then SMTP.Disconnect;
end;
end;
end.
|