📄 pop3_protocol_class_wrapper.shtml.htm
字号:
<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"> 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 1</FONT>
<BR><FONT COLOR="#990000">#define PASSWORD_CHECK 2</FONT>
<BR><FONT COLOR="#990000">#define QUIT_CHECK 3</FONT>
<BR><FONT COLOR="#990000">#define DELETE_CHECK 4</FONT>
<BR><FONT COLOR="#990000">#define RSET_CHECK 5</FONT>
<BR><FONT COLOR="#990000">#define STAT_CHECK 6</FONT>
<BR><FONT COLOR="#990000">#define NOOP_CHECK 7</FONT>
<BR><FONT COLOR="#990000">#define LIST_CHECK 8</FONT>
<BR><FONT COLOR="#990000">#define RETR_CHECK 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"> BOOL List();</FONT>
<BR><FONT COLOR="#990000"> CWordArray m_SizeOfMsg;</FONT>
<BR><FONT COLOR="#990000"> CString GetErrorMessage();
// If there is any error this will return it method</FONT>
<BR><FONT COLOR="#990000"> CString GetPassword();
// Getting Password stored in class</FONT>
<BR><FONT COLOR="#990000"> void SetPassword(CString& Password);
// Setting Password in class</FONT>
<BR><FONT COLOR="#990000"> CString GetUser();
// Getting user name stored in class</FONT>
<BR><FONT COLOR="#990000"> void SetUser(CString& User);
// Setting user name in class</FONT>
<BR><FONT COLOR="#990000"> CString GetHost();
// Getting Host name (email server name) stored in class</FONT>
<BR><FONT COLOR="#990000"> void SetHost(CString& Host);
// Setting Host name (email server name) in class</FONT>
<BR><FONT COLOR="#990000"> BOOL Connect();
// Connecting to email server</FONT>
<BR><FONT COLOR="#990000"> int GetTotalMailSize();
// it returns the Total Mail Size</FONT>
<BR><FONT COLOR="#990000"> int GetNumberOfMails();
// It return the number of mails</FONT>
<BR><FONT COLOR="#990000"> CString GetMsgContents(); </FONT>
<BR><FONT COLOR="#990000"> BOOL Statistics();
// issue the STAT command on email server</FONT>
<BR><FONT COLOR="#990000"> BOOL Retrieve(int MsgNumber);
// Getting any particular mail message</FONT>
<BR><FONT COLOR="#990000"> BOOL Reset();
// issue the reset command on email server</FONT>
<BR><FONT COLOR="#990000"> int GetMessageSize(int MsgNumber);
// Return a size of any particular mail</FONT>
<BR><FONT COLOR="#990000"> BOOL Noop();
// issue the NOOP command on email server</FONT>
<BR><FONT COLOR="#990000"> BOOL Disconnect();
// issue the QUIT command on email server</FONT>
<BR><FONT COLOR="#990000"> BOOL Delete(int& MsgNumber); // Deleteing
a particular message from email server</FONT>
<BR><FONT COLOR="#990000"> BOOL Connect(CString& Host, CString&
User, CString& Password);</FONT>
<BR><FONT COLOR="#990000"> CPop();</FONT>
<BR><FONT COLOR="#990000"> virtual ~CPop();</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000">private:</FONT>
<BR><FONT COLOR="#990000"> CString m_ErrorMessage;</FONT>
<BR><FONT COLOR="#990000"> BOOL CheckResponse(int ResponseType);</FONT>
<BR><FONT COLOR="#990000"> CString m_Password;</FONT>
<BR><FONT COLOR="#990000"> CString m_User;</FONT>
<BR><FONT COLOR="#990000"> CString m_Host;</FONT>
<BR><FONT COLOR="#990000"> CString m_MsgContents;</FONT>
<BR><FONT COLOR="#990000"> int m_TotalSize;</FONT>
<BR><FONT COLOR="#990000"> int m_NumberMail;</FONT>
<BR><FONT COLOR="#990000"> 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> <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"> 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"> m_PopServer.Close();</FONT>
<BR><FONT COLOR="#990000">}</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000">BOOL CPop::Connect(CString & Host, CString
& User, CString & Password)</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000"> char buf [512];</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000"> if (!m_PopServer.Connect(Host,110)) // 110
Pop3 Port</FONT>
<BR><FONT COLOR="#990000"> {</FONT>
<BR><FONT COLOR="#990000"> m_ErrorMessage = _T("Server cannot be
connected");</FONT>
<BR><FONT COLOR="#990000"> return FALSE;</FONT>
<BR><FONT COLOR="#990000"> }</FONT>
<BR><FONT COLOR="#990000"> else</FONT>
<BR><FONT COLOR="#990000"> {</FONT>
<BR><FONT COLOR="#990000"> if(CheckResponse(CONNECTION_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000"> return FALSE;</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000"> wsprintf (buf, "USER %s\r\n", (LPCSTR)
User);</FONT>
<BR><FONT COLOR="#990000"> m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000"> if(CheckResponse(USER_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000"> return FALSE;</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000"> wsprintf (buf, "PASS %s\r\n", (LPCSTR)
Password);</FONT>
<BR><FONT COLOR="#990000"> m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000"> if (CheckResponse(PASSWORD_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000"> return FALSE;</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000"> return TRUE;</FONT>
<BR><FONT COLOR="#990000"> }</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000">}</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000">BOOL CPop::Delete(int & MsgNumber)</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000"> char buf [512];</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000"> wsprintf (buf, "DELE %d\r\n",MsgNumber );</FONT>
<BR><FONT COLOR="#990000"> m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000"> if (CheckResponse(DELETE_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000"> return FALSE;</FONT>
<BR><FONT COLOR="#990000"> else</FONT>
<BR><FONT COLOR="#990000"> 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"> char buf [512];</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000"> wsprintf (buf, "QUIT \r\n");</FONT>
<BR><FONT COLOR="#990000"> m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000"> if (CheckResponse(QUIT_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000"> return FALSE;</FONT>
<BR><FONT COLOR="#990000"> else</FONT>
<BR><FONT COLOR="#990000"> 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"> char buf [512];</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000"> wsprintf (buf, "NOOP \r\n");</FONT>
<BR><FONT COLOR="#990000"> m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000"> if (CheckResponse(NOOP_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000"> return FALSE;</FONT>
<BR><FONT COLOR="#990000"> else</FONT>
<BR><FONT COLOR="#990000"> 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"> if(m_SizeOfMsg.GetSize() < MsgNumber+1)</FONT>
<BR><FONT COLOR="#990000"> return 0;</FONT>
<BR><FONT COLOR="#990000"> else</FONT>
<BR><FONT COLOR="#990000"> 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"> char buf [512];</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000"> wsprintf (buf, "RSET \r\n");</FONT>
<BR><FONT COLOR="#990000"> m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000"> if (CheckResponse(RSET_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000"> return FALSE;</FONT>
<BR><FONT COLOR="#990000"> else</FONT>
<BR><FONT COLOR="#990000"> 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 MsgNumber)</FONT>
<BR><FONT COLOR="#990000">{</FONT>
<BR><FONT COLOR="#990000"> char buf [512];</FONT><FONT COLOR="#990000"></FONT>
<P><FONT COLOR="#990000"> wsprintf (buf, "RETR %d\r\n",MsgNumber );</FONT>
<BR><FONT COLOR="#990000"> m_PopServer.Send(buf, strlen (buf));</FONT>
<BR><FONT COLOR="#990000"> if (CheckResponse(RETR_CHECK)==FALSE)</FONT>
<BR><FONT COLOR="#990000"> return FALSE;</FONT>
<BR><FONT COLOR="#990000"> else</FONT>
<BR><FONT COLOR="#990000"> return TRUE;</FONT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -