Allen 2009-06-10 10:39:59 13453 0 0 0 0

用Delphi制作简单的发送邮件程序
---
作者:TTT
日期:200903041702
---
  1,新建TIdSMTP控件SMTP1 (此控件在Indy Clients面板中)
  2,新建TIdMessage控件:IdMsg (此控件在Indy Misc面板中)
  3,添加引用单元:IdComponent, IdTCPConnection, IdTCPClient, IdMessageClient,  IdSMTP, IdBaseComponent,  IdMessage;
  4,设置IdSMTP1的Host,端口号,用户名,密码等,可以在属性面板中设置,也可以用以下代码设置:
        SMTP1.AuthenticationType:=atLogin;
        SMTP1.Host:=^10.105.10.19^;
        SMTP1.Port:=25;
        SMTP1.UserId:=^test^;
        SMTP1.Password:=^abc^;
        //用INI文件设置
        {SMTP1.AuthenticationType=atLogin;
        SMTP1.Host:=cfReadIniFile(^Smtp_setting^,^Host^,^^);
        SMTP1.Port:=strToint(cfReadIniFile(^Smtp_setting^,^Port^,^^));
        SMTP1.UserId:=cfReadIniFile(^Smtp_setting^,^User_ID^,^^);
        SMTP1.Password:=cfReadIniFile(^Smtp_setting^,^Password^,^^);}
  5,建一个按钮,将以下代码加入单击事件中:


procedure TForm1.Button1Click(Sender: TObject);
var
  i,j,k : integer;
  pFrom_Address,pRece_Address : string;
  pSubject : string;
begin
    SMTP1.Host:=^10.105.10.19^;
    SMTP1.Port:=25;
    SMTP1.UserId:=^test^;
    SMTP1.Password:=^abc^;

       smtp1.Connect;   //连接smtp
       try
           with IdMsg do begin
               //设定邮件主题
               Subject:=^ 这是一封测试邮件^;
               //设定邮件发送人地址
               From.address :=^[email protected]^;

               //设定邮件收件人地址
               //--这段是用多个地址的字符串来添加收件人地址
               {J:=0;
               k:=0;
               for i:=1 to length(pRece_Address) do begin
                   if (copy(pRece_Address,i,1)=^,^) or (copy(pRece_Address,i,1)=^;^) then begin
                       if (trim(copy(pRece_Address,j+1,i-j-1))<>^^) and (pos(^@^, copy(pRece_Address,j+1,i-j-1))>0) then begin
                           idmsg.Recipients.Add;
                           idmsg.Recipients[k].Address:= trim(copy(pRece_Address,j+1,i-j-1));
                           inc(k);

                       end;
                       J:=i;
                   end;
               end;
                }
               //--这段是直接添加单个收件人地址
               idmsg.Recipients.Add;
               Recipients[0].Address:=^[email protected]^;
               {idmsg.Recipients.Add;
               Recipients[1].Address:=^邮件地址2^;
               idmsg.Recipients.Add;
               Recipients[2].Address:=^邮件地址3^;   }

               //清空邮件内容
               body.Clear;
               //添加邮件内容
               //Body.Assign(Memo2.Lines);  //从指定的控件中获
               Body.Add(^邮件内容^);  //直接添加字符串
           end;
           SMTP1.Send(IdMsg);
       finally
            SMTP1.Disconnect;
       end;
end;


  (以上代码在Delphi6中运行通过)


Tag: Delphi技巧 TTTBLOG
我也要发一个   ·   返回首页   ·   返回[Delphi]   ·   前一个   ·   下一个
欢迎评论
未登录,
请先 [ 注册 ] or [ 登录 ]
(一分钟即可完成注册!)
返回首页     ·   返回[Delphi]   ·   返回顶部