Sending Mail

After logging on to MAPI with the MAPILogon function, messages can be created and sent. The process is as follows:

  1. Declare a variable of type MAPIMessage and set the values of its members.

  2. Declare an array of MAPIRecip and call MAPIResolveName on each element.

  3. If attachments are desired, declare an array of MapiFile and set each element appropriately. Sending attachments is covered later in this chapter.

  4. Call MAPISendMail to send the message.

Example 3-1 shows how to send a message, assuming the MAPILogon function has already been called successfully.

Example 3-1. Sending a Message

Dim nRetVal As Long
Dim MyMessage As MAPIMessage
Dim MyRecips( ) As MapiRecip
Dim MyFiles( ) As MapiFile
   
' Set the subject and body text.
MyMessage.Subject = "Test message from Simple MAPI."
MyMessage.NoteText = "This is the body text of the message."

' Add a recipient.
MyMessage.RecipCount = 1
ReDim MyRecips(1 To MyMessage.RecipCount) As MapiRecip
nRetVal = MAPIResolveName(nMAPISession, 0, "Annemarie", 0, 0, MyRecips(1))

' Send the message.
nRetVal = MAPISendMail(nMAPISession, 0, MyMessage, MyRecips, MyFiles, 0, 0)

The MAPIMessage datatype is defined in mapi32.txt like this:

Type MAPIMessage
    Reserved As Long
    Subject As String
    NoteText As String
    MessageType As String
    DateReceived As String
    ConversationID As String
    Flags As Long
    RecipCount As Long
    FileCount As Long
End Type

The member elements of this datatype are:

Reserved

This member is not used and must be 0.

Subject

The subject of the message. ...

Get CDO & MAPI Programming with Visual Basic: now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.