实现邮件发送(delphi)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值