|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Sending Email From Visual BasicNow that youve added the MAPISession and MAPIMessages control to your program (see the previous topic), how do you use them to send email? Lets see an example. Create a new standard EXE project, and add the MAPISession and MAPIMessages controls MAPISession1 and MAPIMessages1. Next add two command buttons, Command1 and Command2, with the captions Send email and Read email. Well enable Command1, the Send Email button, in this topic, and Command2, the Read Email button, in the next topic. In addition, well need some place to display the email weve read, so add a text box, Text1, to the form, setting its MultiLine property to True and its ScrollBars property to Both (3). When users click Command1, they want to send email, and we let them do so by using the MAPIMessages controls Compose and Send methods. Our first task, however, is to start a new MAPI session, and we do that with the MAPISession controls SignOn method, after indicating that we dont want to download email by setting its DownLoadMail property to False:
Private Sub Command1_Click()
MAPISession1.DownLoadMail = False
MAPISession1.SignOn
...
After signing on to the Microsoft Exchange email system, we set the MAPIMessages controls SessionID to the MAPISession controls SessionID property to initialize MAPIMessages1:
Private Sub Command1_Click()
MAPISession1.DownLoadMail = False
MAPISession1.SignOn
MAPIMessages1.SessionID = MAPISession1.SessionID
...
To compose a new email message, we have to set the MAPIMessages1 controls MsgIndex property to -1 and call its Compose method:
Private Sub Command1_Click()
MAPISession1.DownLoadMail = False
MAPISession1.SignOn
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.MsgIndex = -1
MAPIMessages1.Compose
...
This code displays the Compose dialog box, as shown in Figure 21.12. Users can enter the email text and address they want to use in that dialog box and click the Send button (the Send button displays an envelope in Figure 21.12) to send their email.
When the user is done composing the email, we send it with the MAPIMessages1 controls Send method and sign off the MAPI session using the MAPISession1 controls SignOff method:
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
Thats itweve sent our email. What actually happens is that the program sends the new email message to the users Outbox (which is also opened when you open the Inbox), and the Outbox is usually set to send email automatically. In fact, thats the way the Microsoft Exchange usually works: by logging into the mail server youve specified at regular intervals. When it logs in, it sends the mail waiting in the Outbox and reads any waiting email, placing it in the Inbox. (In fact, now that weve sent email, well see how to read that email in the next topic.) The code for this example, email.frm version 1 (version 2, which is located on this books accompanying CD-ROM, will let the user read email as well), appears in Listing 21.3.
|
|
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. |