ANPOP Developers Center > Using ANPOP in Visual C++
Installation and Deployment
You should download the anpop installer and install it on your machine at first. If you want to distribute or deploy anpop without anpop installer, please click here to learn more.
C++ header files for ANPOP
All header files for C++ are located in "Include" sub-directory of ANPOP installation directory.
ANPOP.IDL //MIDL file for ANPOP, it was generated by OleView tool in Visual Studio. ANPOP.H // header file of ANPOP ANPOP.i_c ANPOP.tlh // header file for ANPOP SmartPointer ANPOP.tli
Using ANPOP with COM interface
This sample demonstrates how to retrieve pop3 email in Visual C++ with COM interface.
#include <comdef.h>
#include "ANPOP.h"
#include "ANPOP_i.c"
//create pop3 connection object and messge object
IPOPMAIN* pConnection = NULL;
IPOPMSG* pMessage = NULL;
HRESULT hr = ::CoInitialize( NULL );
hr = ::CoCreateInstance( CLSID_POPMAIN,
NULL,
CLSCTX_ALL,
IID_IPOPMAIN,
(LPVOID*)&pConnection );
_ASSERT( SUCCEEDED( hr ) && pConnection != NULL );
hr = ::CoCreateInstance( CLSID_POPMSG,
NULL,
CLSCTX_ALL,
IID_IPOPMSG,
(LPVOID*)&pMessage );
_ASSERT( SUCCEEDED( hr ) && pMessage != NULL );
//Connect to pop3 email server
long lRet = 1;
pConnection->Connect( _bstr_t("pop3.control.com"), _bstr_t("Jack"),
_bstr_t("ja1234"), &lRet );
if( lRet == 0 )
{
//Get the total count of emails existing on POP3 server.
pConnection->GetTotalOfMails( &lRet );
long lLen = lRet;
for( int i = 0 ; i < lLen; i++ )
{
//Retrieve each email and save it to disk
BSTR bstrOut = NULL;
pConnection->Retrieve( i+1, &bstrOut );
pMessage->put_RawContent( bstrOut );
TCHAR szBuf[256];
::wsprintf( szBuf, "%s\\%d.eml", "c:\\",
i+1 );
pMessage->ExportFile( _bstr_t( szBuf ), &lRet );
::SysFreeString( bstrOut );
bstrOut = NULL;
}
}
pConnection->Close();
pConnection->Release();
pConnection = NULL;
pMessage->Release();
pMessage = NULL;
::CoUninitialize();
Using ANPOP with Smart Pointer
Be honest, using the raw COM interface in Visual C++ is boring. Smart Pointer makes programming life easier. The following code demonstrates how smart pointer facilitates your Visual C++ works.
#include <comdef.h>
#include <string>
#include <iostream>
#include <ANPOP.tlh>
using namespace std;
using namespace ANPOPLib;
VOID Retrieve( const char* lpszServer,
const char* lpszUser,
const char* lpszPassword )
{
::CoInitialize( NULL );
IPOPMAINPtr oPop3("ANPOP.POPMAIN");
IPOPMSGPtr oMsg("ANPOP.POPMSG");
int i = 0, nCount = 0, nRet = 0, nSize = 0;
_bstr_t emailContent = "", messageId = "";
try
{
nRet = oPop3->Connect( _bstr_t(lpszServer),
_bstr_t(lpszUser),
_bstr_t(lpszPassword));
if( nRet != 0 )
throw string( "error with Connect" );
nCount = oPop3->GetTotalOfMails(); //Get total count of emails
if( nCount == -1 )
throw string( "error with GetTotalOfMails" );
else if( nCount == 0 )
throw string( "no email" );
for( i = 1; i <= nCount; i++ )
{
nSize = oPop3->GetMsgSize(i); //Get email size
if( nSize == -1 )
throw string( "error with GetMsgSize" );
messageId = oPop3->GetMsgID(i); //Get message-id
if( messageId == _bstr_t((BSTR)NULL))
throw string( "error with GetMsgID" );
emailContent = oPop3->Retrieve(i); //Retrieve email
if( emailContent == _bstr_t((BSTR)NULL))
throw string( "error with Retrieve" );
oMsg->RawContent = emailContent;
char szFile[MAX_PATH];
memset( szFile, 0, sizeof(szFile));
::sprintf( szFile, "c:\\pop3Test_%d.eml", i );
nRet = oMsg->ExportFile( _bstr_t(szFile)); //Save email
if( nRet != 0 )
throw string( "error with ExportFile" );
if( oPop3->Delete(i)!= 0 ) //Delete email from server
throw string( "error with Delete" );
}
}
catch( string &e )
{
cout << e << endl;
}
oPop3->Close(); //Close connection
oPop3.Release();
oMsg.Release();
::CoUninitialize();
}
Asynchronous mode and event driving
If you want to use POPMAIN in asynchronous mode, please refer to Pop3Sink.h of VCSAMPLE1 in ANPOP installation package. You just need to inherit from class CPop3Sink and override the event hanlder.
#include "Pop3Sink.h"
class CMyDlg: public CDialog, /*POPMAIN Events Handler*/CPop3Sink
{
....
protected:
HRESULT __stdcall OnConnectedHandler();
HRESULT __stdcall OnClosedHandler();
HRESULT __stdcall OnErrorHandler( long lError, BSTR errDescription );
HRESULT __stdcall OnGetTotalOfMailsHandler( long lCount );
HRESULT __stdcall OnGetMsgIDHandler( BSTR messageId, long lMsg );
HRESULT __stdcall OnReceivingHandler( long lBytes, long lMsg );
HRESULT __stdcall OnReceivedHandler( BSTR emailContent, long lMsg );
HRESULT __stdcall OnReceiveHandler( long lMsg );
HRESULT __stdcall OnDeletedHandler( long lMsg );
...
}
Full Sample
Please refer to VCSAMPLE1 in ANPOP installation package.
Free Email Support
Not enough? Please contact our technical support team.
Support@EmailArchitect.NET
VIP@EmailArchitect.NET(Registered
User)
Remarks
We usually reply emails in 24hours. The reason for getting no response is
likely that your smtp server bounced our reply. In this case, please try to use
another email address to contact us. Your Hotmail or Yahoo email account is
recommended.
|