返回首页

POP3协议封装类

时间:2004-08-03 来源: 作者: 点击:
目前互连网上,EMAIL的使用是越来越广泛了。在所有的TCP连接线路中,大概有一半的线路是用来收发EMAIL的。因此,许多网络应用程序要想离开POP3协议是不可能的。 而且,很多EMAIL系统都是使用SMTP协议来作为发送协议,而POP3协议来作为接受协议。关于这2个协议的许多资
  

 目前互连网上,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\r\n",(LPCSTR)User);
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(USER_CHECK)==FALSE)
returnFALSE;

wsprintf(buf,"PASS%s\r\n",(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\r\n",MsgNumber);
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(DELETE_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}

BOOLCPop::Disconnect()
{
charbuf[512];

wsprintf(buf,"QUIT\r\n");
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(QUIT_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}

BOOLCPop::Noop()
{
charbuf[512];

wsprintf(buf,"NOOP\r\n");
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\r\n");
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(RSET_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}

//MsgContentswillholdthemsgbody
BOOLCPop::Retrieve(intMsgNumber)
{
charbuf[512];

wsprintf(buf,"RETR%d\r\n",MsgNumber);
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(RETR_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}

BOOLCPop::Statistics()
{
charbuf[512];

wsprintf(buf,"STAT\r\n");
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]='\0';

m_PopServer.Receive(buf,sizeof(buf));

switch(ResponseType)
{
caseCONNECTION_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("BadConnection");
returnFALSE;
}
break;

caseUSER_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("BadUserName");
returnFALSE;
}
break;
casePASSWORD_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("BadPasswordName");
returnFALSE;
}
break;
caseQUIT_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("ErroroccuredduringQUIT");
returnFALSE;
}
break;
caseDELETE_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("ErroroccuredduringDELE");
returnFALSE;
}
break;
caseRSET_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("ErroroccuredduringRSET");
returnFALSE;
}
break;
caseSTAT_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("ErroroccuredduringSTAT");
returnFALSE;
}
else
{
BOOLEmailNumber=TRUE;
for(char*p=buf;*p!='\0';p++)
{
if(*p=='\t'||*p=='')
{
if(EmailNumber==TRUE)
{
m_NumberMail=atoi(p);
EmailNumber=FALSE;
}
else
{
m_TotalSize=atoi(p);
returnTRUE;
}
}
}

}
break;
caseNOOP_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("ErroroccuredduringNOOP");
returnFALSE;
}
break;

caseLIST_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("ErroroccuredduringLIST");
returnFALSE;
}
else
{
m_PopServer.Receive(buf,sizeof(buf));

for(char*p=buf;*p!='.';p++)
if(*p=='\t'||*p=='')
m_SizeOfMsg.Add(atoi(p));
}
break;
caseRETR_CHECK:
if(strnicmp(buf,"-ERR",4)==0)
{
m_ErrorMessage=_T("ErroroccuredduringRETR");
returnFALSE;
}
else
{
chartemp[9000];
m_PopServer.Receive(temp,sizeof(temp));
m_MsgContents=temp;
}
break;
}
returnTRUE;
}

CStringCPop::GetErrorMessage()
{
returnm_ErrorMessage;
}

BOOLCPop::List()
{
charbuf[512];

wsprintf(buf,"LIST\r\n");
m_PopServer.Send(buf,strlen(buf));
if(CheckResponse(LIST_CHECK)==FALSE)
returnFALSE;
else
returnTRUE;
}

------分隔线----------------------------
顶一下
(3)
75%
踩一下
(1)
25%
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
推荐内容