ANSMTP Developers Center > Using ANSMTP in Visual C++

Introduction

ANSMTP is a SMTP component which supports all operations of SMTP/ESMTP protocols (RFC 821, RFC 822, RFC 2554). This tutorial covers the basics of sending email with ANSMTP in Visual C++.

Installation and Deployment

You should download the ansmtp installer and install it on your machine at first. If you want to distribute or deploy ansmtp without ansmtp installer, please click here to learn more.

C++ header files for ANSMTP

All header files for C++ are located in "Include" sub-directory of ANSMTP installation directory.

ANSMTP.IDL //MIDL file for ANSMTP, it was generated by OleView tool in Visual Studio.
 
ANSMTP.H  // header file of ANSMTP
ANSMTP.i_c

ANSMTP.tlh // header file for ANSMTP SmartPointer
ANSMTP.tli

Using ANSMTP with COM interface

This sample demonstrates how to send email in Visual C++ with COM interface.

#include "stdafx.h"
#include <comdef.h>
#include <iostream>
#include "ansmtp.h"
#include "ansmtp_i.c"

using namespace std;

VOID
SendMail( const char* lpszSenderName,
		 const char* lpszSenderAddr,
		 const char* lpszRecipientName,
		 const char* lpszRecipientAddr,
		 const char* lpszEmailBody )
{
  ::CoInitialize( NULL );
  IOBJ* pSmtp = NULL;
  HRESULT hr = ::CoCreateInstance( CLSID_OBJ,
                                   NULL,
                                   CLSCTX_ALL,
                                   IID_IOBJ, (LPVOID*)&pSmtp );

  if((pSmtp == NULL) || !SUCCEEDED(hr))
  {
    cout <<  "error with create ANSMTP instance" << endl;
    return;
  }

  LONG lRet = 0;
  pSmtp->Reset();
  pSmtp->put_ServerAddr(_bstr_t("")); //send email via dns lookup

  pSmtp->put_From( _bstr_t(lpszSenderName));
  pSmtp->put_FromAddr( _bstr_t(lpszSenderAddr));
	
  pSmtp->put_Subject( _bstr_t("This is a test from ansmtp"));
  pSmtp->put_BodyText( _bstr_t(lpszEmailBody));
  pSmtp->AddRecipient( _bstr_t(lpszRecipientName), 
                       _bstr_t(lpszRecipientAddr),
                       0, //0 normal recipient, 1 cc, 2 bcc 
                       &lRet );

  //pSmtp->AddAttachment( _bstr_t("c:\\test.doc"), &lRet ); //add an attachment
	

  pSmtp->SendMail( &lRet );

  if( lRet != 0 )
  {
    BSTR bOut = NULL;
    pSmtp->GetLastErrDescription( &bOut );
    cout << (const char*)_bstr_t( bOut ) << endl;
    ::SysFreeString( bOut );
    bOut = NULL;
  }
  else
  {
    cout << "Message delivered" << endl;
  }

  pSmtp->Release();
  pSmtp = NULL;
}

Using ANSMTP with Smart Pointer

Using the raw COM interface in Visual C++ is boring. Smart Pointer makes programming life more easier. The following code demonstrates how smart pointer facilitates your Visual C++ works.

#include "stdafx.h"
#include <comdef.h>
#include <iostream>
#include "ansmtp.tlh"
using namespace ANSMTPLib;
using namespace std;

void SendEmail()
{
  ::CoInitialize( NULL );
  IOBJPtr oSmtp = NULL;
  oSmtp.CreateInstance("ANSMTP.OBJ");
	
  oSmtp->ServerAddr = _bstr_t( "mail.adminsystem.net" );
  oSmtp->FromAddr = _bstr_t( "test@adminsystem.net" );
  oSmtp->AddRecipient( _bstr_t("Support Team"), 
                      _bstr_t("support@adminsystem.net"), 0 );

  oSmtp->Subject = _bstr_t("Test");
  oSmtp->BodyText = _bstr_t("Hello, this is a test....");
  
  if( oSmtp->SendMail() == 0 )
    cout << "Message delivered!" << endl;
  else
    cout << (const char*)(oSmtp->GetLastErrDescription()) << endl;
}

int main(int argc, char* argv[])
{
  SendEmail();
  return 0;
}

					

Asynchronous mode and event driving

If you want to use IOBJ in asynchronous mode, please refer to ANSMTPSink.h of VCSAMPLE2 in ANSMTP installation package. You just need to inherit from class CSmtpSink and override the event hanlder.

#include "ANSMTPSink.h"

class CMyDlg: public CDialog, /*IOBJ Events Handler*/CSmtpSink
{
....
protected:
  HRESULT __stdcall OnClosedHandler();
  HRESULT __stdcall OnSendingHandler( long nSent, long nTotalSize );
  HRESULT __stdcall OnErrorHandler( long nErrorCode, BSTR ErrorMessage );
  HRESULT __stdcall OnConnectedHandler();
  HRESULT __stdcall OnAuthenticatedHandler();
}

Full Sample

Please refer to VCSAMPLE1, VCSAMPLE2 and SendMailIsapi in ANSMTP 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.



2001-2008 © Copyright AdminSystem Software Limited. All rights reserved.   About us  Site Map