目前互连网上,EMAIL的使用是越来越广泛了。在所有的TCP连接线路中,大概有一半的线路是用来收发EMAIL的。因此,许多网络应用程序要想离开POP3协议是不可能的。
而且,很多EMAIL系统都是使用SMTP协议来作为发送协议,而POP3协议来作为接受协议。关于这2个协议的许多资料,你可以看一下RCF821(关于SMTP的)和RCF1225(关于POP3的)。虽然在WINDOWS操作系统中,有一个叫MAPI应用程序是来管理EMAIL的,但是它的功能还不够强大,不足以直接处理一些EMAIL服务。所以,我写了一个有关于POP3协议的C++类,在这个类中我还使用了CSocket作为一个类成员(这好象有点不可想象),这样的话呢,我们可以在连接的时候使用它。并且,该POP类使用的一些功能类似于POP3协议中的一些命令。下面就代码:
/*--------------------------------------------------------------------
Pop.h:mainheaderfileforthePOPapplication
-----------------------------------------------------------------------*/
#if!defined(AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_)
#defineAFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_
#defineCONNECTION_CHECK0
#defineUSER_CHECK1
#definePASSWORD_CHECK2
#defineQUIT_CHECK3
#defineDELETE_CHECK4
#defineRSET_CHECK5
#defineSTAT_CHECK6
#defineNOOP_CHECK7
#defineLIST_CHECK8
#defineRETR_CHECK9
/////////////////////////////////////////////////////////////////////////////
classCPop
{
public:
BOOLList();
CWordArraym_SizeOfMsg;
CStringGetErrorMessage();//Ifthereisanyerrorthiswillreturnitmethod
CStringGetPassword();//GettingPasswordstoredinclass
voidSetPassword(CString&Password);//SettingPasswordinclass
CStringGetUser();//Gettingusernamestoredinclass
voidSetUser(CString&User);//Settingusernameinclass
CStringGetHost();//GettingHostname(emailservername)storedinclass
voidSetHost(CString&Host);//SettingHostname(emailservername)inclass
BOOLConnect();//Connectingtoemailserver
intGetTotalMailSize();//itreturnstheTotalMailSize
intGetNumberOfMails();//Itreturnthenumberofmails
CStringGetMsgContents();
BOOLStatistics();//issuetheSTATcommandonemailserver
BOOLRetrieve(intMsgNumber);//Gettinganyparticularmailmessage
BOOLReset();//issuetheresetcommandonemailserver
intGetMessageSize(intMsgNumber);//Returnasizeofanyparticularmail
BOOLNoop();//issuetheNOOPcommandonemailserver
BOOLDisconnect();//issuetheQUITcommandonemailserver
BOOLDelete(int&MsgNumber);//Deleteingaparticularmessagefromemailserver
BOOLConnect(CString&Host,CString&User,CString&Password);
CPop();
virtual~CPop();
private:
CStringm_ErrorMessage;
BOOLCheckResponse(intResponseType);
CStringm_Password;
CStringm_User;
CStringm_Host;
CStringm_MsgContents;
intm_TotalSize;
intm_NumberMail;
CSocketm_PopServer;
};
/#endif//!defined(AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_)
/*-----------------------------------------------------------------------------------------------
//Pop.cpp:Definestheclassbehaviorsfortheapplication.
---------------------------------------------------------------------------------------------------*/
#include"stdafx.h"
#include"Pop.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
////////////////////////////////////////////////////////////////////
//CPopClass
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPop::CPop()
{
m_PopServer.Create();
}
//{4EEC1C91-6BE1-11d1-8824-00001C302581}
CPop::~CPop()
{
m_PopServer.Close();
}
BOOLCPop::Connect(CString&Host,CString&User,CString&Password)
{
charbuf[512];
if(!m_PopServer.Connect(Host,110))//110Pop3Port
{
m_ErrorMessage=_T("Servercannotbeconnected");
returnFALSE;
}
else
{
if(CheckResponse(CONNECTION_CHECK)==FALSE)
returnFALSE;
wsprintf(buf,"USER%s",(LPCSTR)User);
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(USER_CHECK)==FALSE)
returnFALSE;
wsprintf(buf,"PASS%s",(LPCSTR)Password);
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(PASSWORD_CHECK)==FALSE)
returnFALSE;
returnTRUE;
}
}
BOOLCPop::Delete(int&MsgNumber)
{
charbuf[512];
wsprintf(buf,"DELE%d",MsgNumber);
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(DELETE_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}
BOOLCPop::Disconnect()
{
charbuf[512];
wsprintf(buf,"QUIT");
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(QUIT_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}
BOOLCPop::Noop()
{
charbuf[512];
wsprintf(buf,"NOOP");
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(NOOP_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}
//ReturntheMsgSizeforgivenmsgnumber
intCPop::GetMessageSize(intMsgNumber)
{
if(m_SizeOfMsg.GetSize()<MsgNumber+1)
return0;
else
returnm_SizeOfMsg[MsgNumber+1];
}
BOOLCPop::Reset()
{
charbuf[512];
wsprintf(buf,"RSET");
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(RSET_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}
//MsgContentswillholdthemsgbody
BOOLCPop::Retrieve(intMsgNumber)
{
charbuf[512];
wsprintf(buf,"RETR%d",MsgNumber);
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(RETR_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}
BOOLCPop::Statistics()
{
charbuf[512];
wsprintf(buf,"STAT");
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(STAT_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}
CStringCPop::GetMsgContents()
{
returnm_MsgContents;
}
intCPop::GetNumberOfMails()
{
returnm_NumberMail;
}
intCPop::GetTotalMailSize()
{
returnm_TotalSize;
}
BOOLCPop::Connect()
{
Connect(m_Host,m_User,m_Password);
returnTRUE;
}
voidCPop::SetHost(CString&Host)
{
m_Host=Host;
}
CStringCPop::GetHost()
{
returnm_Host;
}
voidCPop::SetUser(CString&User)
{
m_User=User;
}
CStringCPop::GetUser()
{
returnm_User;
}
voidCPop::SetPassword(CString&Password)
{
m_Password=Password;
}
CStringCPop::GetPassword()
{
returnm_Password;
}
BOOLCPop::CheckResponse(intResponseType)
{
charbuf[1000];
for(inti=0;i<512;i++)
buf[i]='