FastNet 页下的,NMSMTP 来作。
unit uEMail;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Psock, NMsmtp;
type
TfEMail = class(TForm)
NMSMTP1: TNMSMTP;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Memo1: TMemo;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Edit6: TEdit;
Label5: TLabel;
Button1: TButton;
Button2: TButton;
OpenDialog1: TOpenDialog;
ListBox1: TListBox;
Button3: TButton;
Label6: TLabel;
procedure Button1Click(Sender: TObject);
procedure NMSMTP1Connect(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fEMail: TfEMail;
implementation
{$R *.dfm}
function encodebase64(s:string):string; //上面代码的函数名相应改过来。
const
Tb: String[64] =
('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/');
var
i,a:integer;
temp,temp1:string;
begin
temp:='';
temp1:=s;
if length(temp1) mod 3<>0 then temp1:=temp1+char(0);
for i:=1 to length(temp1) do
begin
case (i-1) mod 3 of
0: begin
a:=ord(temp1);
a:=a shr 2;
temp:=temp+tb[a+1];
end;
1: begin
a:=ord(temp1[i-1]) and 3;
a:=a shl 8+ ord(s);
a:=a shr 4;
temp:=temp+tb[a+1];
end;
2: begin
a:=ord(temp1[i-1]) and $F;
a:=a shl 8+ord(temp1);
a:=a shr 6;
temp:=temp+tb[a+1];
a:=ord(temp1);
a:=a and $3f;
temp:=temp+tb[a+1];
end;
end;
end;
a:=length(s);
if a mod 3=2 then
begin
temp1:='';
for i:=1 to length(temp)-1 do temp1:=temp1+temp;
temp:=temp1;
end;
if (a>0) and (a mod 3<>0) then
if a mod 3 =1 then temp:=temp+'=='
else temp:=temp+'=';
result:=temp;
end;
procedure TfEMail.Button1Click(Sender: TObject);
begin
NMSmtp1.Host:=Edit1.Text; {SMTP服务器名,例如:'smtp.21cn.com'}
NMSmtp1.UserID:=Edit2.Text;{用户名,例如:'jtchang'}
NMSmtp1.PostMessage.FromAddress:=Edit4.Text; {发件人地址}
NMSmtp1.PostMessage.ToAddress.Clear; {清除所有收件人地址}
NMSmtp1.PostMessage.ToAddress.Add (Edit5.Text); {加一个收件人地址}
NMSmtp1.PostMessage.Subject:=Edit6.Text; {邮件主题}
NMSmtp1.PostMessage.Body.Assign(Memo1.Lines); {邮件正文,memo1里的文字}
NMSmtp1.PostMessage.Attachments.AddStrings(ListBox1.Items); {邮件的附件}
//开始发送
NMSmtp1.Connect; {连接smtp服务器,当连接时NMSMTP1的OnConnect事件被激活。}
NMSmtp1.SendMail; {发送邮件}
NMSmtp1.Disconnect; {关闭连接}
application.MessageBox('邮件发送完成!','成功',0);
end;
procedure TfEMail.NMSMTP1Connect(Sender: TObject);
var
strUserName, strPassword: String;
begin
strUserName := encodebase64(edit2.Text); {自已的邮箱帐号}
strPassword := encodebase64(edit3.Text); {邮箱密码,我可不能告诉你。}
nmsmtp1.Transaction('AUTH LOGIN'); {请求服务器身份验证}
nmsmtp1.Transaction(strUserName); {发送经过base64编码的帐号}
nmsmtp1.Transaction(strPassword); {发送经过base64编码的密码}
end;
procedure TfEMail.Button2Click(Sender: TObject);
begin
if opendialog1.Execute then
listbox1.Items.Add (opendialog1.FileName);
end;
procedure TfEMail.Button3Click(Sender: TObject);
begin
listbox1.Items.Clear;
end;
end.