PC MAPI: Sample VB Code to Read a Microsoft Mail Message

Last reviewed: November 27, 1995
Article ID: Q140106
The information in this article applies to:
  • Microsoft Mail for PC Networks, versions 3.0 and 3.2

SUMMARY

The simple Messaging Application Programming Interface (MAPI) includes functions that allow developers to access mail programmatically. The following Visual Basic code illustrates successful calls to these functions.

MORE INFORMATION

The sample code below uses MAPILogon to log in the Mail system, MAPIFindNext to find the first message ID, loops MAPIReadmail and MAPIFindNext to access all messages, and uses MAPILogOff to log out.

' **************************************************
'        Simple MAPI Declarations
' **************************************************
Dim M As MAPIMessage Dim Mo As MapiRecip ReDim Mr(0 To 0) As mapirecip ReDim mf(0 To 0) As mapifile
MsgType$ = ""      ' NULL default to IPM.Microsoft Mail.Note
m.MessageType = MsgType$   ' message type you want to download
NL = Chr(10)         ' a new line constant variable
' **********************************************************
' Login and start the MAPI Session, using the dialog box and
' download new mail if any exists
' **********************************************************
rc& = MAPILogon(main_form.hWnd, "", "", MAPI_LOGON_UI + MAPI_FORCE_DOWNLOAD,

 0&, lhsession)
If rc& <> SUCCESS_SUCCESS Then
  MsgBox "Error logging in with error=" + Str(rc&)

Else

  MsgBox "Success logging in"
End If rc& = MAPIFindNext(lhsession&, 0&, MsgType$, "", 0&, 0&, msgid$)
  If rc& <> SUCCESS_SUCCESS Then
    MsgBox "Error during message find or no messages in Inbox."
    rc& = MAPILogoff(lhsession&, 0&, 0&, 0&)
    End
  End If
  i = 0
  Do While (rc& = SUCCESS_SUCCESS)
        rc& = MAPIReadMail(lhsession&, 0&, msgid$, 0&, 0&, m, mo, Mr(),
mf())
        If rc& <> SUCCESS_SUCCESS Then
            MsgBox "Error during MAPIReadMail"
            Exit Do
        Else
            i = i + 1
   ' ************** accessing various fields of the message ************
            msg$ = "Message# " & Str(i)
            msg$ = msg$ & NL & "Subject: " & m.subject
            msg$ = msg$ & NL & "Sent by: " & mo.name
            msg$ = msg$ & NL & "Message type: " & m.MessageType
            If m.filecount <> 0 Then
                msg$ = msg$ & NL & "Message includes attachment"
            Else
                msg$ = msg$ & NL & "Message includes no attachment"
            End If
        End If
        MsgBox msg$
        rc& = MAPIFindNext(lhsession&, 0&, MsgType$, msgid$, 0&, 0&,
msgid$)

   Loop

rc& = MAPILogoff(lhsession, 0&, 0&, 0&) If rc& <> SUCCESS_SUCCESS Then MsgBox "Error logging off with error=" + Str(rc&) Else MsgBox "Success logging off" End If

IMPORTANT NOTE: Make sure a MAPI declaration module, such as MAPILIB.BAS, is included in the project.


Additional reference words: 3.00 3.20 1.00 visual basic code
KBCategory: kbtool kbcode
KBSubcategory: MailPCMAPI


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 27, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.