ANSMTP Developers Center > Using ANSMTP in Visual C++
Note: this article is only suitable for ANSMTP 6.4 or later.
For ANSMTP 6.3 or earlier version, please click here.
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++.
How to send email with COM interface;
How to use smart pointer;
How to send email in asynchronous mode;
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.
AOSMTP.IDL //MIDL file for ANSMTP, it was generated by OleView tool in Visual Studio. AOSMTP.H // header file of ANSMTP AOSMTP.i_c AOSMTP.tlh // header file for ANSMTP SmartPointer AOSMTP.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 "aosmtp.h"
#include "aosmtp_i.c"
using namespace std;
VOID
SendMail( const char* lpszSenderName,
const char* lpszSenderAddr,
const char* lpszRecipientName,
const char* lpszRecipientAddr,
const char* lpszEmailBody )
{
::CoInitialize( NULL );
IMail* pSmtp = NULL;
HRESULT hr = ::CoCreateInstance( CLSID_Mail,
NULL,
CLSCTX_ALL,
IID_IMail, (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 "aosmtp.tlh"
using namespace AOSMTPLib;
using namespace std;
void SendEmail()
{
::CoInitialize( NULL );
IMailPtr oSmtp = NULL;
oSmtp.CreateInstance("AOSMTP.Mail");
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 IMail 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, /*IMail 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.
|