CSDN博客

img caoqizi

实现邮件发送(delphi)

发表于2004/10/12 17:32:00  844人阅读

分类: 收藏

nit Umain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Menus, Psock, NMsmtp, ExtCtrls, ComCtrls;
  const BaseTable:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
type
  TForm1 = class(TForm)
    procedure BitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure NMSMTP1Connect(Sender: TObject);
    procedure NMSMTP1Failure(Sender: TObject);
    procedure NMSMTP1SendStart(Sender: TObject);
    procedure NMSMTP1Success(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  AuthSucc:boolean;// 是否需要密码验证
implementation

{$R *.dfm}

function 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 TForm1.BitBtn1Click(Sender: TObject);
begin
  NMSMTP1.Host := edit1.Text;//邮件服务器主机名,发送邮件的服务器
  NMSMTP1.Port :=strtoint(edit6.Text);//SMTP端口号,缺省为25,POP3端口号为110
  NMSMTP1.UserID := edit11.Text;//用户标识
  NMSMTP1.PostMessage.FromName:=edit8.Text;//发送邮件主机名
  StatusBar1.Panels[1].Text:='正在连接邮件服务器!';
  if not NMSMTP1.Connected then NMSMTP1.Connect;//连接
  NMSMTP1.PostMessage.FromAddress :=edit7.Text;//发件人地址
  NMSMTP1.PostMessage.ToAddress.Text := edit2.Text;//收件人地址
  NMSMTP1.PostMessage.ToCarbonCopy.Text :=edit3.Text;//抄送
  NMSMTP1.PostMessage.Body.Text := memo1.Text;//邮件内容
  NMSMTP1.PostMessage.Subject := edit4.Text;//邮件标题
  if edit5.Text<>'' then
  NMSMTP1.PostMessage.Attachments.Add(edit5.Text);//邮件附件
  NMSMTP1.SendMail;//发送邮件
  showmessage('邮件发送成功!');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
panel1.Visible:=false;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
panel1.Visible:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
panel1.Visible:=false;
end;

procedure TForm1.NMSMTP1Connect(Sender: TObject);
begin
  if nmsmtp1.ReplyNumber = 250 then
    nmsmtp1.Transaction('auth login'); //开始认证
  if nmsmtp1.ReplyNumber =334 then //输入用BASE64编码后的用户名
    begin
      if Length(edit9.Text)>0 then
      nmsmtp1.Transaction(EncodeBase64(edit9.Text));// 用户名
    end;
  if nmsmtp1.ReplyNumber =334 then  //输入用BASE64编码后的用户密码
    begin
      if Length(edit10.Text)>0 then
      nmsmtp1.Transaction(EncodeBase64(edit10.Text)); //密码
    end;
  if nmsmtp1.ReplyNumber =235 then
  begin
    AuthSucc:=true;
  end;
end;

procedure TForm1.NMSMTP1Failure(Sender: TObject);
begin
  StatusBar1.Panels[1].Text:='邮件发送失败!';
end;

procedure TForm1.NMSMTP1SendStart(Sender: TObject);
begin
  StatusBar1.Panels[1].Text:='邮件正在发送......';
end;

procedure TForm1.NMSMTP1Success(Sender: TObject);
begin
  StatusBar1.Panels[1].Text:='邮件发送成功!';
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if opendialog1.Execute then
   edit5.Text:=opendialog1.FileName;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if NMSMTP1.Connected then NMSMTP1.Disconnect;
Action:=cafree;
end;

end.

阅读全文
0 0

相关文章推荐

img
取 消
img