|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
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 BasicNow that weve seen how to send email (see the previous topic), how do you read email? You set the MAPISession controls DownLoadMail property to True. Lets see an example. In this case, well download any waiting email into the users Inbox and then display the first message in a text box. Well use the program we started in the previous topic and add the code we need to the Read Email buttons event handler. First, we set the MAPISession controls DownLoadMail property to True, then we use that controls 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 controls 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 controls SessionID property to the MAPISession controls 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 controls 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 buttonnote 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 thats itwe can now receive email, as you see in Figure 21.13. Now were sending and receiving email with Visual Basic.
The code for this example is located in the email folder on this books accompanying CD-ROM.
|
|
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. |