You are here:

ActiveXperts.com > Email Component > How to Use > Smtp > Visual C++
ActiveXperts Email Component

Quicklinks

ActiveXperts Email Component is a software development kit (SDK) that enables the user to send (SMTP) and receive (POP3) e-mail messages. ActiveXperts Email Component 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).


Introduction

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 give a phone number and a message body on the command prompt.

Step 1: Download and install the ActiveXperts Email Component

Download the ActiveXperts Email Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual C++ project

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':

Visual C

(Click on the picture to enlarge)

Select the kind of project, for instance a 'Hello, world!' application and click 'Finish':

Visual C

(Click on the picture to enlarge)

A new Project is created now.

Step 3: Refer to ActiveXperts Email Component Library

Before you can use ActiveXperts Email Component, you need to refer to ActiveXperts Email Component library. The actually reference files ship with the product and are located in the following directory:

C:\Program Files\ActiveXperts\Email Component\Samples\Visual C++\Include

Step 4: Declare and create ActiveXperts Email Component objects

On top of your code, declare the following objects (defined in 'MmWrapper.h'):

CSmtp          objSmtp;
CEMailMessage  objMessage;

Step 5: Send E-mail messages

You can now send E-mail messages.

The following code shows how to send an e-mail message through SMTP:

Appendix: Full source code

Following you can find the full source code which is also included in ActiveXperts Email Component package.

#include <comdef.h>
#include <atlbase.h>
#include <windows.h>
#include <stdio.h>

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

#include "..\..\include\aemail_i.h"
#include "..\..\include\aemail_i.c"
#include "..\..\include\aemailconstants.h"

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

LPTSTR ReadInput( LPCTSTR lptszTitle, BOOL bAllowEmpty = FALSE );

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



int main(int argc, char* argv[])
{
  IEMailMessage  *pEmail           = NULL;
  ISmtp          *pStmp            = NULL;
  LONG           lLastError        = 0L;
  HRESULT        hr;
  LPSTR          cp;
  _bstr_t        bstrHostName      = "";
  _bstr_t        bstrAccount       = "";
  _bstr_t        bstrPassword      = "";
  BSTR           pErrorDescription = NULL;
  BOOL           bSecure           = FALSE;
  BOOL           bHtml             = FALSE;
  VARIANT        vtMail;


    // initialize COM
  CoInitialize(NULL);

  hr = CoCreateInstance(CLSID_Smtp, NULL, 
      CLSCTX_INPROC_SERVER, IID_ISmtp, (void**) &pStmp);
  if( ! SUCCEEDED( hr ) )
  {
    pStmp = NULL;
    _tprintf( _T("Failed to create AxEmail:SmtpServer.\n") );
    goto _EndMain;
  }

  hr = CoCreateInstance(CLSID_EMailMessage, NULL, 
      CLSCTX_INPROC_SERVER, IID_IEMailMessage, (void**) &pEmail);
  if( ! SUCCEEDED( hr ) )
  {
    pEmail = NULL;
    _tprintf( _T("Failed to create AxEmail:SmtpMail.\n") );
    goto _EndMain;
  }

    cp          = ReadInput( _T("SMTP Server") );
  bSecure       = _stricmp( cp, _T("smtp.gmail.com") ) == 0;
  bstrHostName  = cp;
  bstrAccount   = ReadInput( _T("SMTP server account (optional)"), TRUE );
  if( bstrAccount.length() > 0 ) 
    bstrPassword  = ReadInput( _T("SMTP server password"), TRUE );

  if( bSecure )
  {
    pStmp->SetSecure( 465 );
    pStmp->get_LastError ( &lLastError );
    pStmp->GetErrorDescription( lLastError, &pErrorDescription );
    _tprintf( _T("SetSecure, result: %ld (%ls)\n"), lLastError, pErrorDescription ); 
    SysFreeString( pErrorDescription );
  }

  _tprintf( _T("Connecting...\n") );
  pStmp->Connect( bstrHostName, bstrAccount, bstrPassword );
  pStmp->get_LastError ( &lLastError );
  pStmp->GetErrorDescription( lLastError, &pErrorDescription );
  _tprintf( _T("Connect, result: %ld (%ls)\n"), lLastError, pErrorDescription ); 
  SysFreeString( pErrorDescription );
  if( lLastError != 0L )
    goto _EndMain;

  cp = ReadInput( _T("FROM address") );
  pEmail->put_FromAddress( _bstr_t( cp ) );
  pEmail->put_FromName( _bstr_t( cp ) );

  pEmail->put_Subject( _bstr_t( ReadInput( _T("Subject") ) ) );

  cp = ReadInput( _T("HTML formatted message body (y/n)") );
  if( cp[0] == 'y' || cp[0] == 'Y' )
    pEmail->put_BodyHtml( _bstr_t( ReadInput( _T("Message (HTML formatted)") ) ) );
  else
    pEmail->put_BodyPlainText( _bstr_t( ReadInput( _T("Message (Plain Text formatted)") ) ) );

//  pEmail->put_BodyType( asMESSAGE_BODY_PLAIN );    // Plain text (default)
  pEmail->put_Priority( asMESSAGE_PRIORITY_MEDIUM );  // Normal priority (default)
  pEmail->put_Encoding( asMESSAGE_ENCODING_DEFAULT );  // Default Encoding
  
  cp = ReadInput( _T("TO address") );

  pEmail->AddTo( _bstr_t( cp ), _bstr_t( cp ));

  _tprintf( _T("Sending...\n") );
  
  VariantInit ( &vtMail );
  vtMail.pdispVal = pEmail;
  vtMail.vt = VT_DISPATCH;
  
  pStmp->Send( &vtMail );
  pStmp->get_LastError( &lLastError );
  pStmp->GetErrorDescription( lLastError, &pErrorDescription );
  _tprintf( _T("Send, result: %ld (%ls)\n"), lLastError, pErrorDescription );
  SysFreeString( pErrorDescription );

  _tprintf( _T("Disconnecting...\n") );
  pStmp->Disconnect();


_EndMain:


  if( pStmp != NULL )
    pStmp->Release();

  if( pEmail   != NULL )
    pEmail->Release ();

  _tprintf( _T("Ready.\n") );

  return 0;
}

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

LPTSTR ReadInput( LPCTSTR lptszTitle, BOOL bAllowEmpty )
{
  static TCHAR    tszInput [ 255 + 1 ] = { _T('\0') };

  _tprintf( _T("%s:\n"), lptszTitle );
  do
  {
    _tprintf ( _T("   > ") );
    // scanf ( "%s", tszInput );
    fflush(stdin); 
    fflush(stdout); 
    _fgetts( tszInput, 255, stdin );
    if( tszInput[ 0 ] != _T('\0') && tszInput[ _tcsclen( tszInput ) - 1  ] == _T('\n') )
      tszInput[ _tcsclen( tszInput ) - 1  ] = _T('\0');
  } while( _tcsclen ( tszInput ) == 0 && ! bAllowEmpty );

  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.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The ActiveXperts Email Component 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.