需要支持pop3,mime的收取邮件的源代码,请大家帮助,最好是vc工程版的,如果有支持smime的就更好了。
解决方案 »
- 刚学习VC的数据库,在读取excel的时候遇到些问题
- 阿强QQ农场助手 是用C++编的
- 保存对话框如何动态设置初始的文件名
- 大家好,帮看一下这个计数器,为什么上传后只是计数为一然后就不再计数了呀?
- 高手请进:如何将字体及颜色保存到文件中然后从文件中加载
- 请问如何让文字竖排?就跟中国古代的书上写的那样?
- MFC中一个指针转换的问题请教高手
- 為何我的函數在編譯時沒定義??怎麼解決??大蝦幫幫忙!!
- 关于共享内存检测和进程间通信的问题.
- wisock
- 控件到底是不是窗口?
- ★☆VC中有没有打开对话框、保存、颜色、字体等对话框控件或函数,像delphi那样有opendialog,savedialog,colordialog,fontdialog☆★
工程:Lite Mail Server。
作者:Steven Xu
描述:POP3客户端操作类。
主要函数:CPop3Client类
版本:1.0,2002-07-26。
修改:说明对文件的修改内容、修改原因以及修改日期。
参考文献:
*/#ifndef __POP3CLIENT_H__
#define __POP3CLIENT_H__#include <string>
#include "BlockSocket.h"using namespace std;enum DataTypes{
MAIL_AS_STRING,
TOP_AS_STRING,
LIST_AS_STRING,
UIDL_AS_STRING
};class CPop3Client : public CBlockSocket
{
public:
CPop3Client();
virtual ~CPop3Client();protected:
enum CheckTypes {
CONNECTION_CHECK,
USER_CHECK,
PASSWORD_CHECK,
QUIT_CHECK,
DELETE_CHECK,
RSET_CHECK,
STAT_CHECK,
NOOP_CHECK,
LIST_CHECK,
LISTONE_CHECK,
RETR_CHECK,
TOP_CHECK,
UIDL_CHECK,
UIDLONE_CHECK
}; // Temporary Files for storing Data
string m_strMailList;
string m_strMailMessage;
string m_strError; int m_iSocket;
int m_iNewMailsCount;
long m_lTotalMailSize;
bool m_bConnect;public:
bool Connect(const char* pHost, int iPort = 110);
bool Disconnect(); bool Login(const char* pUser, const char* pPassword); bool Statistics();
bool Delete(int iMsgNumber);
bool Noop();
bool Reset();
bool Retrieve(int iMsgNumber);
bool Retrieve(int iMsgNumber, const char* pTargetFileName);
bool List(int iMsgNumber = 0);
bool GetUID(int iMsgNumber = 0);
bool GetTop(int iMsgNumber, int iLength); int GetNewMailsCount();
int GetTotalMailSize(); string Get(int iDataType, int iMessageNumber = 1,int iLength = 0);
string GetError();
protected:
bool CheckResponse(int iResponseType);
bool SendResponse(const char * pcFormat,...);
};#endif //__POP3CLIENT_H__
#include <stdarg.h>#include "Pop3Client.h"CPop3Client::CPop3Client()
{
// Initialising
m_bConnect = false;
m_strMailList = "";
m_strMailMessage = "";
m_strError = "";
m_iNewMailsCount = 0;
m_lTotalMailSize = 0;
}CPop3Client::~CPop3Client()
{
}bool CPop3Client::Connect(const char* pHost, const int iPort)
{
CSockAddr saServer; if (!saServer.Create(pHost, iPort))
return false; try
{
CBlockSocket::Create();
CBlockSocket::Connect(saServer);
}
catch (const char* e)
{
m_strError = e;
return false;
} if(CheckResponse(CONNECTION_CHECK) == false)
return false;
m_bConnect = true;
return true;
} bool CPop3Client::Disconnect()
{
SendResponse("QUIT\r\n");
if (CheckResponse(QUIT_CHECK) == false)
return false; m_bConnect = false; return true;
} bool CPop3Client::Login(const char* pUser, const char* pPassword)
{ SendResponse("USER %s\r\n", pUser);
if(CheckResponse (USER_CHECK) == false)
return false; SendResponse("PASS %s\r\n", pPassword);
if (CheckResponse (PASSWORD_CHECK) == false)
return false; return true;
}bool CPop3Client::SendResponse(const char * pcFormat,...)
{
char buffer[2048];
memset(buffer, 0, sizeof(buffer)); va_list ap;
va_start(ap, pcFormat); vsprintf(buffer, pcFormat, ap);
va_end(ap); try
{
Send(buffer, strlen(buffer));
}
catch (...)
{
m_strError = "send command exception";
return false;
} return true;
}
bool CPop3Client::Statistics()
{
SendResponse("STAT\r\n");
if (CheckResponse(STAT_CHECK) == false)
return false;
else
return true;
}
bool CPop3Client::Delete(int iMsgNumber)
{
SendResponse("DELE %d\r\n",iMsgNumber);
if (CheckResponse(DELETE_CHECK)==false)
return false;
else
return true;
} bool CPop3Client::Noop()
{
SendResponse("NOOP\r\n");
if (CheckResponse(NOOP_CHECK)==false)
return false;
else
return true;
}bool CPop3Client::Reset()
{
SendResponse("RSET\r\n");
if (CheckResponse(RSET_CHECK)==false)
return false;
else
return true;
}bool CPop3Client::Retrieve(int iMsgNumber)
{
SendResponse("RETR %d\r\n", iMsgNumber);
if (CheckResponse(RETR_CHECK)==false)
return false;
else
return true;
}bool CPop3Client::Retrieve (int iMsgNumber, const char* pTargetFileName)
{
if (Retrieve(iMsgNumber))
{
FILE* fp; fp = fopen(pTargetFileName,"wb"); if (fp == NULL)
{
m_strError = "Create target file error!";
return false;
} fputs(m_strMailMessage.c_str(),fp);
fclose(fp);
return true;
}
else {
return false;
}
}bool CPop3Client::List(int iMsgNumber)
{
bool iRet;
if (iMsgNumber != 0)
{
SendResponse("LIST %d\r\n",iMsgNumber);
iRet = CheckResponse(LISTONE_CHECK);
}else
{
SendResponse("LIST\r\n");
iRet = CheckResponse(LIST_CHECK);
} return iRet;
}bool CPop3Client::GetTop(int iMsgNumber, int iLength)
{
SendResponse("TOP %d %d\r\n",iMsgNumber, iLength);
if (CheckResponse(TOP_CHECK)==false)
return false;
else
return true;
}bool CPop3Client::GetUID(int iMsgNumber)
{
bool iRet;
if (iMsgNumber != 0)
{
SendResponse("UIDL %d\r\n",iMsgNumber);
iRet = CheckResponse(UIDLONE_CHECK);
}else
{
SendResponse("UIDL\r\n");
iRet = CheckResponse(UIDL_CHECK);
} return iRet;
}string CPop3Client::Get(int iDataType, int iMessageNumber,int iLength)
{
// Switch back to standard Output file
string strData = ""; switch (iDataType)
{
case MAIL_AS_STRING:
if (Retrieve(iMessageNumber))
strData = m_strMailMessage;
break;
case TOP_AS_STRING:
if (GetTop(iMessageNumber, iLength))
strData = m_strMailMessage;
break;
case LIST_AS_STRING:
if (List())
strData = m_strMailList;
break;
case UIDL_AS_STRING:
if (GetUID(iMessageNumber))
strData = m_strMailList;
break;
} return strData;
}int CPop3Client::GetNewMailsCount()
{
return m_iNewMailsCount;
}int CPop3Client::GetTotalMailSize()
{
return m_lTotalMailSize;
}string CPop3Client::GetError()
{
return m_strError;
}
{
char buffer[8192];
int iRecBytes, skip;
bool bReady;
unsigned long lMsgLen;// usleep(2000);
memset(buffer, 0, sizeof(buffer)); try
{
iRecBytes = Receive(buffer, sizeof(buffer)/sizeof(char)-1);
}
catch (...)
{
m_strError = "check response receive exception";
return false;
} switch (iResponseType)
{
case CONNECTION_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Bad Connection";
return false;
}
break;
case USER_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Bad User Name";
return false;
}
break;
case PASSWORD_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Bad Password";
return false;
} break;
case QUIT_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Error occured during QUIT";
return false;
} break;
case DELETE_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Error occured during DELE";
return false;
} break;
case RSET_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Error occured during RSET";
return false;
}
break;
case STAT_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Error occured during STAT";
return false;
}
else
{
bool bMailNum = true; for (char* p = buffer; *p != '\0'; p++)
{
if (*p == '\t' || *p == ' ')
{
if(bMailNum == true)
{
m_iNewMailsCount = atoi(p+1);
bMailNum = false;
}
else
{
m_lTotalMailSize = atol(p+1);
}
}
}
return true;
}
break;
case NOOP_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Error occured during NOOP";
return false;
}
break;
case LIST_CHECK:
case LISTONE_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Error occured during LIST";
return false;
}
else
{
bool bRet;
m_strMailList = "";
lMsgLen = 0;
bReady = false;
if (strnicmp(buffer, "+OK \r\n", 6) == 0)
skip = 6;
else
skip = 4; //skip +OK(SPACE) if (iResponseType == LIST_CHECK)
{
if (strnicmp(buffer,"+OK", 3) == 0)
{
int i = 0;
while (buffer[i] != '\n') //skip tip info
i++;
skip = i+1;
}
} while (!bReady)
{
if (iRecBytes == SOCKET_ERROR)
{
m_strError = "Error reading from socket!";
bReady = true;
bRet = false;
}
else
{
m_strMailList += (buffer+skip);
lMsgLen += (iRecBytes-skip);
skip=0; if (((m_strMailList[lMsgLen-3] == '.')
&& (m_strMailList[lMsgLen-2] == '\r')
&& (m_strMailList[lMsgLen-1] == '\n'))
|| (iRecBytes==0)
|| ((buffer[0] == '+')
&& (buffer[1] == 'O')
&& (buffer[2] == 'K')
&& (m_strMailList[lMsgLen-3] != ' ')
&& (m_strMailList[lMsgLen-2] == '\r')
&& (m_strMailList[lMsgLen-1] == '\n')))
{
bReady = true;
}
else
{
memset(buffer, 0, sizeof(buffer)); try
{
iRecBytes = Receive(buffer, sizeof(buffer)/sizeof(char)-1);
}
catch (...)
{
m_strError = "receive data exception";
return false;
}
}
bRet = true;
}
}
return bRet;
}
break;
case RETR_CHECK:
if (strnicmp(buffer,"-ERR", 4) == 0)
{
m_strError = "Error occured during RETR";
return false;
}
else
{
if (strnicmp(buffer, "+OK \r\n", 6) == 0)
skip = 6;
else
skip = 4; //skip +OK(SPACE) if (strnicmp(buffer,"+OK", 3) == 0)
{
int i = 0;
while (buffer[i] != '\n') //skip tip info until "Received"
i++; skip= i+1;
}
bool bRet;
bReady = false;
m_strMailMessage = "";
lMsgLen = 0;
while (!bReady)
{
if (iRecBytes == SOCKET_ERROR)
{
bReady = true;
m_strError = "Error reading from socket!";
bRet = false;
}
else
{
m_strMailMessage += (buffer+skip);
lMsgLen += (iRecBytes-skip);
skip = 0;
if (((m_strMailMessage[lMsgLen-3] == '.')
&& (m_strMailMessage[lMsgLen-2] == '\r')
&& (m_strMailMessage[lMsgLen-1] == '\n'))
|| (iRecBytes == 0))
{
bReady = true;
}
else
{
memset(buffer, 0, sizeof(buffer));
try
{
iRecBytes = Receive(buffer, sizeof(buffer)/sizeof(char)-1);
}
catch (...)
{
m_strError = "receive data exception";
return false;
} }
bRet = true;
}
}
return bRet;
}
break;....还有一段太长了,需要的话请发邮件给我 [email protected]
[email protected]
谢谢!!!
[email protected]
谢谢!!!
[email protected]
我也想要!谢谢![email protected].