Knocker 发表于 2006-3-26 20:08

用C++BUILDER实现POP3电子邮件的接收

王传胜
<P><FONT color=#000000>通过对C++BUILDER的组件设置,我们可以创建一个自己的POP3电子邮件接收程序。 </FONT>
<P><FONT color=#000000>一、建立一个工程文件 </FONT>
<P><FONT color=#000000>二、建立一个新的表单,设置它的caption属性为Getmail,在上面放置一个TpageControl控件,单击右键创建三个TabSheet(1,2,3)。在TabSheet1上放置四个Edit(1,2,3,4)控件并清除它们的Text属性及两个Botton(1,2),其中Edit1用以输入POP3服务器的名称;Edit2用以设置访问端口号,我们设置它为110,并将它的Text属性设为110;Edit3用以输入用户名;Edit4用以输入口令。设Botton1的caption为“连接”,Botton2的caption为“断开连接”。在TabSheet2上放置五个Edit(5,6,7,8,9)控件、两个Memo控件和三个Botton(3,4,5),Edit5用来显示邮件的编号;Edit6、Edit7、Edit8和Edit9分别用来显示邮件的发送者、邮件的主题、大小及ID号;Memo1用来显示邮件的正文;Memo2用来显示邮件头;Botton3、Botton4、Botton5的caption属性分别设置为“删除”、“邮件”和“邮件信息”。在TabSheet3上放置两个Botton和一个Memo控件,其中Botton6的caption属性为“邮件列表”;Botton7的caption为“清除列表”;Memo的用途自然为显示邮件列表。在表单的底部放置一个StatusBar控件用以显示工作状态。最后,最重要的是在表单上放置一个NMPOP3控件,在这个程序里,它是我们与POP3服务器通讯的核心。 </FONT>
<P><FONT color=#000000>三、编写代码 </FONT>
<P><FONT color=#000000>为Botton1的OnClick事件编写代码:</FONT>
<P><FONT color=#000000>   NMPOP31-〉AttachFilePath = ″.″;  //设定邮件的存储目录 </FONT>
<P><FONT color=#000000>   NMPOP31-〉DeleteOnRead = false;    //读完邮件后是否删除 </FONT>
<P><FONT color=#000000>   NMPOP31-〉ReportLevel = Status_Basic; //控制OnStatus事件和Status属性报告的资料的数量 </FONT>
<P><FONT color=#000000>   NMPOP31-〉TimeOut = 20000;          //设置超时的时间 </FONT>
<P><FONT color=#000000>   NMPOP31-〉Host = Edit1-〉Text;    //设置pop3服务器的主机名 </FONT>
<P><FONT color=#000000>   NMPOP31-〉Port = StrToInt(Edit2-〉Text); //设置pop3服务器的端口号 </FONT>
<P><FONT color=#000000>   NMPOP31-〉UserID = Edit3-〉Text;   //取得用户名 </FONT>
<P><FONT color=#000000>   NMPOP31-〉Password = Edit4-〉Text; //得到口令 </FONT>
<P><FONT color=#000000>   NMPOP31-〉Connect(); //开始连接 </FONT>
<P><FONT color=#000000>   Label10-〉Caption = ″# of Messages: ″+IntToStr(NMPOP31-〉MailCount); //显示邮件的数量 </FONT>
<P><FONT color=#000000>为Botton2的OnClick事件编写代码:</FONT>
<P><FONT color=#000000>   NMPOP31-〉Disconnect();  //使之能够与服务器断开连接 </FONT>
<P><FONT color=#000000>为Botton3的OnClick事件编写代码,当点击它时可以删除邮件:</FONT>
<P><FONT color=#000000>   NMPOP31-〉DeleteMailMessage(StrToInt(Edit5-〉Text)); </FONT>
<P><FONT color=#000000>为Botton4的OnClick事件编写代码,当点击它时显示整个邮件:</FONT>
<P><FONT color=#000000>   NMPOP31-〉GetMailMessage(StrToInt(Edit5-〉Text)); </FONT>
<P><FONT color=#000000>   Edit6-〉Text = NMPOP31-〉MailMessage-〉From; //通过MailMessage的From属性得到发信人 </FONT>
<P><FONT color=#000000>   Edit7-〉Text = NMPOP31-〉MailMessage-〉Subject; //通过MailMessage的Subjecet属性得到主题 </FONT>
<P><FONT color=#000000>   Edit9-〉Text = NMPOP31-〉MailMessage-〉MessageId; //通过MailMessage的MessageIds属性得到Id </FONT>
<P><FONT color=#000000>   Memo2-〉Lines-〉Assign(NMPOP31-〉MailMessage-〉Head); </FONT>
<P><FONT color=#000000>   Memo1-〉Lines-〉Assign(NMPOP31-〉MailMessage-〉Body); </FONT>
<P><FONT color=#000000>   if (NMPOP31-〉MailMessage-〉Attachments-〉Text != ″″) </FONT>
<P><FONT color=#000000>      ShowMessage(″Attachments:\n″+NMPOP31-〉MailMessage-〉Attachments-〉Text); </FONT>
<P><FONT color=#000000>为Botton5的OnClick事件编写代码,点击它时将在Memo中显示邮件的概要信息:</FONT>
<P><FONT color=#000000>   NMPOP31-〉GetSummary(StrToInt(Edit5-〉Text)); </FONT>
<P><FONT color=#000000>   Edit6-〉Text = NMPOP31-〉Summary-〉From; </FONT>
<P><FONT color=#000000>   Edit7-〉Text = NMPOP31-〉Summary-〉Subject; </FONT>
<P><FONT color=#000000>   Edit8-〉Text = IntToStr(NMPOP31-〉Summary-〉Bytes); </FONT>
<P><FONT color=#000000>   Edit9-〉Text = NMPOP31-〉Summary-〉MessageId; </FONT>
<P><FONT color=#000000>为Botton6的OnClick事件编写代码,点击它时将在Memo3中显示邮件列表:</FONT>
<P><FONT color=#000000>   NMPOP31-〉List(); //得到邮件的数量和大小的列表 </FONT>
<P><FONT color=#000000>为Botton7的OnClick事件编写代码,点击它时将清除Memo3中的邮件列表:</FONT>
<P><FONT color=#000000>   Memo3-〉Clear(); </FONT>
<P><FONT color=#000000>为NMPOP3的事件编写代码</FONT>
<P><FONT color=#000000>1. 为它的OnConnect事件编写代码 </FONT>
<P><FONT color=#000000>   StatusBar1-〉SimpleText = ″Connected″; //连接后在状态栏显示已连接 </FONT>
<P><FONT color=#000000>2. 为它的OnConnectionFailed事件编写代码 </FONT>
<P><FONT color=#000000>   ShowMessage(″Connection Failed″); //若连接失败则报告 </FONT>
<P><FONT color=#000000>3. 为它的OnConnectionRequired事件编写代码 </FONT>
<P><FONT color=#000000>   AnsiString BoxCaption; </FONT>
<P><FONT color=#000000>   AnsiString BoxMsg; </FONT>
<P><FONT color=#000000>   BoxCaption = ″请求连接″; </FONT>
<P><FONT color=#000000>   BoxMsg = ″请求连接,连接吗?″; </FONT>
<P><FONT color=#000000>   if (MessageBox(0, &BoxMsg[1], &BoxCaption[1], MB_YESNO + MB_ICONEXCLAMATION) == IDYES) </FONT>
<P><FONT color=#000000>      { handled = TRUE; </FONT>
<P><FONT color=#000000>        Form1-〉Button1Click(this); <BR>      } </FONT>
<P><FONT color=#000000>4.为它的OnDisconnect事件编写代码 </FONT>
<P><FONT color=#000000>    if (StatusBar1 != 0) </FONT>
<P><FONT color=#000000>       StatusBar1-〉SimpleText = ″Disconnected″; //断开连接后在状态栏显示已断开 </FONT>
<P><FONT color=#000000>5. 为它的OnFailuer事件编写代码 </FONT>
<P><FONT color=#000000>   ShowMessage(″操作失败!″); </FONT>
<P><FONT color=#000000>6. 为它的OnHostResovled事件编写代码 </FONT>
<P><FONT color=#000000>   StatusBar1-〉SimpleText = ″Host Resolved″; </FONT>
<P><FONT color=#000000>7.为它的OnInvailidHost事件编写代码 </FONT>
<P><FONT color=#000000>  AnsiString NewHost; </FONT>
<P><FONT color=#000000>  if (InputQuery(″非法主机名!″, ″请重新输入新的主机名″,NewHost)) </FONT>
<P><FONT color=#000000>    { NMPOP31-〉Host = NewHost; </FONT>
<P><FONT color=#000000>      handled = true; <BR>    } </FONT>
<P><FONT color=#000000>8. 为它的OnList事件编写代码 </FONT>
<P><FONT color=#000000>   if (Msg 〈 2) </FONT>
<P><FONT color=#000000>     { Memo3-〉Clear(); </FONT>
<P><FONT color=#000000>       Memo3-〉Lines-〉Add(″Message Number / Message Size″); <BR>      } </FONT>
<P><FONT color=#000000>    Memo3-〉Lines-〉Add(IntToStr(Msg)+″/ ″+IntToStr(Size)); </FONT>
<P><FONT color=#000000>9. 为它的OnListPacketRecvd事件编写代码 </FONT>
<P><FONT color=#000000>   StatusBar1-〉SimpleText = IntToStr(NMPOP31-〉BytesRecvd)+″ bytesof ″+IntToStr(NMPOP31-〉BytesTotal)+″ Received″; </FONT>
<P><FONT color=#000000>10. 为它的OnReset事件编写代码 </FONT>
<P><FONT color=#000000>    ShowMessage(″重置删除标志″); </FONT>
<P><FONT color=#000000>11. 为它的OnRetrieveEnd事件编写代码 </FONT>
<P><FONT color=#000000>    Form1-〉Cursor = crDefault; </FONT>
<P><FONT color=#000000>    StatusBar1-〉SimpleText = ″恢复完成″; </FONT>
<P><FONT color=#000000>12. 为它的OnRetrieveStart事件编写代码 <BR>  <BR>    Form1-〉Cursor =crHourGlass; </FONT>
<P><FONT color=#000000>    StatusBar1-〉SimpleText = ″恢复开始″; </FONT>
<P><FONT color=#000000>13. 为它的OnStatus事件编写代码 </FONT>
<P><FONT color=#000000>    if (StatusBar1 != 0) </FONT>
<P><FONT color=#000000>      StatusBar1-〉SimpleText = Status; </FONT>
<P><FONT color=#000000>14. 为它的OnSuccess事件编写代码 </FONT>
<P><FONT color=#000000>    StatusBar1-〉SimpleText = ″操作成功″。 </FONT>
<P><FONT color=#000000>    至此,我们的电子邮件程序就完成了,编译运行后,输入你的pop3服务器名,你就可以接收到你的电子邮件了,怎么样?试一试吧。 </FONT></P>


页: [1]

编程论坛