Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / MFC
Article

CMapiAdmin - MAPI wrapper

Rate me:
Please Sign up or sign in to vote.
4.65/5 (17 votes)
25 Jan 2000 236.7K   4.8K   42   70
A class to encapsulate extended MAPI functions.

Overview

The CMapiAdmin class contains functions that make using extended MAPI a bit easier. Having the functionality encapsulated in a class reduces the overhead of coding, research, and the learning curve involved in getting some of these things done for the first time. The illustration below shows the structure of the class. Nodes in the tree with a key associated with the icon are protected functions or members and are used internally to the class (explained for developers not familiar with the Microsoft Visual C++ IDE) and are therefore not 'exposed'.

A bit of dipping into the registry has proved to be essential for the ability to remove a service when only given the StoreID (a.k.a. EntryID). In the source code, there is a CRegistryKey class which is used by CMapiAdmin. They can't be separated unless you want to use Win32 registry functions instead of the class.

CMapiAdmin Class Outline

Public Methods

bool GetStorePath(CString strStoreID (in), CString& strStorePath (out))

Requires a string representation of the binary InformationStoreID. This function is typically used to find out the full pathname of a PST file used for "Personal Folders".

bool RemoveService(BYTE* pbyEntryID, DWORD dwSize)

Requires a binary array representing the InformationStoreID. This function will remove the specified store. This is a more accurate way to remove such a service (see other version(s) of this function). Having said that, I recently found that you can have more than one service with the same EntryID (a.k.a. StoreID), and this function now removes all that are found (er... with the same EntryID)

bool RemoveService(LPTSTR lpszDisplayName)

Requires a pointer to a string containing the display name of the service you want removed. This function will remove all the services found with that name. WARNING: It is possible to have more than one service with the same name.

bool GetProfileName(LPTSTR& lpszProfileName)

If you log on using an existing session, you may then want to know exactly which profile you are using.

bool CreateMessage(DWORD dwRecipientCount, 
                   LPCTSTR* ppRecipents, 
                   LPCTSTR pMessageSubject, 
                   LPCTSTR pMessageText, 
                   BOOL bHighImportance);
bool CreateNewProfile(LPTSTR lpszNewName)
bool CreateMsgService(LPTSTR lpszService, LPTSTR lpszDisplayName)
bool Logon(ULONG ulUIParam=NULL, 
           LPTSTR lpszProfileName=NULL, 
           LPTSTR lpszPassword=NULL, 
           FLAGS flFlags=NULL);

If you do not supply parameters, this function will use MapiLogonEx to attempt to log on using an existing session. This method of logging on is useful for projects which work with or within applications like Microsoft Outlook.

Example use of CMapiAdmin:

Note: Make sure you've called Co(Un)Initialise():

HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
    MessageBox(0, "CoInitialize Failed", "InitInstance Error", MB_OK);
        return FALSE;
}

and:

CoUninitialize();

Simple send message to single recipient:

CMapiAdmin mapi;
if ( mapi.Logon() )
{
    LPCTSTR ppRecipients[1] = {"jason.hattingh@csfb.com"};
    mapi.CreateMessage(1, ppRecipients, "Hello Geezah!", 
                       "Here is a test message", TRUE);
    if (mapi.SendMessage())
        AfxMessageBox("Message sent successfully");
}

Example use inside COM object:

There are 2 sample projects that make use of CMapiAdmin.

The Visual Basic Project uses CDO (DLL included in the zip) to display the stores available. When a user clicks the "GetPath" or the "Delete Service" button, the appropriate function is called in the COM object (who's Visual C++ Project is included in a zip file).

For example, the following code in VB:

VB
Private Sub Command1_Click()
    Dim cStore As MAPI.InfoStore
    Dim sStoreID As String

    Set cStore = cStores.Item(List1.ListIndex + 1)
    sStoreID = cStore.ID

    Dim MsgAdmin As New MessageServiceAdmin

    Dim nReturn As Integer

    nReturn = MsgAdmin.RemoveByEntryID(sStoreID)

    If nReturn = 0 Then
        MsgBox "Service Removed Successfully"
    Else
        MsgBox "Failed to Remove the Service"
    End If
End Sub

...relates to the following C++ code in the COM object (which uses CMapiAdmin):

STDMETHODIMP CMessageServiceAdmin::RemoveByEntryID(VARIANT vEntryID, 
                                                   VARIANT* pvReturn)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState())

    // Start by setting the default return value to S_FALSE:
    V_VT(pvReturn) = VT_I2;
    V_I2(pvReturn) = S_FALSE;

    ////////////////////////////////////////////////////////
    // CONVERT THE STRING TO A BYTE ARRAY:

    ...Conversion code here...

    // END STRING TO BYTE ARRAY CONVERSION
    /////////////////////////////////////////////////////////////

    CMapiAdmin MapiAdmin;

    if (! MapiAdmin.Logon() ) 
        return S_FALSE;

    if ( ! MapiAdmin.RemoveService( pByte, (DWORD)nSize ) )
        return S_FALSE;

    // Set the return to ZERO which means all is ok (a non zero normally
    // represents an error code)
    V_VT(pvReturn) = VT_I2;
    V_I2(pvReturn) = S_OK;
    return S_OK;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralForce MAPI to download a messages Pin
27-Mar-02 5:39
suss27-Mar-02 5:39 
QuestionSet Content-Type? Pin
22-Mar-02 20:22
suss22-Mar-02 20:22 
GeneralHi Pin
21-Mar-02 5:59
suss21-Mar-02 5:59 
GeneralRe: Hi Pin
Carlos Antollini21-Mar-02 6:06
Carlos Antollini21-Mar-02 6:06 
GeneralRe: Hi Pin
21-Mar-02 6:14
suss21-Mar-02 6:14 
GeneralRe: Hi Pin
Carlos Antollini21-Mar-02 6:25
Carlos Antollini21-Mar-02 6:25 
GeneralRe: Hi Pin
21-Mar-02 6:32
suss21-Mar-02 6:32 
GeneralView E-Mails from file. Pin
MartenS5-Oct-01 3:42
MartenS5-Oct-01 3:42 
I want to write a program that can show E-Mails from file. Can I do it with MAPI or should I use something else instead of it?

Thanks in advance,

MartenS
GeneralRe: View E-Mails from file. Pin
5-Oct-01 3:55
suss5-Oct-01 3:55 
GeneralRe: View E-Mails from file. Pin
MartenS8-Oct-01 4:35
MartenS8-Oct-01 4:35 
GeneralRe: View E-Mails from file. Pin
Andrew Schetinin15-Jun-03 2:19
Andrew Schetinin15-Jun-03 2:19 
QuestionWindows 98 Coinitialize(NULL) failed???? Pin
15-Aug-01 19:01
suss15-Aug-01 19:01 
AnswerRe: Windows 98 Coinitialize(NULL) failed???? Pin
26-Apr-02 23:49
suss26-Apr-02 23:49 
GeneralMAPI: Internet E-mail service configuration Pin
Stole26-Jun-01 3:31
Stole26-Jun-01 3:31 
GeneralCMapiAdmin in a NT service Pin
Alfred Boehme4-Jun-01 21:02
Alfred Boehme4-Jun-01 21:02 
GeneralRe: CMapiAdmin in a NT service Pin
Claudius Mokler4-Jun-01 21:28
Claudius Mokler4-Jun-01 21:28 
GeneralRe: CMapiAdmin in a NT service Pin
26-Sep-01 1:46
suss26-Sep-01 1:46 
GeneralError Pin
Igor Emelyanov28-May-01 20:25
Igor Emelyanov28-May-01 20:25 
GeneralRe: Error Pin
28-Jun-01 4:38
suss28-Jun-01 4:38 
QuestionHow to trap the delete in Outlook Client Pin
Manjunath N.A18-Jan-01 6:38
Manjunath N.A18-Jan-01 6:38 
GeneralAttachment Pin
22-Nov-00 13:26
suss22-Nov-00 13:26 
GeneralRe: Attachment Pin
5-Mar-02 3:02
suss5-Mar-02 3:02 
GeneralClients that support Extended MAPI Pin
Member 102214014-Sep-00 20:36
Member 102214014-Sep-00 20:36 
GeneralRe: Clients that support Extended MAPI Pin
Jason Juneau22-Sep-00 10:49
sussJason Juneau22-Sep-00 10:49 
GeneralSetting priority of a message in MFC using MAPI Pin
Member 102214017-Aug-00 20:07
Member 102214017-Aug-00 20:07 

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.