Click here to Skip to main content
15,744,193 members
Articles / Desktop Programming / MFC
Posted 23 Apr 2007


27 bookmarked

Junior Outlook

Rate me:
Please Sign up or sign in to vote.
4.61/5 (10 votes)
23 Apr 2007CPOL3 min read
Send e-mails easily from your application using MAPI ( Messaging API ).

Screenshot - NOutlookGif.gif


This article explains to you how to incorporate MAPI into a document view architecture application.


Sometime back I was asked to give training on document view architecture to my colleagues. So that's how I bumped into document view architecture. This got me thinking, since I had to explain to my colleagues about document view architecture in an interesting way.

This meant some cool demo application that demonstrates document view architecture.

How to enable MAPI support

Here is a screenshot of the application wizard, see the one circled in red, just click that checkbox.

Screenshot - WizardGif.gif

Now the question is how to use MAPI. Just go to File menu and click send...

Screenshot - MAPIMenu.gif

You will get a dialog similar to the following one, this one is provided by Outlook...

Screenshot - MAPSendDialog.gif

Take a look at the attached file name. It's the name of your document. What MFC does is that it creates a temporary copy of your document (internally calls CYourDocument::Serialize) by saving it to a temporary file, later on this file is deleted.

You will find code for the above process in CDocument::OnFileSendMail.

Behind the scenes

Now let's see what happens when we enable MAPI support for a document view project.

Open up your document class, you will message map macros. See what entries there are:

BEGIN_MESSAGE_MAP( CNOutlookDoc, CDocument )

Now you know how it happens. When you click on File->Send, the code in CDocument::OnFileSendMail gets executed.

You might have got an idea of what's happening. The dialog that you see when you click on File->Send is provided by Outlook.

Now open up Winword, just type in something and click on File->Send To->Mail Recipient (Attachment). You will see a similar window as we have.

Customizing default behavior

I didn't want a dialog to popup when I click on Send, since I had my own dialog and text boxes and other options. So I had to customize this behavior.

Here is the code for customizing this behavior...

void CNOutlookDoc::OnFileSendMail()
     SetModifiedFlag( TRUE );
     HINSTANCE hMail = NULL;
     hMail = ::LoadLibrary( _T( "MAPI32.DLL" ));
     if (hMail == NULL)
        AfxMessageBox( AFX_IDP_FAILED_MAPI_LOAD );
     ASSERT( hMail != NULL );
            (ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
     PFNSENDMAIL lpfnSendMail = 0;
     lpfnSendMail = ( PFNSENDMAIL )GetProcAddress( hMail, "MAPISendMail" );
     if( lpfnSendMail == NULL )
         AfxMessageBox( AFX_IDP_INVALID_MAPI_DLL );

     // Assign message

     MapiRecipDesc mapiRecipient;
     memset( &mapiRecipient, 0, sizeof( mapiRecipient ));
     mapiRecipient.ulRecipClass = MAPI_TO;
     mapiRecipient.lpszName = "Nibu thomas"; // Recipient name
     m_csTo = ( "SMTP:" + m_csTo );
     mapiRecipient.lpszAddress = m_csTo.GetBuffer( 0 );

     // Prepare a mapi message
     MapiMessage mapiMessage;
     memset( &mapiMessage, 0, sizeof( mapiMessage ));
     mapiMessage.nRecipCount = 1;
     mapiMessage.lpRecips = &mapiRecipient;
     m_csSubject = _T( "Subject: " ) + m_csSubject;
     mapiMessage.lpszSubject = m_csSubject.GetBuffer( 0 );
     mapiMessage.lpszNoteText = m_csMessage.GetBuffer( 0 );

     // Prepare attachment
     MapiFileDesc mapiFileAttachments;
     memset( &mapiFileAttachments, 0, sizeof( mapiFileAttachments ));
     if( PathFileExists( m_csAttachFileName ))
          mapiFileAttachments.nPosition = (ULONG)-1;
          mapiFileAttachments.lpszPathName = m_csAttachFileName.GetBuffer( 0 );
          mapiFileAttachments.lpszFileName = mapiFileAttachments.lpszPathName;
          mapiMessage.nFileCount = 1;
          mapiMessage.lpFiles = &mapiFileAttachments;

     // Send message
     int nError = lpfnSendMail(0, ( ULONG )AfxGetMainWnd()->GetSafeHwnd(), 
                    &mapiMessage, MAPI_LOGON_UI, 0);

     // Release the buffer that we just locked
     if (nError != SUCCESS_SUCCESS && nError != 

     ::FreeLibrary( hMail );

MapiFileDesc structure lists the attachments that are to be sent.
MapiMessage structure lists the message that is to be sent.
MapiRecipDesc structure lists the recipients.

MapiSendMail is not the only API that's available. Just look up MAPI.h for more options.

Now you know how simple it is to customize MAPI code in document view architecture.

MAPI in a dialog

It's quite easy to do the same in a dialog. Just fill in the required fields of the above structures and then send the message using MapiSendMail. MAPI. This can be used anywhere.

Interesting ideas

I have some other interesting ideas based on MAPI. One of them is an error reporting tool, i.e. a tool that mails to you any error that occurs in your application much like a Window error reporting dialog.

MAPI provides you with interesting options.


Look up the above structures carefully in the docs to see what you can do. It's fairly easy to do. I will also add more on these structures as I get time.

Hope this helps you.


  • Created on 4/23/2007


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer Microsoft
India India
My Technical Blog

I am a computer programmer who started his career as a Java programmer in 2000. Wrote a game and several applications like editors, DB apps etc in Java. My fascination for programming went on a high in the following years. I did a post graduate diploma of 2 years because of which I got introduced to a plethora of Languages. My favorite at that time was VB because it was way too easy to program, intelli-sense was too cool.

I joined an NGO in 2002 as part time computer programmer. Primary responsibility was to develop their website and to write apps for them in VB. During this process I got introduced to DHTML, CSS, HTML, JavaScript, wow had hell of a time. Learned and learned and learned during this time. I got into Visual C++ because of my post graduation in computer applications. I was forced into this powerful language but how lucky I am. Initially I was scared of CreateFont API Wink | ;) but now its a piece of cake. Smile | :)

As of now I'm working as Visual C++ engineer with Microsoft. My passion for this language never ends. Its the raw power of the language, the kind of performance and flexibility it provides, that keeps me motivated to continue working in this language. Started working in VC6 and all through till the latest version of Visual C++. Smile | :)

I'm part of the Microsoft Developer Support - Programming Languages Team. Enjoying every day.

Comments and Discussions

GeneralNo need to get dialog box while sending message Pin
John5024-Nov-09 2:23
John5024-Nov-09 2:23 
Hi Nibu,
Great application... I need small help in this.. I am able to send emails using your application, but every time while sending it is aksing to to click on yes..

I don't want to get that message, with out getting that message i want to send emails to the respected persons...

Can you please help me out as it is urgent requirement for me...

GeneralHai Pin
John50218-Mar-09 1:33
John50218-Mar-09 1:33 
QuestionProblem with MAPIsendMail Pin
harishkj11-Jan-09 19:31
harishkj11-Jan-09 19:31 
Generalhi Pin
Hiran Das4-Jul-07 21:09
Hiran Das4-Jul-07 21:09 
GeneralRe: hi Pin
Nibu babu thomas5-Jul-07 5:35
Nibu babu thomas5-Jul-07 5:35 
GeneralRe: hi Pin
Hiran Das5-Jul-07 18:19
Hiran Das5-Jul-07 18:19 
GeneralCrash with MAPILogon Pin
worksopbenny1-May-07 22:45
worksopbenny1-May-07 22:45 
GeneralRe: Crash with MAPILogon Pin
Nibu babu thomas2-May-07 0:11
Nibu babu thomas2-May-07 0:11 
GeneralRe: Crash with MAPILogon Pin
Nibu babu thomas2-May-07 0:49
Nibu babu thomas2-May-07 0:49 
QuestionAdding to existing project? Pin
andrewtruckle30-Apr-07 20:41
andrewtruckle30-Apr-07 20:41 
AnswerRe: Adding to existing project? Pin
Nibu babu thomas1-May-07 17:19
Nibu babu thomas1-May-07 17:19 
QuestionRe: Adding to existing project? Pin
andrewtruckle1-May-07 20:07
andrewtruckle1-May-07 20:07 
AnswerRe: Adding to existing project? Pin
Nibu babu thomas1-May-07 20:10
Nibu babu thomas1-May-07 20:10 
Generalto build it with vs2005 Pin
andré_k24-Apr-07 1:22
andré_k24-Apr-07 1:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.