Sending a Message Without Using The MAPI Interface

You can also create your own form to send messages from. Your form can provide the means for the user to enter the standard message items, with code behind the form providing the functionality needed to send the message to the intended recipients.

Here's a look at a sample form that would replace the MAPI common dialog box for sending messages:

Creating a Custom Send Note

In this example, we're using text boxes for the addressee lines, subject and body of the message. We've incorporated a Return Receipt check box, allowing the user to request a receipt for the message. The final items, the Send and Cancel buttons provide the starting point to the functionality that sends the message.


Sub cmdSend_Click()
    'This procedure is called to
    'send out a message

    'Set up our variables
    Dim iStatus As Long
    Dim tMessage As MAPIMessage
    Dim iNumRecips As Integer
    Dim tFiles As MAPIFile
    Dim iNumFiles As Integer
    
    'first, logon to MAPI    
    iStatus = MAPILogon(0&, "", "", MAPI_LOGON_UI, 0&, gMAPISession)
    If iStatus <> SUCCESS_SUCCESS Then
        MsgBox "Unable to logon to MAPI"
        Exit Sub
    End If

    '---------------------------------------------------
    'Establishing recipients
    '---------------------------------------------------
    'Check to see if 1 or 2 recipients specified.
    If Not IsNull(txtCC) Then
        '2 recipients; TO and CC
        iNumRecips = 2
    Else
        'only the TO recipient was specified.
        iNumRecips = 1
    End If

    'Set up our structure
    ReDim tRecip(1 To iNumRecips) As MAPIRecip

    'fill in the structures as needed
    tRecip(1).Name = txtTO
    tRecip(1).RecipClass = MAPI_TO

    'If applicable, set up the CC addressee
    If Not IsNull(txtCC) Then
        tRecip(2).Name = txtCC
        tRecip(2).RecipClass = MAPI_CC
    End If
    
    'Update the message header with the number of recipients
    tMessage.RecipCount = iNumRecips

    '---------------------------------------------------
    'Establishing attached file counts
    '---------------------------------------------------
    tMessage.FileCount = 0

    '---------------------------------------------------
    'Establishing the actual message
    '---------------------------------------------------
    tMessage.Subject = txtSubject
    tMessage.NoteText = txtNoteText

    If WhatForm!cbReturnReceipt Then
        tMessage.flags = MAPI_RECEIPT_REQUESTED
    End If

    '---------------------------------------------------
    'Send the message
    '---------------------------------------------------
    'MAPISendMail is a custom function that sets up the message
    iStatus = MAPISENDMAIL(gMAPISession, 0&, tMessage, tRecip(1), tFiles, MAPI_LOGON_UI, 0&)
    If iStatus <> SUCCESS_SUCCESS Then
        MsgBox "Problem sending message"
        Exit Sub
    End If

    'logoff of mail, release our session
    iStatus = MAPILogoff(gMAPISession, 0, 0, 0)

End Sub

You may recall that you can establish custom message classes for messages. In this example, the message is sent out as a standard message, so we leave the message type defaulted to blank, requiring MAPI to specify it. If you want or need to set up a new message class, you would do so prior to calling the MAPISendMail function.

You can see from the subroutine that establishing the different aspects of the message is pretty straight-forward. You simply set up the different properties and then pass them to the API call as parameters.