You are here:
ActiveXperts.com > ActiveEmail > How to Use the ActiveEmail > E-mail POP3 > Visual C++
Quicklinks
ActiveEmail SMTP/POP3 Toolkit is a software development kit (SDK) that enables the user to send (SMTP) and receive (POP3) e-mail messages. ActiveEmail supports SMTP, POP3, multiple recipients (To, CC, BCC), multiple attachments (ASCII and binary), rich text body formats (RTF/HTML), Unicode, multiple character sets, SMTP authorization (AUTH PLAIN, AUTH LOGIN, AUTH CRAM MD5), POP3 authorization (Plain, APOP), POP3 header download, different character sets (including arabic, chinese, japanese, russian, greek, hebrew and many more), different encodings (including 7/8 bit, quoted-printable, base64).
In this example we are going to use Visual Studio 2008 to create a C++ application project named 'DemoApp'. We are going tot store this project in the directory 'C:\MyProjects'. All of these names can be changed according to your preferences. This demo project will ask the user to enter the username and password of the POP3 server where we will download the messages.
Download the ActiveEmail Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Launch 'Microsoft Visual C++' from the Start menu, and choose 'New' from the 'File Menu'. The 'New' dialog appears.
Select the type of project: 'Win32 Console Application', enter a 'Project name' and select the 'Location':
Select the kind of project, for instance a 'Hello, world!' application and click 'Finish':
A new Project is created now.
Before you can use the ActiveEmail Toolkit, you need to refer to the ActiveEmail Toolkit library. The actually reference files ship with the product and are located in the following directory:
C:\Program Files\ActiveXperts\ActiveEmail Toolkit\Samples\Visual C++\Include
ActiveXperts recommends to make use of the MmWrapper.h and MmWrapper.cpp COM wrappers. To do so, include 'MmWrapper.h' in your source file, and add 'MmWrapper.cpp' to your project so it will be compiled and linked together with your program code.
On top of your code, declare the following objects (defined in 'MmWrapper.h'):
CPop3 objPop3; CEMailMessage *pObjMessage = NULL;
The following code will download and display all messages from the POP3 server:
for( i = 1; i < lNumMessages + 1 ; i++ )
{
objPop3.GetEmailHeader ( i, &pObjMessage );
_tprintf( _T( "Mail From : %s\n" ), pObjMessage->getFromAddress( strValue ).t_str() );
_tprintf( _T( "Mail To : %s\n" ), pObjMessage->getToAddress( strValue ).t_str() );
_tprintf( _T( "Mail Subject : %s\n" ), pObjMessage->getSubject( strValue ).t_str() );
_tprintf( _T( "Mail Size : %ld\n"), pObjMessage->getSize() );
_tprintf( _T( "Mail Sent : %s\n" ), pObjMessage->getDate( strValue ).t_str() );
_tprintf( _T("\n") );
}
Following you can find the full source code which is also included in the ActiveEmail Toolkit package.
#include <windows.h>
#include <stdio.h>
////////////////////////////////////////////////////////////////////////////////
#include "..\..\..\include\MmWrapper.h"
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
LPTSTR ReadInput( LPCTSTR lptszTitle, BOOL bAllowEmpty = FALSE );
////////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, CHAR* argv[])
{
CPop3 objPop3;
CEMailMessage *pObjMessage = NULL;
CMmString strPop3Server = _T(""), strPop3Account = _T(""), strPop3Password = _T("");
BOOL bSecure = FALSE;
CMmString strFromAddress, strToAddress;
LONG lNumMessages = 0L;
CMmString strValue, strResult;
LONG lLastError = 0L;
CMmString strVersion, strBuild, strModule, strExpDate;
INT i = 0;
_tprintf( _T( "ActiveEmail Toolkit Version: %s\nActiveEmail Toolkit Build : "
"%s\nActiveEmail Toolkit Module : %s\nExpiration date : %s\n\n" ),
objPop3.getVersion( strVersion ).t_str(),
objPop3.getBuild( strBuild ).t_str(),
objPop3.getModule( strModule ).t_str(),
objPop3.getExpirationDate( strExpDate ).t_str() );
if( ! objPop3.IsInitialized() )
{
_tprintf( _T("Unable to initialized one or more objects.\n") );
goto _EndMain;
}
strPop3Server = ReadInput( _T("POP3 Server") );
bSecure = _tcsicmp( strPop3Server.t_str(), _T("pop.gmail.com") ) == 0;
strPop3Account = ReadInput( _T("POP3 mailbox account") );
strPop3Password = ReadInput( _T("POP3 mailbox password"), TRUE );
if( bSecure )
{
lLastError = objPop3.SetSecure( 465 );
_tprintf( _T("SetSecure, result: %ld (%s)\n"), lLastError,
objPop3.GetErrorDescription( lLastError, strResult ).t_str() );
if( lLastError != 0L )
goto _EndMain;
}
_tprintf( _T("Connecting...\n") );
lLastError = objPop3.Connect( strPop3Server.t_str(), strPop3Account.t_str(),
strPop3Password.t_str() );
_tprintf( _T("Connect, result: %ld (%s)\n"), lLastError,
objPop3.GetErrorDescription( lLastError, strResult ).t_str() );
lLastError = objPop3.CountMessages( &lNumMessages );
_tprintf( _T("CountMessages, result: %ld (%s)\n"), lLastError,
objPop3.GetErrorDescription( lLastError, strResult ).t_str() );
if( lLastError != 0 )
goto _EndMain;
_tprintf( _T("Number of messages in mailbox: %ld\n"), lNumMessages );
for( i = 1; i < lNumMessages + 1 ; i++ )
{
objPop3.GetEmailHeader ( i, &pObjMessage );
_tprintf( _T( "Mail From : %s\n" ), pObjMessage->getFromAddress( strValue ).t_str() );
_tprintf( _T( "Mail To : %s\n" ), pObjMessage->getToAddress( strValue ).t_str() );
_tprintf( _T( "Mail Subject : %s\n" ), pObjMessage->getSubject( strValue ).t_str() );
_tprintf( _T( "Mail Size : %ld\n"), pObjMessage->getSize() );
_tprintf( _T( "Mail Sent : %s\n" ), pObjMessage->getDate( strValue ).t_str() );
_tprintf( _T("\n") );
}
_EndMain:
_tprintf( _T("Disconnecting...\n") );
objPop3.Disconnect();
_tprintf( _T("Ready.\n") );
return 0;
}
/*
int _tmain(int argc, _TCHAR* argv[])
{
IPop3 *pPop3Server = NULL;
IEMailMessage *pPop3Mail = NULL;
VARIANT vt;
LONG lLastError = 0L;
LONG lNumMessages = 0L;
LONG lSize = 0L;
INT i;
HRESULT hr;
_bstr_t bstrHostName, bstrAccount, bstrPassword;
BSTR pErrorDescription = NULL;
BSTR bstrTemp = NULL;
LPTSTR cp;
BOOL bSecure = FALSE;
// initialize COM
CoInitialize(NULL);
hr = CoCreateInstance ( CLSID_Pop3, NULL, CLSCTX_INPROC_SERVER, IID_IPop3,
( void** ) &pPop3Server );
if( ! SUCCEEDED( hr ) )
{
pPop3Server = NULL;
_tprintf( _T("Failed to create ActiveXperts:Pop3Server.\n") );
goto _EndMain;
}
VariantInit ( &vt );
cp = ReadInput( _T("POP3 Server") );
bSecure = _stricmp( cp, _T("pop.gmail.com") ) == 0;
bstrHostName = cp;
bstrAccount = ReadInput( _T("POP3 mailbox account") );
bstrPassword = ReadInput( _T("POP3 mailbox password") );
if( bSecure )
{
pPop3Server->SetSecure( 995 );
pPop3Server->get_LastError ( &lLastError );
pPop3Server->GetErrorDescription( lLastError, &pErrorDescription );
_tprintf( _T("SetSecure, result: %ld (%ls)\n"), lLastError, pErrorDescription );
SysFreeString( pErrorDescription );
}
// Connect
_tprintf( _T("Connecting...\n") );
pPop3Server->Connect ( bstrHostName, bstrAccount, bstrPassword );
pPop3Server->get_LastError ( &lLastError );
pPop3Server->GetErrorDescription( lLastError, &pErrorDescription );
_tprintf( _T("Connect, result: %ld (%ls)\n"), lLastError, pErrorDescription );
if( lLastError != 0 )
goto _EndMain;
// Count messages
pPop3Server->CountMessages ( &lNumMessages );
pPop3Server->get_LastError ( &lLastError );
pPop3Server->GetErrorDescription( lLastError, &pErrorDescription );
_tprintf( _T("CountMessages, result: %ld (%ls)\n"), lLastError, pErrorDescription );
if( lLastError != 0 )
goto _EndMain;
_tprintf( _T("Number of messages in mailbox: %ld\n"), lNumMessages );
for( i = 1; i < lNumMessages + 1 ; i++ )
{
pPop3Server->GetEmailHeader ( i, &vt );
if( vt.vt != VT_DISPATCH )
continue;
if( ( pPop3Mail = ( IEMailMessage* ) vt.pdispVal ) == NULL )
{
VariantClear ( &vt );
continue;
}
pPop3Mail->get_FromAddress ( &bstrTemp );
_tprintf( _T("Mail From : %ls\n"), bstrTemp );
SysFreeString ( bstrTemp );
pPop3Mail->get_ToAddress ( &bstrTemp );
_tprintf( _T("Mail To : %ls\n"), bstrTemp );
SysFreeString ( bstrTemp );
pPop3Mail->get_Subject ( &bstrTemp );
_tprintf( _T("Mail Subject : %ls\n"), bstrTemp );
SysFreeString ( bstrTemp );
pPop3Mail->get_Size ( &lSize );
_tprintf( _T("Mail Size : %ld\n"), lSize );
pPop3Mail->get_Date ( &bstrTemp );
_tprintf ( "Mail Sent : %ls\n", bstrTemp );
SysFreeString ( bstrTemp );
VariantClear ( &vt );
_tprintf( _T("\n") );
}
pPop3Server->Disconnect ();
_EndMain:
if( pPop3Server != NULL )
{
pPop3Server->Release();
pPop3Server = NULL;
}
_tprintf( _T("Ready.\n") );
return 0;
}
*/
////////////////////////////////////////////////////////////////////////////////
LPTSTR ReadInput( LPCTSTR lptszTitle, BOOL bAllowEmpty )
{
static TCHAR tszInput [ 255 + 1 ] = { 0 };
_tprintf( _T("%s:\n"), lptszTitle );
do
{
_tprintf( _T(" > ") );
// scanf ( "%s", tszInput );
fflush(stdin);
fflush(stdout);
fgets( tszInput, 255, stdin );
if( tszInput[ 0 ] != _T('\0') && tszInput[ _tcslen( tszInput ) - 1 ] == _T('\n') )
tszInput[ _tcslen( tszInput ) - 1 ] = _T('\0');
} while( _tcslen ( tszInput ) == 0 && ! bAllowEmpty );
_tprintf( _T("\n") );
return tszInput;
}
You can download the full source code of this project from the ActiveXperts FTP site: ftp://ftp.activexperts-labs.com/samples/smtp-pop3-component/. There are many other working samples included with the product or on the FTP site.
The ActiveEmail Toolkit project ships with a set of Microsoft Visual Studio .NET samples. The projects are created with Microsoft Visual Studio 2008.
Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.