Skip to Content
CDO & MAPI Programming with Visual Basic:
book

CDO & MAPI Programming with Visual Basic:

by Dave Grundgeiger
October 2000
Intermediate to advanced
384 pages
10h 22m
English
O'Reilly Media, Inc.
Content preview from CDO & MAPI Programming with Visual Basic:

Sending Email

After a MAPI session has been established, email messages can be sent and received. Example B-2 shows a subroutine that sends an email.

Example B-2. Sending Email

Private Sub SendMessage( )

   Dim OutlookFolder As Outlook.MAPIFolder
   Dim OutlookMailItem As Outlook.MailItem
   Dim OutlookRecipient As Outlook.Recipient
   
   ' Run Outlook and establish a MAPI session.
   ConnectToOutlook
   
   ' Get a reference to the Outbox. (Messages must be created in
   ' some folder.)
   Set OutlookFolder = _
      gOutlookApplication.Session.GetDefaultFolder(olFolderOutbox)
   
   ' Create a new mail item.
   Set OutlookMailItem = OutlookFolder.Items.Add(olMailItem)
   OutlookMailItem.Subject = "This is the subject."
   OutlookMailItem.Body = "This is the body text."
   
   ' Add a recipient.
   Set OutlookRecipient = OutlookMailItem.Recipients.Add("dg")
   OutlookRecipient.Type = olTo
   
   ' Send the message.
   OutlookMailItem.Send
   
   ' Clean up.
   Set OutlookRecipient = Nothing
   Set OutlookMailItem = Nothing
   Set OutlookFolder = Nothing
   
   DisconnectFromOutlook

End Sub ' SendMessage

The SendMessage subroutine in Example B-2 performs the following steps:

  1. Calls the ConnectToOutlook subroutine (already shown in Example B-1) to create an Application object and store a reference to it in the global variable gOutlookApplication.

  2. Obtains a reference to the user’s Outbox folder. This is done by calling the NameSpace object’s GetDefaultFolder method. (The NameSpace object is referenced through the Application object’s Session property.)

    The GetDefaultFolder method ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Excel® 2007 VBA Programmer's Reference

Excel® 2007 VBA Programmer's Reference

John Green, Stephen Bullen, Rob Bovey, Michael Alexander
COM Programming with Microsoft® .NET

COM Programming with Microsoft® .NET

John Paul Mueller, Julian Templeman

Publisher Resources

ISBN: 156592665XSupplemental ContentCatalog PageErrata