Microsoft KB Archive/161833

From BetaArchive Wiki
Knowledge Base


Article ID: 161833

Article Last Modified on 10/1/2005



APPLIES TO

  • Microsoft Collaboration Data Objects 1.21
  • Microsoft Collaboration Data Objects 1.1
  • Microsoft Collaboration Data Objects 1.2
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition



This article was previously published under Q161833

INTRODUCTION

This article describes how to programmatically send an e-mail message from Visual Basic by using the Collaboration Data Objects (CDO 1.x) Libraries. This example assumes that the CDO 1.x was correctly installed on your computer. For more information, click the following article number to view the article in the Microsoft Knowledge Base:

171440 Where to acquire the CDO Libraries (all versions)


The sample code can also be used from Visual Basic for Applications (VBA), which allows Access, Excel, or Project to send e-mail messages through the CDO 1.x Library.

MORE INFORMATION

With the CDO 1.x library, you can quickly and easily add e-mail capabilities to your Visual Basic application. These capabilities include sending and receiving e-mail messages and interacting with folders and address books. 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 CDO 1.x library are much fuller than the MAPI controls that ship with Visual Basic. As a result, you will have access to many MAPI properties that the MSMAPI OCX controls simply do not address.

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 object 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
           'Type can be ActMsgTo, mapiTo, or CdoTo for different CDO versions;
           'they all have a constant value of 1.
           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.


Keywords: kbhowto kbprogramming KB161833