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

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0090.html


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