unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, StdCtrls; type TForm1 = class(TForm) IdSMTP1: TIdSMTP; IdMessage1: TIdMessage; GroupBox1: TGroupBox; Edit2: TEdit; Edit1: TEdit; Label1: TLabel; Label2: TLabel; Edit3: TEdit; Label3: TLabel; Label4: TLabel; Edit4: TEdit; GroupBox2: TGroupBox; GroupBox3: TGroupBox; Memo1: TMemo; Edit5: TEdit; Edit6: TEdit; Edit7: TEdit; Memo2: TMemo; Button1: TButton; Label5: TLabel; Label6: TLabel; Label7: TLabel; Label8: TLabel; OpenDialog1: TOpenDialog; Button2: TButton; Edit8: TEdit; procedure Button1Click(Sender: TObject); procedure IdSMTP1Disconnected(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin //TIdAttachment.Create(IdMessage1.MessageParts,'C:\cmd.txt'); IdSmtp1.AuthenticationType:=atLogin; IdSMTp1.UserId := edit1.Text; IdSmtp1.Password:= edit2.Text; IdSmtp1.Host:=edit3.Text; Idsmtp1.Port:=strtoint(edit4.Text); //IdMessage1.Clear; //IdMessage1.Body.Assign(memo2.Lines); IdMessage1.Body.Text := memo2.Text ; IdMessage1.From.Address:=edit6.Text; IdMessage1.Recipients.EMailAddresses:=edit5.Text; IDMessage1.Subject:=edit7.Text; //AAttachment.Create(IDMessage1.MessageParts,'C:\cmd.txt'); //TIdAttachmentFile.Create(IdMessage1.MessageParts, 'C:\cmd.txt'); try try memo1.Lines.Add('Connecting...'); IdSMTP1.Connect; except MessageBox(0, '连接服务器失败', nil, MB_OK or MB_ICONERROR); memo1.Lines.Add('Connect Failed'); Exit; end; memo1.Lines.Add('Connected'); try memo1.Lines.Add('Sending...'); IdSMTP1.Send(IdMessage1); memo1.Lines.Add('Send Successed'); MessageBox(0, '邮件已经发送成功', nil, MB_OK or MB_ICONINFORMATION); except memo1.Lines.Add('Send Failed'); MessageBox(0, '邮件发送失败', nil, MB_OK or MB_ICONERROR); end; finally IdSmtp1.Disconnect; IdSmtp1.AuthenticationType:=atNone; //IdMessage1.Free; //IdSmtp1.Free; end; end; procedure TForm1.IdSMTP1Disconnected(Sender: TObject); begin memo1.Lines.Add('DisConnected!'); end; procedure TForm1.Button2Click(Sender: TObject); begin if OpenDialog1.Execute then begin TIdAttachment.Create(IdMessage1.MessageParts,OpenDialog1.FileName); //showmessage(OpenDialog1.FileName); edit8.Text := OpenDialog1.FileName; end; end; end.