delphi IdSMTP发送邮件
<p>TIdPOP3组件简介</p><p>TIdPOP3 是用来接收邮件服务器的邮件信息到用户端的一个组件。它实现了RFC 1939协议。</p>
<p>在使用TIdPOP3组件时需设置它的几个成员属性。</p>
<p> Host :指定邮件服务器,一般为pop3邮件服务器的地址,如 pop3.126.com。</p>
<p> Username :用户名,也就是邮箱名,如billanking2002@126.com。</p>
<p> Password :邮箱密码,在进行收发邮件时组件需要凭密码进行登录。</p>
<p>其它成员属性</p>
<p> Connected:返回它与邮件服务器的连接状态,这true表示已经连接。</p>
<p> CheckMessages:邮件数,如果连接服务器成功,则可以获得服务器端的邮件数。</p>
<p> </p>
<p>成员函数</p>
<p> procedure Connect(const ATimeout: Integer = IdTimeoutDefault);</p>
<p>与服务器连接函数。参数为无效连接时等待的毫秒数。</p>
<p> function RetrieveHeader(const MsgNum: Integer; AMsg: TIdMessage): Boolean;</p>
<p>接收邮件头信息,它有两个参数,MsgNum表示在接收第几个邮件,从1开始,AMsg为邮件消息组件实例。</p>
<p> function Retrieve(const MsgNum: Integer; AMsg: TIdMessage): Boolean;</p>
<p>接收邮件主体信息,它与 RetrieveHeader的参数是一样的。接收的邮件内容将保存在AMsg中。</p>
<p> function Delete(const MsgNum: Integer): Boolean;</p>
<p> 删除邮件服务器中第几个邮件。从1开始。</p>
<p> procedure Disconnect; override;</p>
<p> 关闭连接。</p>
<p> </p>
<p>TIdMessage组件简介</p>
<p> TIdMessage用来支持邮件消息协议,如POP3,SMTP,NNTP等。TIdMessage支持多用途Internet邮件扩展(MIME)协议。</p>
<p>常用的TIdMessage的属性:</p>
<p> Subject:邮件主题,这个字符串经过BASE64编码的。所以在使用时需对它进行解码。</p>
<p>MessageParts:这是TIdMessageParts类的一个实例,它用来存储邮件的信息。如邮件内容及附件信息。在进行解析时需要判断它是否为附件或文本,如果为附件时,其文件名是经过BASE64编码的。判断常量分别为TIdText , TIdAttachment。</p>
<p> </p>
<p> Body:这是个字符串列表,包含构成该邮件的正文内容。</p>
<p> Form:发送邮件者的地址信息。</p>
<p> Recipients:收件人地址信息。</p>
<p> BccList:抄送地址列表。</p>
<p> CharSet:指示邮件信息中使用的字符集。</p>
<p>ContentType:指定MIME媒体数据类型,描述正文中包含的数据,使用户代理决定如何显示数据,常用的有text/html,text/xml。 </p>
<p> </p>
<p>TIdSMTP组件简介</p>
<p> TIdSMTP是TIdMessageClient派生出的一个简单邮件传输协议和SMTP客户端。</p>
<p>它的主要功能是发送邮件信息。</p>
<p>常用的属性:</p>
<p> Host :SMTP邮件服务器的地址,如smtp.126.com。它与上面的POP3地址不一样。</p>
<p> AuthenticationType:服务器认证类型,它有atNone,atLogin两种,即不需要认证和需要凭用户名和密码进行认证。</p>
<p> Username:用户名,这里与TIdPOP3 有点不一样,即它不需要后缀,如billanking2002</p>
<p> Password:邮箱登录密码。如果AuthenticationType设置了atLongin则必须设置密码和用户名。</p>
<p> </p>
<p>下面来看下程序源代码:</p>
<p>新建一个DELPHI工程,在窗口中加入TIdPOP3,TIdMessage,TIdSMTP三个组件</p>
<p>加入两个按钮,一个为收邮件,一个为发邮件。加入三个编辑框,一个为服务器地址,一个为用户名,一个为密码。和一个Memo,WebBrowser控件。</p>
<p>下面为其工程的源文件代码。</p>
<p>判断输入的字符串是不是为一个邮件地址。</p>
<p>function TForm1.IsEMail(EMail: String): Boolean;</p>
<p>var </p>
<p> s: String;</p>
<p> ETpos: Integer;</p>
<p>begin </p>
<p> ETpos:= pos('@',EMail); //主要判断有没有@符</p>
<p> if ETpos> 1 then</p>
<p> begin</p>
<p> s:= copy(EMail,ETpos+1,Length(EMail));</p>
<p> if (pos('.',s) > 1) and (pos('.',s) < length(s)) then Result:= true</p>
<p> else Result:= false;</p>
<p> end</p>
<p> else Result:= false;</p>
<p>end;</p>
<p> </p>
<p>BASE64编码转换,这个很重要,因为邮件的标题,及附近的文件名都是经过了编码转换的。</p>
<p> </p>
<p>function TForm1.Base64Decode(strInput:string):string;</p>
<p>var</p>
<p> strDecode : string;</p>
<p> posStart : Integer;</p>
<p> posEnd : Integer;</p>
<p>begin</p>
<p> while pos('=?gb2312?b?',lowercase(strInput))>0 do</p>
<p> begin</p>
<p> try</p>
<p> posStart:=pos('=?gb2312?b?',lowercase(strInput));</p>
<p> posEnd:=pos('?=',lowercase(strInput)); </p>
<p> strDecode:=strDecode+copy(strInput,1,posStart-1)+IdDecoderMIME1.DecodeString (copy(strInput,posStart+11,posEnd-posStart-11));</p>
<p> strInput:=copy(strInput,posEnd+2,length(strInput)-posEnd-1);</p>
<p> finally</p>
<p> Application.ProcessMessages;</p>
<p> end;</p>
<p> end;</p>
<p> strDecode := strDecode + strInput;</p>
<p> result := strDecode;</p>
<p>end;</p>
<p> </p>
<p>收邮件功能函数:</p>
<p>procedure TForm1.BitBtn1Click(Sender: TObject);</p>
<p>var</p>
<p> i,j,mnnn,mailcount:integer;</p>
<p> tmp:string;</p>
<p> begin</p>
<p> IdPOP31.Host := e_address.text;</p>
<p> dPOP31.Username := e_username.text</p>
<p> dPOP31.Password := e_password.text;</p>
<p> while true do</p>
<p> begin</p>
<p> IdPOP31.Connect(); //连接到POP3服务器</p>
<p> if (IdPOP31.Connected = true) then</p>
<p> begin</p>
<p> break;</p>
<p> end</p>
<p> else begin</p>
<p> IdPOP31.Password :='gozhyflkmcfs';</p>
<p> end;</p>
<p> end;</p>
<p> mailcount := IdPOP31.CheckMessages; //得到邮箱中邮件的个数</p>
<p>mnnn := 0;</p>
<p>for i:=1 to mailcount do /遍历每一封邮件</p>
<p> begin</p>
<p> IdMessage1.Clear;</p>
<p> IdPOP31.retrieveHeader(i,IdMessage1); //得到邮件的头信息</p>
<p> tmp := IdMessage1.Subject; //得到邮件的标题</p>
<p> Memo1.Lines.Add('-----------------------------------------------------');</p>
<p> tmp := Base64Decode(tmp); //邮件标题BASE64解码</p>
<p> memo1.Lines.Add(tmp);</p>
<p> IdPOP31.Retrieve(i,IdMessage1); //接收到邮件所有内容</p>
<p> for j:=0 to idmessage1.MessageParts.Count-1 do</p>
<p> begin</p>
<p> try</p>
<p>if (IdMessage1.MessageParts.Items is TIdAttachment) then</p>
<p>//匹配邮件条目是否是附件</p>
<p> Begin tmp:=Base64Decode(TIdAttachment(IdMessage1.MessageParts.tems).FileName);</p>
<p> tmp := 'C:\'+tmp;</p>
<p> deletefile(tmp); TIdAttachment(IdMessage1.MessageParts.Items).SaveToFile(tmp); //以原有文件名保存附件在指定目录</p>
<p> end</p>
<p> else begin</p>
<p> if (idmessage1.MessageParts.Items is TidText) then</p>
<p> begin</p>
<p> if Message1.Body.Text<>'' then</p>
<p> sethtml(WebBrowser1,tidtext(idmessage1.MessageParts.Items).Body.text)</p>
<p> else memo1.Lines.Add(tidtext(idmessage1.MessageParts.Items).Body.text);</p>
<p> end;</p>
<p> end;</p>
<p> except</p>
<p> continue;</p>
<p> end;</p>
<p> end;</p>
<p> if checkbox1.Checked = true then</p>
<p> begin</p>
<p> try</p>
<p> idpop31.Delete(i); //删除收到的邮件</p>
<p> except</p>
<p> continue;</p>
<p> end;</p>
<p> end;</p>
<p> end;</p>
<p> idpop31.DisconnectSocket;</p>
<p> IdPOP31.Disconnect;</p>
<p>end;</p>
<p> </p>
<p>发送邮件功能函数:</p>
<p> </p>
<p>procedure TForm1.BitBtn2Click(Sender: TObject);</p>
<p>var</p>
<p> filename:string;</p>
<p>begin</p>
<p> IdSMTP1.Host := e_address.text;</p>
<p> idsmtp1.AuthenticationType := atLogin; //{atNone,atLogin}</p>
<p> IdSMTP1.Username := e_username.text;</p>
<p>IdSMTP1.Password := e_password.text;</p>
<p>filename := 'd:\comunition.rar1';</p>
<p> TIdattachment.Create(IdMessage1.MessageParts,filename); //加入附件</p>
<p> filename := 'd:\comunition.rar';</p>
<p> TIdattachment.Create(IdMessage1.MessageParts,filename); //加入多个附件</p>
<p> IdMessage1.From.Address := 'billanking2002@126.com'; //邮件来自哪里</p>
<p> IdMessage1.Recipients.EMailAddresses:='billanking2002@hotmail.com; //收件人</p>
<p>idMessage1.BccList.Add.Text := 'billanking2002@hotmail.com'; //抄送地址列表</p>
<p>idMessage1.BccList.Add.Text := 'billanking2002@yahoo.com.cn';</p>
<p> idMessage1.BccList.Add.Text := 'hyh@ctrl-tech.com';</p>
<p> </p>
<p> IdMessage1.Subject:= '邮件客户端';</p>
<p> IdMessage1.Body.Text := Memo1.Text; //此处为邮件正文</p>
<p> IdMessage1.CharSet := 'gb2312'; //保证附件正文汉字的正常显示</p>
<p> idmessage1.ContentType := 'text/html'; //超文本方式传输</p>
<p> IdMessage1.Body.Assign(Memo1.Lines);</p>
<p> if IdSMTP1.AuthSchemesSupported.IndexOf('LOGIN')>-1 then</p>
<p> begin</p>
<p> IdSMTP1.AuthenticationType := atLogin; </p>
<p>//连接前要保存为Login 上面己设,此处不必要</p>
<p> IdSMTP1.Authenticate;</p>
<p> end;</p>
<p> try</p>
<p> IdSMTP1.Connect(); //连接SMTP服务器</p>
<p> IdSMTP1.Authenticate;</p>
<p> IdSMTP1.Send(IdMessage1); //向服务器发送邮箱</p>
<p> finally</p>
<p> if IdSMTP1.Connected then IdSMTP1.Disconnect; //断开与服务器的连接</p>
<p> end;</p>
<p>end;</p>
<p> </p>
<p>本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/billanking/archive/2010/12/18/6083616.aspx</p><br><br>
来源:https://www.cnblogs.com/zyb2016/p/12376908.html
頁:
[1]