Using AddEmail in C++ projects

<< Click to Display Table of Contents >>

Navigation:  Samples and Tutorials > C++/MFC/ATL >

Using AddEmail in C++ projects

 

To use AddEmail in your C++ project perform the following steps:

 

1. Import AddEmail type library: add #import directive to StdAfx.h or other appropriate header.

 

#import "AddEmail\AddEmail.tlb" named_guids

 

2. Declare a variable that will hold a reference to the SmtpMail object:

 

private AddEmailLib::ISmtpMailPtr m_spSmtpMail;

 

3. Create an instance of the SmtpMail object:

 

m_spSmtpMail.CreateInstance(__uuidof(AddEmailLib::SmtpMail));

 

4. Set SMTP server address, port, username and password:

 

m_spSmtpMail->PutSmtpServer("mail.myserver.com");

m_spSmtpMail->PutSmtpPort(25);

m_spSmtpMail->PutSmtpUsername("jsmith");

m_spSmtpMail->PutSmtpPassword("mypassword");

 

5a. Send an e-mail synchronously using SimpleSend or SimpleSendAttachment:

 

BSTR bstrError;

int resultCode = m_spSmtpMail->SimpleSend("jsmith@myserver.com", "jane@someserver.com;james@someserver.com", "test", "Test message", &bstrError);

if (resultCode == 0)

{

// E-mail was sent successfully

}

else

{

// Send failed, bstrError contains detailed error description

}

 

5b. Alternatively, create and fill MailMessage object and send it synchronously using Send or asynchronously using SendAsync:

 

// Create message and setup subject and body

AddEmailLib::IMailMessagePtr spMessage;

spMessage.CreateInstance(__uuidof(AddEmailLib::MailMessage));

spMessage->PutMessageSubject("test");

spMessage->PutMessageBody("Test message");

 

// Setup sender

AddEmailLib::IMailAddressPtr spFrom;

spFrom.CreateInstance( __uuidof(AddEmailLib::MailAddress) );

spFrom->PutAddress("jsmith@myserver.com");

spFrom->PutName("John Smith");

spMessage->PutSender(spFrom);

 

// Setup first recipient

AddEmailLib::IMailAddressPtr spTo;

spTo.CreateInstance(__uuidof(AddEmailLib::MailAddress));

spTo->PutAddress("jane@someserver.com");

spTo->PutName("Jane Smith");

spMessage->AddRecipient(spTo);

 

// Setup second recipient

spTo.CreateInstance(__uuidof(AddEmailLib::MailAddress));

spTo->PutAddress("james@someserver.com");

spTo->PutName("James Smith");

spMessage->AddRecipient(spTo);

 

// Send prepared message

BSTR bstrError;

int resultCode = m_spSmtpMail->Send(spMessage, TRUE, &bstrError);

if (resultCode == 0)

{

// E-mail was sent successfully

}

else

{

// Send failed, bstrError contains detailed error description

}

 

// Alternatively, send prepared message asynchronously

//int messageNumber = m_spSmtpMail->SendAsync(spMessage, TRUE);

 

6. If the e-mail is sent asynchronously your application will need to process events to find out when the email was sent successfully or send operation failed. To process events from the SmtpMail object you need to add ATL support to your project, inherit your class from IDispEventImpl<> template, declare event handlers, create sink map and subscribe to events:

 

class CMyClass :

  public IDispEventImpl<1, CMyClass, &AddEmailLib::DIID__ISmtpMailEvents,

                           &AddEmailLib::LIBID_AddEmailLib, 1, 0>

{

public:

   // SmtpMail events

  void __stdcall OnStatusChange(LONG messageNumber, AddEmailLib::MailStatus newStatus);

  void __stdcall OnProgress(LONG messageNumber, LONG bytesSent, LONG bytesTotal);

 

   // ATL sink for SmtpMail events

   BEGIN_SINK_MAP(CMyClass)

           SINK_ENTRY_EX(1, AddEmailLib::DIID__ISmtpMailEvents, 1, OnStatusChange)

           SINK_ENTRY_EX(1, AddEmailLib::DIID__ISmtpMailEvents, 2, OnProgress)

   END_SINK_MAP()

}

 

// Subscribe to events

DispEventAdvise(m_spSmtpMail);

 

Please refer to the Reference section of this manual for detailed description of AddEmail objects, methods and properties. Included C++ samples provide code snapshots for common operations such as sending text e-mails, sending HTML e-mails, adding attachments to e-mails, creating HTML e-mails with embedded images.