分享
 
 
 

POP3协议封装类

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

//原著:Asif Rasheed

//译者:重庆大学光电工程学院 贾旭滨

//欢迎批评指教,谢谢!

目前互连网上,EMAIL的使用是越来越广泛了。在所有的TCP连接线路中,大概有一半的线路是用来收发EMAIL的。因此,许多网络应用程序要想离开POP3协议是不可能的。

而且,很多EMAIL系统都是使用SMTP协议来作为发送协议,而POP3协议来作为接受协议。关于这2个协议的许多资料,你可以看一下RCF821(关于SMTP的)和RCF1225(关于POP3的)。虽然在WINDOWS操作系统中,有一个叫MAPI应用程序是来管理EMAIL的,但是它的功能还不够强大,不足以直接处理一些EMAIL服务。所以,我写了一个有关于POP3协议的C++类,在这个类中我还使用了CSocket作为一个类成员(这好象有点不可想象),这样的话呢,我们可以在连接的时候使用它。并且,该POP类使用的一些功能类似于POP3协议中的一些命令。下面就代码:

/*--------------------------------------------------------------------

Pop.h : main header file for the POP application

-----------------------------------------------------------------------*/

#if !defined(AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_)

#define AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_

#define CONNECTION_CHECK 0

#define USER_CHECK 1

#define PASSWORD_CHECK 2

#define QUIT_CHECK 3

#define DELETE_CHECK 4

#define RSET_CHECK 5

#define STAT_CHECK 6

#define NOOP_CHECK 7

#define LIST_CHECK 8

#define RETR_CHECK 9

/////////////////////////////////////////////////////////////////////////////

class CPop

{

public:

BOOL List();

CWordArray m_SizeOfMsg;

CString GetErrorMessage(); // If there is any error this will return it method

CString GetPassword(); // Getting Password stored in class

void SetPassword(CString& Password); // Setting Password in class

CString GetUser(); // Getting user name stored in class

void SetUser(CString& User); // Setting user name in class

CString GetHost(); // Getting Host name (email server name) stored in class

void SetHost(CString& Host); // Setting Host name (email server name) in class

BOOL Connect(); // Connecting to email server

int GetTotalMailSize(); // it returns the Total Mail Size

int GetNumberOfMails(); // It return the number of mails

CString GetMsgContents();

BOOL Statistics(); // issue the STAT command on email server

BOOL Retrieve(int MsgNumber); // Getting any particular mail message

BOOL Reset(); // issue the reset command on email server

int GetMessageSize(int MsgNumber); // Return a size of any particular mail

BOOL Noop(); // issue the NOOP command on email server

BOOL Disconnect(); // issue the QUIT command on email server

BOOL Delete(int& MsgNumber); // Deleteing a particular message from email server

BOOL Connect(CString& Host, CString& User, CString& Password);

CPop();

virtual ~CPop();

private:

CString m_ErrorMessage;

BOOL CheckResponse(int ResponseType);

CString m_Password;

CString m_User;

CString m_Host;

CString m_MsgContents;

int m_TotalSize;

int m_NumberMail;

CSocket m_PopServer;

};

/#endif // !defined(AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_)

/*-----------------------------------------------------------------------------------------------

// Pop.cpp : Defines the class behaviors for the application.

---------------------------------------------------------------------------------------------------*/

#include "stdafx.h"

#include "Pop.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

////////////////////////////////////////////////////////////////////

// CPop Class

//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////

CPop::CPop()

{

m_PopServer.Create();

}

//{4EEC1C91-6BE1-11d1-8824-00001C302581}

CPop::~CPop()

{

m_PopServer.Close();

}

BOOL CPop::Connect(CString & Host, CString & User, CString & Password)

{

char buf [512];

if (!m_PopServer.Connect(Host,110)) // 110 Pop3 Port

{

m_ErrorMessage = _T("Server cannot be connected");

return FALSE;

}

else

{

if(CheckResponse(CONNECTION_CHECK)==FALSE)

return FALSE;

wsprintf (buf, "USER %s\r\n", (LPCSTR) User);

m_PopServer.Send(buf, strlen (buf));

if(CheckResponse(USER_CHECK)==FALSE)

return FALSE;

wsprintf (buf, "PASS %s\r\n", (LPCSTR) Password);

m_PopServer.Send(buf, strlen (buf));

if (CheckResponse(PASSWORD_CHECK)==FALSE)

return FALSE;

return TRUE;

}

}

BOOL CPop::Delete(int & MsgNumber)

{

char buf [512];

wsprintf (buf, "DELE %d\r\n",MsgNumber );

m_PopServer.Send(buf, strlen (buf));

if (CheckResponse(DELETE_CHECK)==FALSE)

return FALSE;

else

return TRUE;

}

BOOL CPop::Disconnect()

{

char buf [512];

wsprintf (buf, "QUIT \r\n");

m_PopServer.Send(buf, strlen (buf));

if (CheckResponse(QUIT_CHECK)==FALSE)

return FALSE;

else

return TRUE;

}

BOOL CPop::Noop()

{

char buf [512];

wsprintf (buf, "NOOP \r\n");

m_PopServer.Send(buf, strlen (buf));

if (CheckResponse(NOOP_CHECK)==FALSE)

return FALSE;

else

return TRUE;

}

// Return the Msg Size for given msg number

int CPop::GetMessageSize(int MsgNumber)

{

if(m_SizeOfMsg.GetSize() < MsgNumber+1)

return 0;

else

return m_SizeOfMsg[MsgNumber+1];

}

BOOL CPop::Reset()

{

char buf [512];

wsprintf (buf, "RSET \r\n");

m_PopServer.Send(buf, strlen (buf));

if (CheckResponse(RSET_CHECK)==FALSE)

return FALSE;

else

return TRUE;

}

// MsgContents will hold the msg body

BOOL CPop::Retrieve(int MsgNumber)

{

char buf [512];

wsprintf (buf, "RETR %d\r\n",MsgNumber );

m_PopServer.Send(buf, strlen (buf));

if (CheckResponse(RETR_CHECK)==FALSE)

return FALSE;

else

return TRUE;

}

BOOL CPop::Statistics()

{

char buf [512];

wsprintf (buf, "STAT \r\n");

m_PopServer.Send(buf, strlen (buf));

if (CheckResponse(STAT_CHECK)==FALSE)

return FALSE;

else

return TRUE;

}

CString CPop::GetMsgContents()

{

return m_MsgContents;

}

int CPop::GetNumberOfMails()

{

return m_NumberMail;

}

int CPop::GetTotalMailSize()

{

return m_TotalSize;

}

BOOL CPop::Connect()

{

Connect(m_Host, m_User, m_Password);

return TRUE;

}

void CPop::SetHost(CString & Host)

{

m_Host = Host;

}

CString CPop::GetHost()

{

return m_Host;

}

void CPop::SetUser(CString & User)

{

m_User = User;

}

CString CPop::GetUser()

{

return m_User;

}

void CPop::SetPassword(CString & Password)

{

m_Password = Password;

}

CString CPop::GetPassword()

{

return m_Password;

}

BOOL CPop::CheckResponse(int ResponseType)

{

char buf [1000];

for (int i=0;i<512;i++)

buf[i]='\0';

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

switch (ResponseType)

{

case CONNECTION_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Bad Connection");

return FALSE;

}

break;

case USER_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Bad User Name");

return FALSE;

}

break;

case PASSWORD_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Bad Password Name");

return FALSE;

}

break;

case QUIT_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Error occured during QUIT");

return FALSE;

}

break;

case DELETE_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Error occured during DELE");

return FALSE;

}

break;

case RSET_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Error occured during RSET");

return FALSE;

}

break;

case STAT_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Error occured during STAT");

return FALSE;

}

else

{

BOOL EmailNumber = 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);

return TRUE;

}

}

}

}

break;

case NOOP_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Error occured during NOOP");

return FALSE;

}

break;

case LIST_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Error occured during LIST");

return FALSE;

}

else

{

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

for (char *p = buf; *p != '.'; p++)

if (*p == '\t' || *p == ' ')

m_SizeOfMsg.Add(atoi(p));

}

break;

case RETR_CHECK:

if (strnicmp(buf,"-ERR", 4) == 0)

{

m_ErrorMessage = _T("Error occured during RETR");

return FALSE;

}

else

{

char temp[9000];

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

m_MsgContents = temp;

}

break;

}

return TRUE;

}

CString CPop::GetErrorMessage()

{

return m_ErrorMessage;

}

BOOL CPop::List()

{

char buf [512];

wsprintf (buf, "LIST \r\n");

m_PopServer.Send(buf, strlen (buf));

if (CheckResponse(LIST_CHECK)==FALSE)

return FALSE;

else

return TRUE;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有