Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Listing 21.3 email.frm version 1

VERSION 6.00
Object = "{20C62CAE-15DA-101B-B9A8-444553540000}#1.1#0"; "MSMAPI32.OCX"
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3405
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   5970
   LinkTopic       =   "Form1"
   ScaleHeight     =   3405
   ScaleWidth      =   5970
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox Text1
      Height          =   2175
      Left            =   240
      MultiLine       =   -1  'True
      ScrollBars      =   3  'Both
      TabIndex        =   2
      Top             =   120
      Width           =   5415
   End
   Begin VB.CommandButton Command2
      Caption         =   "Read email"
      Height          =   495
      Left            =   360
      TabIndex        =   1
      Top             =   2520
      Width           =   1215
   End
   Begin MSMAPI.MAPISession MAPISession1
      Left            =   1440
      Top             =   1920
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DownloadMail    =   -1  'True
      LogonUI         =   -1  'True
      NewSession      =   0   'False
   End
   Begin MSMAPI.MAPIMessages MAPIMessages1
      Left            =   2640
      Top             =   1920
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      AddressEditFieldCount=   1
      AddressModifiable=   0   'False
      AddressResolveUI=   0   'False
      FetchSorted     =   0   'False
      FetchUnreadOnly =   0   'False
   End
   Begin VB.CommandButton Command1
      Caption         =   "Send email"
      Height          =   495
      Left            =   4320
      TabIndex        =   0
      Top             =   2520
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()

    MAPISession1.DownLoadMail = False
    MAPISession1.SignOn

    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.MsgIndex = -1
    MAPIMessages1.Compose
    MAPIMessages1.Send True

    MAPISession1.SignOff
End Sub
Private Sub Command2_Click()
    MAPISession1.DownLoadMail = True
    MAPISession1.SignOn

    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.Fetch

    MAPIMessages1.MsgIndex = 0
    Text1.Text = MAPIMessages1.MsgNoteText

    MAPISession1.SignOff

End Sub

Reading Email In Visual Basic

Now that we’ve seen how to send email (see the previous topic), how do you read email? You set the MAPISession control’s DownLoadMail property to True.

Let’s see an example. In this case, we’ll download any waiting email into the user’s Inbox and then display the first message in a text box. We’ll use the program we started in the previous topic and add the code we need to the Read Email button’s event handler. First, we set the MAPISession control’s DownLoadMail property to True, then we use that control’s SignOn method to start the MAPI session and download any waiting email into the Inbox:

Private Sub Command2_Click()
    MAPISession1.DownLoadMail = True
    MAPISession1.SignOn
...

Now that the email is in the Inbox, how do we reach it? We use the MAPIMessages control’s Fetch method to create a message set (you can find out how many messages are in the set with the MsgCount property). To do that, we first set the MAPIMessages control’s SessionID property to the MAPISession control’s SessionID property and then use Fetch:

Private Sub Command2_Click()
    MAPISession1.DownLoadMail = True
    MAPISession1.SignOn

    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.Fetch
...

Next, we display the text of the first email message now in the Inbox by setting the MAPIMessages control’s MsgIndex to 0 and using the MsgNoteText property. (Note that in a real email program, you should check to make sure there really are messages waiting here, but in this case we assume there are because we just sent one using the Send Email button—note that if your system takes significant time to deliver email messages, you might have to alter this code.) Finally we sign off the MAPI session:

Private Sub Command2_Click()
    MAPISession1.DownLoadMail = True
    MAPISession1.SignOn

    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.Fetch

    MAPIMessages1.MsgIndex = 0
    Text1.Text = MAPIMessages1.MsgNoteText

    MAPISession1.SignOff

End Sub

And that’s it—we can now receive email, as you see in Figure 21.13. Now we’re sending and receiving email with Visual Basic.


Figure 21.13  Receiving email.

The code for this example is located in the email folder on this book’s accompanying CD-ROM.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.