Delphi发送邮件的源码

2024-11-18 00:20:15
推荐回答(1个)
回答1:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdMessage, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Memo1: TMemo;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
with idmessage1 do
begin
body.Clear;
subject:=edit2.text;
From.Text:=edit4.text;
recipients.emailaddresses:=edit3.text;
body.assign(memo1.Lines);
ReceiptRecipient.Text := '';
//if edit7.Text='' then
//tidattachment.Create(idmessage1.MessageParts,edit7.Text);
end;
idsmtp1.authenticationtype:=atnone;
idsmtp1.UserId:=edit5.Text;
idsmtp1.Password:=edit6.Text;
idsmtp1.Host:='smtp.sohu.com';
idsmtp1.Port:=25;
{try
idsmtp1.Connect();
except
application.MessageBox('邮件发送失败!','提示',MB_OK+MB_ICONINFORMATION);
exit;
end;}
try
idsmtp1.Connect;
IdSMTP1.Send(IdMessage1);
application.MessageBox('邮件发送成功!','提示',MB_OK+MB_ICONINFORMATION);
finally
idsmtp1.Disconnect;
end;

end.