Using MAPI to Send E-Mail

Environment: ATL, COM, Visual C++ 6, MFC, Microsoft Outlook Express, Win XP, Win 2000

Introduction

During software development, I have seen many applications that require incorporating e-mail support. I remember once I was to develop an application that sends e-mails at a specific time to specific people with the customized messages. For that, I developed a COM service that used MAPI. MAPI, or Messaging Application Programming Interface, is the standard messaging architecture and a complete set of functions and object-oriented interfaces. Here I have an Email component and a COM DLL, which is a set of messaging functions that helps you create messaging-enabled applications.

Note: MAPI is used by various industry-standard e-mail clients, such as the Microsoft Exchange client, all versions of Microsoft Outlook and Outlook Express, including QUALCOMM Incorporated (Eudora) and Netscape Communications Corporation. So, you can use this component with these client applications also.

Component Design

The CLSID of the E-mail component is CLSID_Mail and it has only one interface, IMail, with the interface ID IID_IMail.

CMail is the implementation class and contains the following data members:

Data Member Function Pointer for
m_MAPILogon MAPILogon
m_MAPISendMail MAPISendMail
m_MAPISendDocuments   MAPISendDocuments
m_MAPIFindNext MAPIFindNext
m_MAPIReadMail MAPIReadMail
m_MAPIResolveName MAPIResolveName
m_MAPIAddress MAPIAddress
m_MAPILogoff MAPILogoff
m_MAPIFreeBuffer MAPIFreeBuffer
m_MAPIDetails MAPIDetails
m_MAPISaveMail MAPISaveMail

IMail has the following functions:

Function Description
IsMapiInstalled Checks whether the MAPI is installed on the system. It searches the Win.INI file for the MAPI key. If found, it returns S_OK.
InitMapi After checking through IsMapiInstalled, it initializes the MAPI function pointers. The method should be called once before using the component.
put_strProfileName Takes the name of the Outlook profile name you are using to send the e-mail.
put_strEmailAddress Sets the recipient e-mail address. You can specify more than one comma (,) separated recipient addresses.
put_strRecipient The name you want to specify for the recipients, sets the e-mail names of the sender—it may be different from your specified profile e-mail address.
put_strSubject Sets the subject of the e-mail
get_strSubject Returns the e-mail subject
put_strMessage Sets e-mail message text
get_strMessage Returns message text
put_strAttachmentFilePath   Sets the e-mail attachment with the full path, such as “c:\abc.txt”
get_strAttachmentFilePath Returns the path of the e-mail attachment
put_strAttachmentFile The display name of the attachment (Sample.txt); by default, its the same as that of specified in put_strAttachmentFilePath.
get_strAttachmentFile Returns the attachment file name
Logon Opens a new logon session if not already opened, using specified outlook profile, name and the profile password; you must logon before sending the e-mail. I have set the password to NULL, assuming that the profile you will specify have NO password, but you can specify your own password. Automatically called by Send method.
Logoff Log off, and closes the session. Automatically called by the Send method after sending the e-mail.
Send Sends the e-mail, requires valid Outlook profile name, recipient e-mail address, and a login session to send e-mail.

Steps to Execute the Demo Project

The demo project demonstrates the way you can use the component to send an e-mail. To execute the demo project, the following settings are required:

Step 1: You must have some valid Output Express e-mail account or create one named TestProfile.

You can create an e-mail profile in Express from the Tools>Accounts menu. This will open the Internet Accounts property sheet. On the All tab, click Add and then Mail. A wizard will let you create an e-mail account and specify the valid e-mail address. For a Hotmail account, the wizard automatically sets the names of e-mail servers.

After successfully creating the account, select the account name from the list on the All tab (for a Hotmail account, the default account name is Hotmail). Open the account’s properties by pressing the Properties button and change the account name from the default name (say Hotmail) to TestProfile.

Step 2: Register the DLL

All COM DLLs are required to be registered. After copying the DLL source code, you can register the DLL by right-clicking the DLL and selecting the Register DLL or Register COM component option, or simply by double-clicking the DLL. Compiling the DLL code in Visual Studio will automatically register the DLL.

Step 3: Log on to the net

Make sure you are logged on to the net. If not, Outlook will fail to deliver the e-mail; however, you can still check the composed e-mail in your outbox.

How to Send E-Mail

After specifying the information in the dialog box, click the Send button. A warning message will be displayed; click the Send button.

Note: These dialog boxes are displayed just to demonstrate the process. You can even hide them.

Here is the CTestEmailDlg::OnSend method, which is used to pass the user-specified information to the e-mail object and calls the IMail::Send method after setting everything.


//##//##//////////////////////////////////////////////////////////

///////////////////////////////////////////////
//
// void CTestEmailDlg::OnSend()
//
// This module is called when the Send button is pressed and
// uses the Email COM DLL to send e-mail – Aisha Ikram
// Note: Don’t forget to initialize the COM library using
// CoInitialize(NULL);
//##//##//////////////////////////////////////////////////////////

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

void CTestEmailDlg::OnSend()
{
UpdateData();
try{

CComPtr objMail;

HRESULT hr;
// make sure the DLL is registered
hr = objMail.CoCreateInstance(CLSID_Mail);
if(SUCCEEDED(hr))
{
if(hr== S_OK)
{
// Profile name is compulsory; this is the Outlook profile.
// I used “Outlook Express” because configuring it is easier
// than “MS Outlook.”
// Make sure to specify the correct sender’s address for this
// profile and make sure that Outlook Express is the default
// e-mail client.

if(m_strProfile.IsEmpty())
{
AfxMessageBox(“Please specify email profile name “);
return;
}

if(m_strTo.IsEmpty())
{
AfxMessageBox(“Please specify recipient’s email address “);
return;
}
// by default, it’s TestProfile, assumes that a profile
// with this name exists in outlook

hr= objMail->put_strProfileName((_bstr_t)m_strProfile);

hr = objMail->put_strSubject((_bstr_t)m_strSubject);

// this is the e-mail or set of e-mail addresses
// (separated by ,) which is actually used to send email

hr = objMail->put_strEmailAddress((_bstr_t)m_strTo);

// recipient is just to show the display name
hr = objMail->put_strRecipient((_bstr_t)m_strTo);

hr = objMail->put_strAttachmentFilePath((_bstr_t)
m_strAttachment);

hr = objMail->put_strMessage((_bstr_t)m_strMessage);

hr= objMail->Send();

if(hr!=S_OK)
AfxMessageBox(“Error, make sure the info is correct”);
} //if
} //if
} //try
catch(…)
{
AfxMessageBox(“Error, make sure specified info is correct”);
}
}

Check the mail in the recepient inbox.

Where to Use the Component

This component can be extended to incorporate functionalities such as opening an existing inbox to read e-mails automatically from the Inbox, composing new e-mails, and so forth. It can be used to automatically send e-mails with customized user messages and attachments to specific people at a particular time, especially when using some .exe servers or NT services.

About the Author

Aisha is an M.Sc.in Computer Science with a top position in her university. She has worked mainly in VC++ 6, MFC, ATL, COM/DCOM, ActiveX, C++, ASP, VB, SQL, and so on. These days, she is working on the dot-Net framework, managed C++, and C#. She is inspired by nature and loves to seek knowledge. She is so fond of travelling that she don’t want to die without seeing this whole world.

Website: http://aisha.hostmb.com
E-mail: [email protected]

If there are any suggestions or comments, you are most welcome to contact me. My Web site is http://aisha.hostmb.com.

Downloads


Download Demo project – 116 Kb



Download Source project – 229 Kb

More by Author

Must Read