Click here to Skip to main content
Licence CPOL
First Posted 23 Apr 2007
Views 27,096
Downloads 173
Bookmarked 23 times

Junior Outlook

By | 23 Apr 2007 | Article
Send e-mails easily from your application using MAPI ( Messaging API ).

Screenshot - NOutlookGif.gif

Introduction

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

Background

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 )
    //{{AFX_MSG_MAP(CNOutlookDoc)
   ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
   ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
   //}}AFX_MSG_MAP
END_MESSAGE_MAP 

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 );
        return;
     }
     ASSERT( hMail != NULL );
     typedef ULONG ( PASCAL *PFNSENDMAIL )
            (ULONG, ULONG, MapiMessage*, FLAGS, ULONG);
     PFNSENDMAIL lpfnSendMail = 0;
     lpfnSendMail = ( PFNSENDMAIL )GetProcAddress( hMail, "MAPISendMail" );
     if( lpfnSendMail == NULL )
     {
         AfxMessageBox( AFX_IDP_INVALID_MAPI_DLL );
         return;
     }

     // Assign message
     USES_CONVERSION;

     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
     m_csTo.ReleaseBuffer();
     m_csMessage.ReleaseBuffer();
     m_csSubject.ReleaseBuffer();
     if (nError != SUCCESS_SUCCESS && nError != 
        MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
     {
       AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
     }

     ::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.

Conclusion

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.

History

  • Created on 4/23/2007

License

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

About the Author

Nibu babu thomas

Software Developer
Microsoft
India India

Member

Follow on Twitter Follow on Twitter
Nibu has been programming for 6 years now. He loves programming. Not a geek but trying to be one.
 
Nibu loves playing guitar and piano in his spare time.
Nibu loves his family a lot and loves to spent time with them. He is married to a wonderful woman. Smile | :)
 
Nibu is passionate about leading a good christian life. He dedicates all his success to Jesus. In fact this is what he says "I am whatever I am by the grace of Jesus" Smile | :) .
 
He loves CodeProject.
 
He has got a programming tips and tricks site. You could help him improve with your suggestions.
 
Please visit bits and bytes. Your feedback is very important.
 
I brag here -> Random thoughts.
 
What I post on CodeProject or other forums has nothing to do with Microsoft or any of the companies that I work/worked for. These are my own ideas/thoughts unless otherwise explicitly mentioned.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNo need to get dialog box while sending message PinmemberJohn5022:23 4 Nov '09  
GeneralHai PinmemberJohn5021:33 18 Mar '09  
QuestionProblem with MAPIsendMail Pinmemberharishkj19:31 11 Jan '09  
Generalhi PinmemberHiran Das21:09 4 Jul '07  
GeneralRe: hi PinmemberNibu babu thomas5:35 5 Jul '07  
GeneralRe: hi PinmemberHiran Das18:19 5 Jul '07  
GeneralCrash with MAPILogon Pinmemberworksopbenny22:45 1 May '07  
GeneralRe: Crash with MAPILogon PinmemberNibu babu thomas0:11 2 May '07  
GeneralRe: Crash with MAPILogon PinmemberNibu babu thomas0:49 2 May '07  
QuestionAdding to existing project? Pinmemberandrewtruckle20:41 30 Apr '07  
AnswerRe: Adding to existing project? PinmemberNibu babu thomas17:19 1 May '07  
Smile | :)
 
I was planning to add this section to the article.
 
Answer:
 
Step 1: Open up your menu resource add a menu item called "Send mail" or whatever
 
Step 2: Set it's id as ID_FILE_SEND_MAIL
 
Step 3: Open up your document cpp file
Step 4: Add message entries likewise
 

BEGIN_MESSAGE_MAP(CNOutDoc, CDocument)
    //{{AFX_MSG_MAP(CNOutDoc)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    // DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
    <code>ON_COMMAND( ID_FILE_SEND, CDocument::OnFileSendMail )
    ON_UPDATE_COMMAND_UI( ID_FILE_SEND, CDocument::OnUpdateFileSendMail )</code>
END_MESSAGE_MAP()
 
That's all. Hope this helps.
 

Nibu thomas
A Developer
 
Programming tips[^]  My site[^]

QuestionRe: Adding to existing project? Pinmemberandrewtruckle20:07 1 May '07  
AnswerRe: Adding to existing project? PinmemberNibu babu thomas20:10 1 May '07  
Generalto build it with vs2005 Pinmemberandré_k1:22 24 Apr '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 23 Apr 2007
Article Copyright 2007 by Nibu babu thomas
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid