HOWTO: Send Mail from Visual Basic Using OLE Messaging

Last reviewed: November 21, 1997
Article ID: Q161833

The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SUMMARY

This article outlines the steps necessary to programmatically send an e-mail message from Visual Basic using the OLE Messaging Library. This example assumes that the OLE Messaging Library was properly installed on your computer (typically, by installing Exchange).

The sample code can also be used from VBA, allowing Access, Excel, or Project to send e-mail via the OLE Messaging Library.

MORE INFORMATION

With the OLE Messaging Library, you can quickly and easily add e-mail capabilities (that is, sending and receiving e-mail messages, and interacting with folders and address books) to your Visual Basic application. You can create programmable messaging objects, and then use their properties and methods to meet the needs of your application.

The objects and methods exposed by the OLE Messaging Library are much more robust than the MAPI controls that ship with Visual Basic, thus allowing access to many MAPI properties that the MSMAPI OCX controls simply do not address.

"Microsoft OLE Messaging Library Programmers Reference" is available in the Microsoft Developer Network (MSDN), or the Olemsg.hlp file that installs with Exchange Forms Designer 4.0.

NOTE: If this object library is not available in the References list, click the Browse button and search your Windows\System folder for the file Mdisp32.tlb.

Sample Code

To build a Visual Basic project that sends a simple message, follow these steps:

  1. Open a new project in Visual Basic.

  2. On the Tools menu, click References. Select "OLE/Messaging 1.0 Object Library."

  3. Add a CommandButton to the default form. Accept the default name, Command1.

  4. Copy the following code into the General Declarations section of the default form:

         Option Explicit
    

         Private Sub Command1_Click()
           Dim objSession As Object
           Dim objMessage As Object
           Dim objRecipient As Object
    

           'Create the Session Object
           Set objSession = CreateObject("mapi.session")
    

           'Logon using the session object
           'Specify a valid profile name if you want to
           'Avoid the logon dialog box
           objSession.Logon profileName:="MS Exchange Settings"
    

           'Add a new message object to the OutBox
           Set objMessage = objSession.Outbox.Messages.Add
    

           'Set the properties of the message object
           objMessage.subject = "This is a test."
           objMessage.Text = "This is the message text."
    

           'Add a recipient object to the objMessage.Recipients collection
           Set objRecipient = objMessage.Recipients.Add
    

           'Set the properties of the recipient object
           objRecipient.Name = "John Doe"  '<---Replace this with a valid
    
                                           'display name or e-mail alias
           objRecipient.Type = mapiTo
           objRecipient.Resolve
    
           'Send the message
           objMessage.Send showDialog:=False
           MsgBox "Message sent successfully!"
    
           'Logoff using the session object
           objSession.Logoff
         End Sub
    
    

  5. Run the project. Click Command1. You have sent e-mail from Visual Basic.

REFERENCES

Microsoft OLE Messaging Library Programmers Reference

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)
Keywords          : kbprg ActMsg OLEMSG vbwin
Technology        : kbole kbvba
Version           : WINDOWS:4.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


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 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.