HOWTO: Send Mail from Visual Basic Using OLE Messaging

ID: Q161833


The information in this article applies to:
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 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, choose References and select the Microsoft CDO 1.21 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:

Q176916 INFO: Active Messaging and Collaboration Data Objects (CDO)

Additional query words:

Keywords : kbprg kbActMsg kbCDO kbVBp400 kbGrpMsg kbGrpVBDB OleMsg
Version : WINDOWS:4.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: January 5, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.