Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #90
Online Version at http://www.codeoftheweek.com/membersonly/bi/0090.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp. Word, Excel and Visual Basic are trademarks of Microsoft Corp.

Consulting Services

We are offering our consulting services for all types of projects related to Visual Basic, Outlook, Exchange Server and other Microsoft environments. If you are looking for a high-quality company to provide outstanding consulting services to your organization, email us at consult@codeoftheweek.com

Advertising

If you are interested in advertising in COTW (our rates are VERY reasonable), please email us at sponsor@codeoftheweek.com


Requirements for this Issue

The source code in this issue is designed for Visual Basic 4.0 and higher.

In this Issue

This issue enhances the class from issue #21. It discusses using a class to send a simple email message directly from Visual Basic using the MAPISession and MAPIMessages controls that ship with Visual Basic. This issue adds the ability to send attachments using the MAPI controls.

MAPI Class

You will need to add the MAPI custom control to your project to use this class. In Visual Basic 5.0 you would perform the following steps:

This class requires that you drop a MAPISession and MAPIMessages control onto your form after you add the control to your project.

Properties

Public Enum eRecipType

List of enumerated types that are used with the AddRecipient method.

Public Property Set MAPISessionControl(ctl As Control)

Assign this property the MAPISession control that you dropped onto your form.

Public Property Set MAPIMessageControl(ctl As Control)

Assign this property the MAPIMessage control that you dropped onto your form.

Methods

Public Sub ComposeMessage(sSubject As String, sMsg As String)

Starts the message creation process. sSubject will be the Subject of the message and sMsg will be the message body.

Public Sub AddRecipient(sName As String, eType As eRecipType)

Allows you to add multiple receipients to send the message composed by ComposeMessage. sName is the email address and eType is either RecipTypeTo or RecipTypeCC, which corresponds to the To: field and the CC: field in an email message.

Public Sub AddAttachment(sAttachmentName As String, sFilename As String)

Allows you to add any number of attachments to the message. The only limitation seems to be that the filenames must be different (at least with the Outlook 98 client).

Public Sub SendMessage()

Sends the message composed with the above three methods.

Notes

There are certain assumptions taken in this class. For example, it automatically signs you onto the email system (We tested with Eudora Pro and Outlook 98) when you assign the MAPISessionControl property.

Sample Usage

This is a simple example that will send an email message to our info address at info@codeoftheweek.com with a CC to coolstuff@codeoftheweek.com. It assumes the MAPISession control on your form is called MAPISess and the MAPIMessages control is called MAPIMsg. It will also append three attached data files.

        Dim MAPI As New cMAPI

        Set MAPI.MAPISessionControl = MAPISess
        Set MAPI.MAPIMessageControl = MAPIMsg

        MAPI.ComposeMessage "Order for Code of the Week", _
                   "Please send me Code of the Week as soon as you can."
        MAPI.AddRecipient "info@codeoftheweek.com", RecipTypeTo
        MAPI.AddRecipient "coolstuff@codeoftheweek.com", RecipTypeCC
        MAPI.AddAttachment "Credit Card Info.txt", "c:\visa.txt"
        MAPI.AddAttachment "Credit Card Info2.txt", "c:\visa2.txt"
        MAPI.AddAttachment "Credit Card Info3.txt", "c:\visa3.txt"
        MAPI.SendMessage

        Set MAPI = Nothing

Source Code

Just paste this code into any class and change the name of the class to cMAPI.

'----------------------------------------------------------------------
'
'   Class Name:     cMAPI
'   Written By:     C&D Programming Corp.
'   Create Date:    1/12/98
'   Updated:        4/99
'   Copyright:      Copyright 1997-99 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'
'
'----------------------------------------------------------------------
Option Explicit


'
'   To send a message you do something like this:
'
'   Set MAPI.MAPISessionControl = MAPISess1
'   Set MAPI.MAPIMessageControl = MAPIMsg1
'   MAPI.ComposeMessage "Subject", "Message"
'   MAPI.AddRecipient "joe@codeoftheweek.com", RecipTypeTo
'   MAPI.SendMessage
'
Public Enum eRecipType
    RecipTypeTo = 1
    RecipTypeCC = 2
End Enum

Public MAPIMsg As MAPIMessages
Public MAPISess As MAPISession

'
'   Signing onto this way will force a synchronization of messages with the
'   server in you are running in remote mode.  You can modify the DownloadMail
'   property to modify this behavior.
'
Public Property Set MAPISessionControl(ctl As Control)
    Set MAPISess = ctl '
    MAPISess.SignOn
End Property

Public Property Set MAPIMessageControl(ctl As Control)
    Set MAPIMsg = ctl
    MAPIMsg.SessionID = MAPISess.SessionID
End Property

Public Sub AddRecipient(sName As String, eType As eRecipType)
    MAPIMsg.RecipIndex = MAPIMsg.RecipCount
    MAPIMsg.RecipType = eType
    MAPIMsg.RecipDisplayName = sName
    MAPIMsg.ResolveName
End Sub

Public Sub SendMessage()
    MAPIMsg.Send False
End Sub

Public Sub ComposeMessage(sSubject As String, sMsg As String)
    MAPIMsg.Compose
    MAPIMsg.MsgSubject = sSubject
    MAPIMsg.MsgNoteText = sMsg
End Sub

Private Sub Class_Terminate()
    If Not MAPISess Is Nothing Then
        MAPISess.SignOff
    End If
End Sub

Public Sub AddAttachment(sAttachmentName As String, sFilename As String)
    If MAPIMsg.AttachmentCount = 0 Then
        MAPIMsg.AttachmentIndex = 0
    Else
        MAPIMsg.AttachmentIndex = MAPIMsg.AttachmentCount
    End If

    ' Other Attachment Type available are:
    '   mapData 0   The attachment is a data file.
    '   mapEOLE 1   The attachment is an embedded OLE object.
    '   mapSOLE 2   The attachment is a static OLE object.

    MAPIMsg.AttachmentType = mapData
    MAPIMsg.AttachmentName = sAttachmentName
    MAPIMsg.AttachmentPathName = sFilename

    ' Make room for the attachment.
    MAPIMsg.MsgNoteText = MAPIMsg.MsgNoteText & " "
    MAPIMsg.AttachmentPosition = Len(MAPIMsg.MsgNoteText) - 1
End Sub

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com