www.pudn.com > popmail.zip > poptest.cpp


// poptest.cpp : Defines the entry point for the console application. 
// 
 
#include "stdafx.h" 
#include  
#include  
#include  
#include  
#include "poptest.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#import"..\pophandler\Debug\pophandler.dll" no_namespace named_guids 
 
///////////////////////////////////////////////////////////////////////////// 
// The one and only application object 
 
CWinApp theApp; 
 
using namespace std; 
 
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) 
{ 
	// initialize MFC and print and error on failure 
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) 
		return 1; 
	 
	if( FAILED(CoInitialize(0)) ) 
	{ 
		std::cout << "failed CoInitialize()" << endl; 
		return 1; 
	} 
 
	IPOPLevelPtr	pPOPLevel; 
	LONG			lErr		= 0; 
 
	HRESULT hr = pPOPLevel.CreateInstance(__uuidof(POPLevel)); 
	if(FAILED(hr)) 
	{ 
		std::cout << "failed CreateInstance(), please try to register pophandler.dll" << endl; 
		CoUninitialize(); 
		return 1; 
	} 
 
	bstr_t	bstHost; 
	bstHost = "127.0.0.1"; 
	CHAR szHost[256]; 
	std::cout << "host: " << endl; 
	std::cin >> szHost; 
	bstHost = szHost; 
 
	pPOPLevel->put_Host(bstHost); 
	pPOPLevel->put_Port(110); 
 
	bstr_t	bstUser; 
	bstUser = "test_user"; 
	CHAR szUser[256]; 
	std::cout << "user: " << endl; 
	std::cin >> szUser; 
	bstUser = szUser; 
	pPOPLevel->put_User(bstUser); 
 
	bstr_t	bstPassword; 
	bstPassword = "1111"; 
	CHAR szPassword[256]; 
	std::cout << "password: " << endl; 
	std::cin >> szPassword; 
	bstPassword = szPassword; 
	pPOPLevel->put_Password(bstPassword); 
	 
	if(pPOPLevel->Connect() != 0) 
	{ 
		std::cout << "connect failed" << endl; 
		pPOPLevel->Disconnect(); 
		pPOPLevel.Release(); 
		CoUninitialize(); 
		return 1; 
	} 
	 
	LONG lMailCount;	 
	pPOPLevel->GetMailCount(&lMailCount); 
 
	std::cout << "mail count: " << lMailCount << endl; 
	DWORD i; 
	for(i = 1; i <= lMailCount; i ++) 
	{ 
		std::cout << "message: " << i << endl; 
		if(pPOPLevel->GetMail(i, 1) != 0) 
		{ 
			std::cout << "receive failed" << endl; 
			continue; 
		} 
	 
		BSTR bsFrom; 
		BSTR bsTo; 
		BSTR bsCC; 
		BSTR bsSubject; 
		BSTR bsHeader; 
		BSTR bsDate; 
		 
		USES_CONVERSION; 
		pPOPLevel->get_From(&bsFrom); 
		CHAR* pszString = OLE2T(bsFrom);  
		SysFreeString(bsFrom); 
		std::cout << "from: " << pszString << endl; 
				 
		pPOPLevel->get_To(&bsTo); 
		pszString = OLE2T(bsTo); 
		SysFreeString(bsTo); 
		std::cout << "to: " << pszString << endl; 
		 
		pPOPLevel->get_CC(&bsCC); 
		pszString = OLE2T(bsCC); 
		std::cout << "cc: " << pszString << endl; 
		SysFreeString(bsCC); 
		 
		pPOPLevel->get_Subject(&bsSubject); 
		pszString = OLE2T(bsSubject); 
		SysFreeString(bsSubject); 
		std::cout << "subject: " << pszString << endl; 
				 
		pPOPLevel->get_Header(&bsHeader); 
		pszString = OLE2T(bsHeader); 
		SysFreeString(bsHeader); 
		 
		pPOPLevel->get_Date(&bsDate); 
		pszString = OLE2T(bsDate); 
		SysFreeString(bsDate); 
		 
		 
		BSTR	bsBody; 
		BSTR	bsAttachedFiles; 
		 
		pPOPLevel->get_Body(&bsBody); 
		CString sBody = bsBody; 
		SysFreeString(bsBody); 
		std::cout << "body: " << (LPCSTR)sBody << endl; 
		pPOPLevel->get_AttachedFiles(&bsAttachedFiles); 
		CString sAttachedFiles = bsAttachedFiles; 
		SysFreeString(bsAttachedFiles); 
		std::cout << "attached files: " << (LPCSTR)sAttachedFiles << endl; 
	} 
 
	pPOPLevel->Disconnect(); 
	pPOPLevel.Release(); 
	CoUninitialize(); 
 
	return 0; 
}