⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pop3_protocol_class_wrapper.shtml.htm

📁 mfc资料集合5
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Zafir Anjum">
   <TITLE>Internet - POP3 Protocol wrapper class</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><td>
</tr>
</table>


<CENTER><H3><FONT COLOR="#AOAO99">POP3 Protocol wrapper class</FONT></H3></CENTER>
<HR>


<P>This article was contributed by <A HREF="mailto:aasif@khi.compol.com">Asif
Rasheed</A>.

<P>On the Internet, electronic mail (e-mail) is the most widely used application.
According to experts, of all TCP connections that Internet users establish,
about one-half are for sending and receiving e-mail messages. So it is
impossible for any Internet programmer to leave this area.

<P>Usually e-mail systems use SMTP as a mail sending Protocol and POP3
as a mail receiving Protocol. For details about these Protocols you can
refere to RCF 821 (for SMTP) and RFC 1225 (for POP3). Since in Windows
environment we have MAPI for e-mail handling but it do not provide enough
power to commuincate with e-mail server directly.

<P>Here I developed a class for POP 3 protocol. In this class I used CSocket
as class member (surperise not inherent it), so that I can used it in Threads.
POP Class has methods which are resembled with POP 3 protocol commands.
Following is code of the class with comments.


<P><FONT COLOR="#990000">/*--------------------------------------------------------------------</FONT>
<BR><FONT COLOR="#990000">&nbsp;&nbsp;&nbsp;&nbsp; Pop.h : main header
file for the POP application</FONT>
<BR><FONT COLOR="#990000">-----------------------------------------------------------------------*/</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">#if !defined(AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_)</FONT>
<BR><FONT COLOR="#990000">#define AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">#define CONNECTION_CHECK 0</FONT>
<BR><FONT COLOR="#990000">#define USER_CHECK&nbsp; 1</FONT>
<BR><FONT COLOR="#990000">#define PASSWORD_CHECK 2</FONT>
<BR><FONT COLOR="#990000">#define QUIT_CHECK&nbsp; 3</FONT>
<BR><FONT COLOR="#990000">#define DELETE_CHECK 4</FONT>
<BR><FONT COLOR="#990000">#define RSET_CHECK&nbsp; 5</FONT>
<BR><FONT COLOR="#990000">#define STAT_CHECK&nbsp; 6</FONT>
<BR><FONT COLOR="#990000">#define NOOP_CHECK&nbsp; 7</FONT>
<BR><FONT COLOR="#990000">#define LIST_CHECK&nbsp; 8</FONT>
<BR><FONT COLOR="#990000">#define RETR_CHECK&nbsp; 9</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">/////////////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#990000">class CPop</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">public:</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL List();</FONT>
<BR><FONT COLOR="#990000">&nbsp;CWordArray m_SizeOfMsg;</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString GetErrorMessage();&nbsp;&nbsp;&nbsp;
// If there is any error this will return it method</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString GetPassword();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// Getting Password stored in class</FONT>
<BR><FONT COLOR="#990000">&nbsp;void SetPassword(CString&amp; Password);&nbsp;&nbsp;&nbsp;
// Setting Password in class</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString GetUser();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// Getting user name stored in class</FONT>
<BR><FONT COLOR="#990000">&nbsp;void SetUser(CString&amp; User);&nbsp;
// Setting user name in class</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString GetHost();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// Getting Host name (email server name) stored in class</FONT>
<BR><FONT COLOR="#990000">&nbsp;void SetHost(CString&amp; Host);&nbsp;&nbsp;&nbsp;
// Setting Host name (email server name) in class</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL Connect();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// Connecting to email server</FONT>
<BR><FONT COLOR="#990000">&nbsp;int GetTotalMailSize();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// it returns the Total Mail Size</FONT>
<BR><FONT COLOR="#990000">&nbsp;int GetNumberOfMails();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// It return the number of mails</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString GetMsgContents();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL Statistics();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// issue the STAT command on email server</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL Retrieve(int MsgNumber);&nbsp;&nbsp;&nbsp;
// Getting any particular mail message</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL Reset();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// issue the reset command on email server</FONT>
<BR><FONT COLOR="#990000">&nbsp;int&nbsp; GetMessageSize(int MsgNumber);&nbsp;&nbsp;&nbsp;
// Return a size of any particular mail</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL Noop();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// issue the NOOP command on email server</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL Disconnect();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
// issue the QUIT command on email server</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL Delete(int&amp; MsgNumber); // Deleteing
a particular message from email server</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL Connect(CString&amp; Host, CString&amp;
User, CString&amp; Password);</FONT>
<BR><FONT COLOR="#990000">&nbsp;CPop();</FONT>
<BR><FONT COLOR="#990000">&nbsp;virtual ~CPop();</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">private:</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString m_ErrorMessage;</FONT>
<BR><FONT COLOR="#990000">&nbsp;BOOL CheckResponse(int ResponseType);</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString m_Password;</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString m_User;</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString m_Host;</FONT>
<BR><FONT COLOR="#990000">&nbsp;CString m_MsgContents;</FONT>
<BR><FONT COLOR="#990000">&nbsp;int m_TotalSize;</FONT>
<BR><FONT COLOR="#990000">&nbsp;int m_NumberMail;</FONT>
<BR><FONT COLOR="#990000">&nbsp;CSocket m_PopServer;</FONT>
<BR><FONT COLOR="#990000">};</FONT>
<BR><FONT COLOR="#990000">/#endif // !defined(AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_)</FONT>
<BR><FONT COLOR="#990000"></FONT>&nbsp;<FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">/*-----------------------------------------------------------------------------------------------</FONT>
<BR><FONT COLOR="#990000">// Pop.cpp : Defines the class behaviors for
the application.</FONT>
<BR><FONT COLOR="#990000">---------------------------------------------------------------------------------------------------*/</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">#include "stdafx.h"</FONT>
<BR><FONT COLOR="#990000">#include "Pop.h"</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">#ifdef _DEBUG</FONT>
<BR><FONT COLOR="#990000">#define new DEBUG_NEW</FONT>
<BR><FONT COLOR="#990000">#undef THIS_FILE</FONT>
<BR><FONT COLOR="#990000">static char THIS_FILE[] = __FILE__;</FONT>
<BR><FONT COLOR="#990000">#endif</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#990000">// CPop Class</FONT>
<BR><FONT COLOR="#990000">//////////////////////////////////////////////////////////////////////</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">//////////////////////////////////////////////////////////////////////</FONT>
<BR><FONT COLOR="#990000">// Construction/Destruction</FONT>
<BR><FONT COLOR="#990000">//////////////////////////////////////////////////////////////////////</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">CPop::CPop()</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">&nbsp;m_PopServer.Create();</FONT>
<BR><FONT COLOR="#990000">}</FONT>
<BR><FONT COLOR="#990000">//{4EEC1C91-6BE1-11d1-8824-00001C302581}</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">CPop::~CPop()</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">&nbsp;m_PopServer.Close();</FONT>
<BR><FONT COLOR="#990000">}</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">BOOL CPop::Connect(CString &amp; Host, CString
&amp; User, CString &amp; Password)</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">&nbsp;char buf [512];</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">&nbsp;if (!m_PopServer.Connect(Host,110)) // 110
Pop3 Port</FONT>
<BR><FONT COLOR="#990000">&nbsp;{</FONT>
<BR><FONT COLOR="#990000">&nbsp; m_ErrorMessage = _T("Server cannot be
connected");</FONT>
<BR><FONT COLOR="#990000">&nbsp; return FALSE;</FONT>
<BR><FONT COLOR="#990000">&nbsp;}</FONT>
<BR><FONT COLOR="#990000">&nbsp;else</FONT>
<BR><FONT COLOR="#990000">&nbsp;{</FONT>
<BR><FONT COLOR="#990000">&nbsp; if(CheckResponse(CONNECTION_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000">&nbsp;&nbsp; return FALSE;</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">&nbsp; wsprintf (buf, "USER %s\r\n", (LPCSTR)
User);</FONT>
<BR><FONT COLOR="#990000">&nbsp; m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000">&nbsp; if(CheckResponse(USER_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000">&nbsp;&nbsp; return FALSE;</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">&nbsp; wsprintf (buf, "PASS %s\r\n", (LPCSTR)
Password);</FONT>
<BR><FONT COLOR="#990000">&nbsp; m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000">&nbsp; if (CheckResponse(PASSWORD_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000">&nbsp;&nbsp; return FALSE;</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">&nbsp; return TRUE;</FONT>
<BR><FONT COLOR="#990000">&nbsp;}</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">}</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">BOOL CPop::Delete(int &amp; MsgNumber)</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">&nbsp;char buf [512];</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">&nbsp;wsprintf (buf, "DELE %d\r\n",MsgNumber );</FONT>
<BR><FONT COLOR="#990000">&nbsp;m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000">&nbsp;if (CheckResponse(DELETE_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000">&nbsp; return FALSE;</FONT>
<BR><FONT COLOR="#990000">&nbsp;else</FONT>
<BR><FONT COLOR="#990000">&nbsp; return TRUE;</FONT>
<BR><FONT COLOR="#990000">}</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">BOOL CPop::Disconnect()</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">&nbsp;char buf [512];</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">&nbsp;wsprintf (buf, "QUIT \r\n");</FONT>
<BR><FONT COLOR="#990000">&nbsp;m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000">&nbsp;if (CheckResponse(QUIT_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000">&nbsp; return FALSE;</FONT>
<BR><FONT COLOR="#990000">&nbsp;else</FONT>
<BR><FONT COLOR="#990000">&nbsp; return TRUE;</FONT>
<BR><FONT COLOR="#990000">}</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">BOOL CPop::Noop()</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">&nbsp;char buf [512];</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">&nbsp;wsprintf (buf, "NOOP&nbsp; \r\n");</FONT>
<BR><FONT COLOR="#990000">&nbsp;m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000">&nbsp;if (CheckResponse(NOOP_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000">&nbsp; return FALSE;</FONT>
<BR><FONT COLOR="#990000">&nbsp;else</FONT>
<BR><FONT COLOR="#990000">&nbsp; return TRUE;</FONT>
<BR><FONT COLOR="#990000">}</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">// Return the Msg Size for given msg number</FONT>
<BR><FONT COLOR="#990000">int CPop::GetMessageSize(int MsgNumber)</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">&nbsp;if(m_SizeOfMsg.GetSize() &lt; MsgNumber+1)</FONT>
<BR><FONT COLOR="#990000">&nbsp; return 0;</FONT>
<BR><FONT COLOR="#990000">&nbsp;else</FONT>
<BR><FONT COLOR="#990000">&nbsp; return m_SizeOfMsg[MsgNumber+1];</FONT>
<BR><FONT COLOR="#990000">}</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">BOOL CPop::Reset()</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">&nbsp;char buf [512];</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">&nbsp;wsprintf (buf, "RSET \r\n");</FONT>
<BR><FONT COLOR="#990000">&nbsp;m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000">&nbsp;if (CheckResponse(RSET_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000">&nbsp; return FALSE;</FONT>
<BR><FONT COLOR="#990000">&nbsp;else</FONT>
<BR><FONT COLOR="#990000">&nbsp; return TRUE;</FONT>
<BR><FONT COLOR="#990000">}</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">// MsgContents will hold the msg body</FONT>
<BR><FONT COLOR="#990000">BOOL CPop::Retrieve(int&nbsp; MsgNumber)</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000">&nbsp;char buf [512];</FONT><FONT COLOR="#990000"></FONT>

<P><FONT COLOR="#990000">&nbsp;wsprintf (buf, "RETR %d\r\n",MsgNumber );</FONT>
<BR><FONT COLOR="#990000">&nbsp;m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000">&nbsp;if (CheckResponse(RETR_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000">&nbsp; return FALSE;</FONT>
<BR><FONT COLOR="#990000">&nbsp;else</FONT>
<BR><FONT COLOR="#990000">&nbsp; return TRUE;</FONT>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -