Delphi中发送邮件的代码,不完全参考

 indy10组件的 发送邮件代码,不完全

[本文由iuprg 发表]

[http://www.newsoftstudio.com]

====================================

我的发送代码,仅供参考:

...
...
     SMTP.Username   := SmtpServerUser;
     SMTP.Password := SmtpServerpassword ;
     SMTP.Host := SmtpServerName;

     case   SmtpAuthType of
     0: SMTP.Authtype :=atdefault ;
     1:
       SMTP.Authtype :=atNone ;
     2:
       SMTP.Authtype :=atSASL ;
     end;

     SMTP.Port := SmtpServerPort;
  
   // 'IdMsgSend:tidMsgsend
   with IdMsgSend do

      begin
         Clear;

         Priority := TIdMessagePriority(mpNormal); { Message Priority }
         From.Address:= UserEmail;
     
         Subject := trim(dbeditex3.Text) ; { Subject: header }
         Recipients.EMailAddresses := trim(dbeditex1.Text) ; { To: header }

         Body.Clear;
         Body.Add(dbmemo1.Text);
        /// 附件 :
         for i:=0 to listview2.Items.Count -1 do
         begin
          TIdAttachmentFile.Create(IdMsgSend.MessageParts,
           listview2.Items[i].SubItems.Strings[1]);
          // 9.0版本 TIdAttachment.Create(IdMsgSend.MessageParts,
          // listview2.Items[i].SubItems.Strings[1]);
          //这里是indy 10组件
         end;
          if chkReturnReciept.Checked then
            begin {We set the recipient to the From E-Mail address }
               ReceiptRecipient.Text := From.Text;
            end
           else
            begin {indicate that there is no receipt recipiant}
               ReceiptRecipient.Text := '';
            end;

      end; // idsendmsg

    {authentication settings}

//[本文由iuprg 发表]

//[http://www.newsoftstudio.com]


  try

    if not smtp.Connected then
     SMTP.Connect;

     /// 开始批量发送
     if radiobutton2.Checked then
     begin
       gressbar(listview1.Items.Count,'正在发邮件...');

          IdMsgSend.Recipients.EMailAddresses :=
                listview1.Items[i].SubItems.strings[0];
          try
            if not smtp.Connected then
             SMTP.Connect;
             try
              SMTP.Send(IdMsgSend);
             finally
              smtp.Disconnect;
             end;  // 每次发送必须建立一次连接,因为 有的 mail系统会限制一次连接
                  // 只能发送 2封邮件,而会提示“too many letters during this connection”
          except 
             if showmess(2,1,'往'+listview1.Items[i].SubItems.strings[0]+'发送邮件不成功!请检查输入的邮件地址是否正确/网络连接是否正常,您要继续么?')=1 then
             begin
                 if  smtp.Connected then
                 SMTP.disConnect;
                 break;
             end;
           
          end;

          gresspos(i);

       self.Caption:='发送完毕';
       gressrelease;
     end;

     if  radiobutton1.Checked then
     begin
      gressbar(listview1.Items.Count,'正在发送邮件...');
     if smtp.Connected then
        SMTP.Send(IdMsgSend);

      gresspos(10);
      gressrelease;
     end;


       savemail('1');                     // 保存 邮件,    
  
   if  smtp.Connected then
       SMTP.Disconnect;


    speedbutton2.Enabled :=false;
    showmess(0,9,'邮件发送成功。');
  except

    datamd.adoq_mail.cancel;
     gressrelease;
    if  smtp.Connected then
       SMTP.Disconnect;
    showmess(2,9,'邮件发送不成功!');
  end 

[本文由iuprg 发表]

2006-7-2

[http://www.newsoftstudio.com]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Delphi2010 实现邮件附件收发功能 TIdPOP3组件简介 TIdPOP3 是用来接收邮件服务器的邮件信息到用户端的一个组件。它实现了RFC 1939协议。 在使用TIdPOP3组件时需设置它的几个成员属性。 Host :指定邮件服务器,一般为pop3邮件服务器的地址,如 pop3.126.com。 Username :用户名,也就是邮箱名,如billanking2002@126.com。 Password :邮箱密码,在进行收发邮件时组件需要凭密码进行登录。 其它成员属性 Connected:返回它与邮件服务器的连接状态,这true表示已经连接。 CheckMessages:邮件数,如果连接服务器成功,则可以获得服务器端的邮件数。 成员函数 procedure Connect(const ATimeout: Integer = IdTimeoutDefault); 与服务器连接函数。参数为无效连接时等待的毫秒数。 function RetrieveHeader(const MsgNum: Integer; AMsg: TIdMessage): Boolean; 接收邮件头信息,它有两个参数,MsgNum表示在接收第几个邮件,从1开始,AMsg为邮件消息组件实例。 function Retrieve(const MsgNum: Integer; AMsg: TIdMessage): Boolean; 接收邮件主体信息,它与 RetrieveHeader的参数是一样的。接收的邮件内容将保存在AMsg。 function Delete(const MsgNum: Integer): Boolean; 删除邮件服务器第几个邮件。从1开始。 procedure Disconnect; override; 关闭连接。 TIdMessage组件简介 TIdMessage用来支持邮件消息协议,如POP3,SMTP,NNTP等。TIdMessage支持多用途Internet邮件扩展(MIME)协议。 常用的TIdMessage的属性: Subject:邮件主题,这个字符串经过BASE64编码的。所以在使用时需对它进行解码。 MessageParts:这是TIdMessageParts类的一个实例,它用来存储邮件的信息。如邮件内容及附件信息。在进行解析时需要判断它是否为附件或文本,如果为附件时,其文件名是经过BASE64编码的。判断常量分别为TIdText , TIdAttachment。 Body:这是个字符串列表,包含构成该邮件的正文内容。 Form:发送邮件者的地址信息。 Recipients:收件人地址信息。 BccList:抄送地址列表。 CharSet:指示邮件信息使用的字符集。 ContentType:指定MIME媒体数据类型,描述正文包含的数据,使用户代理决定如何显示数据,常用的有text/html,text/xml。 TIdSMTP组件简介 TIdSMTP是TIdMessageClient派生出的一个简单邮件传输协议和SMTP客户端。 它的主要功能是发送邮件信息。 常用的属性: Host :SMTP邮件服务器的地址,如smtp.126.com。它与上面的POP3地址不一样。 AuthenticationType:服务器认证类型,它有atNone,atLogin两种,即不需要认证和需要凭用户名和密码进行认证。 Username:用户名,这里与TIdPOP3 有点不一样,即它不需要后缀,如billanking2002 Password:邮箱登录密码。如果AuthenticationType设置了atLongin则必须设置密码和用户名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安优小青和他的程序生活

我的文档对您有很大的帮助吗?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值