环境:delphi6
所用控件:TNMSMTP
unit GetKey;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Psock, NMsmtp;
type
TfrmGetKey = class(TForm)
edtXLH: TEdit;
Label1: TLabel;
edtZCH: TEdit;
Label2: TLabel;
btnKey: TBitBtn;
btnClose: TBitBtn;
Label3: TLabel;
NMSMTP1: TNMSMTP;
Memo1: TMemo;
procedure btnCloseClick(Sender: TObject);
procedure btnKeyClick(Sender: TObject);
procedure NMSMTP1Connect(Sender: TObject);
private
function SendEmail: Boolean;
function EncodeBase64(Source: string): string;
public
{ Public declarations }
end;
const
BaseTable:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var
frmGetKey: TfrmGetKey;
implementation
{$R *.dfm}
procedure TfrmGetKey.btnCloseClick(Sender: TObject);
begin
self.Close;
end;
function TfrmGetKey.SendEmail: Boolean;
var
mText: string;
begin
mText := 'Product Number: '+trim(edtXLH.Text)+#13#10;
mText := mText + 'Product RegEdit Number£º'+GetKey(trim(edtXLH.Text ))+#13#10;
mText := mText+ 'RegEdit Time: '+datetimetostr(Now);
try
NMSMTP1.Connect;
NMSMTP1.PostMessage.FromAddress := 'yanzhaozhizi@163.com'; //发信地址
NMSMTP1.PostMessage.FromName := '发信人'; //发信人
NMSMTP1.PostMessage.Subject := '主题'; //主题
NMSMTP1.PostMessage.ToAddress.Add('MISSystem@126.com'); //收件人地址
NMSMTP1.PostMessage.ToAddress.Add('bbb@163.com'); //收件人地址
NMSMTP1.PostMessage.Body.Text := (mText);
NMSMTP1.SendMail; //发送
Result := True;
Except
Result := False;
Application.MessageBox('发送失败!');
Exit;
end;
NMSMTP1.Disconnect;
end;
function TfrmGetKey.EncodeBase64(Source: string): string;
var
Times,LenSrc,i:integer;
x1,x2,x3,x4:char;
xt:byte;
begin
result:='';
LenSrc:=length(Source);
if LenSrc mod 3 =0 then Times:=LenSrc div 3
else Times:=LenSrc div 3 + 1;
for i:=0 to times-1 do
begin
if LenSrc >= (3+i*3) then
begin
x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];
xt:=(ord(Source[1+i*3]) shl 4) and 48;
xt:=xt or (ord(Source[2+i*3]) shr 4);
x2:=BaseTable[xt+1];
xt:=(Ord(Source[2+i*3]) shl 2) and 60;
xt:=xt or (ord(Source[3+i*3]) shr 6);
x3:=BaseTable[xt+1];
xt:=(ord(Source[3+i*3]) and 63);
x4:=BaseTable[xt+1];
end
else if LenSrc>=(2+i*3) then
begin
x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];
xt:=(ord(Source[1+i*3]) shl 4) and 48;
xt:=xt or (ord(Source[2+i*3]) shr 4);
x2:=BaseTable[xt+1];
xt:=(ord(Source[2+i*3]) shl 2) and 60;
x3:=BaseTable[xt+1];
x4:='=';
end else
begin
x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];
xt:=(ord(Source[1+i*3]) shl 4) and 48;
x2:=BaseTable[xt+1];
x3:='=';
x4:='=';
end;
result:=result+x1+x2+x3+x4;
end;
end;
procedure TfrmGetKey.NMSMTP1Connect(Sender: TObject);
begin
//////连接成功,下面用户认证过程
Memo1.lines.Add(nmsmtp1.Status);
if nmsmtp1.ReplyNumber = 250 then
Memo1.lines.Add(nmsmtp1.Transaction('auth login')); //开始认证
if nmsmtp1.ReplyNumber =334 then //返回值为334,让你输入Base64编码后的用户名
Memo1.lines.Add(nmsmtp1.Transaction(EncodeBase64('yanzhaozhizi')));//'YWFhYWE=');//
用户名为(发件箱登录时的用户名)
if nmsmtp1.ReplyNumber =334 then //返回值为334,让你输入Base64编码后的用密码
Memo1.lines.Add(nmsmtp1.Transaction(EncodeBase64('01020304')));//'MTIzNDU2');
//用户密码为(发件箱登录时的密码)
if nmsmtp1.ReplyNumber =235 then
begin
Memo1.lines.Add('successful');
end;
end;