📄 ch7.htm
字号:
<html>
<head>
<title>Chapter 7 -- Creating a Simple MAPI Client with the MAPI Controls</title>
<meta NAME="GENERATOR" CONTENT="Microsoft FrontPage 3.0">
</head>
<body TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<!-- Spidersoft WebZIP Ad Banner Insert -->
<!-- End of Spidersoft WebZIP Ad Banner Insert-->
<h1><font COLOR="#FF0000">Chapter 7</font></h1>
<h1><b><font SIZE="5" COLOR="#FF0000">Creating a Simple MAPI Client with the MAPI Controls</font></b>
</h1>
<hr WIDTH="100%">
<h3 ALIGN="CENTER"><font SIZE="+2" COLOR="#000000">CONTENTS<a NAME="CONTENTS"></a> </font></h3>
<ul>
<li><a HREF="#TheVisualBasicMAPIControls">The Visual Basic MAPI Controls</a> <ul>
<li><a HREF="#TheMAPISessionControl">The MAPISession Control</a> </li>
<li><a HREF="#TheMAPIMessageControl">The MAPIMessage Control</a> </li>
</ul>
</li>
<li><a HREF="#BuildingaSimpleMAPIClientApplicatio">Building a Simple MAPI Client Application</a>
<ul>
<li><a HREF="#LayingOuttheForms">Laying Out the Forms</a> </li>
<li><a HREF="#CodingtheMainSupportRoutines">Coding the Main Support Routines</a><b> </b></li>
<li><a HREF="#CodingtheMAPIMAINFRMEvents">Coding the MAPIMAIN.FRM Events</a> </li>
<li><a HREF="#CodingtheMainButtonBar">Coding the Main Button Bar</a> </li>
<li><a HREF="#CodingtheReaderForm">Coding the Reader Form</a> </li>
<li><a HREF="#RunningtheSimpleMailClient">Running the Simple Mail Client</a> </li>
<li><a HREF="#AdditionalFeatures">Additional Features</a> </li>
</ul>
</li>
<li><a HREF="#Summary">Summary</a> </li>
</ul>
<hr>
<p>In this chapter, you'll learn how to use the MAPI controls from Visual Basic to create
a simple program that can read and reply to all e-mail sent to the logon ID. You'll also
learn how to write routines to access all the electronic mail services available to a
basic MAPI client application on a desktop workstation. This includes creating new e-mail
messages, checking the inbox for new mail, and accessing and updating the e-mail address
book. When you complete this chapter, you will have a fully functional e-mail client
application. </p>
<p>You'll also learn the details of using the <tt><font FACE="Courier">MAPISession</font></tt>
and <tt><font FACE="Courier">MAPIMessage</font></tt> controls with Visual Basic 4.0. The
Visual Basic MAPI controls offer a quick way to build Simple MAPI applications. The MAPI
controls allow you full access to the <tt><font FACE="Courier">Inbox</font></tt> object,
to the MAPI <tt><font FACE="Courier">Compose</font></tt>, <tt><font FACE="Courier">Fetch</font></tt>,
<tt><font FACE="Courier">Read</font></tt>, <tt><font FACE="Courier">Delete</font></tt>,
and <tt><font FACE="Courier">Send</font></tt> services, and limited access to the address
book. </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>Simple MAPI is sometimes called MAPI 0 to indicate that it precedes the MAPI 1.0
release that matches Microsoft Exchange Server. Throughout this book, you'll see Simple
MAPI instead of MAPI 0.</p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>When you complete the coding examples in this chapter, you will understand the basics
of Simple MAPI, and you'll know how to use Visual Basic and the MAPI controls to read,
compose, address, send, and delete MAPI messages. </p>
<h2><a NAME="TheVisualBasicMAPIControls"><font SIZE="5" COLOR="#FF0000">The Visual Basic
MAPI Controls</font></a><br>
</h2>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>Throughout the rest of this chapter, there are sample programs that illustrate the use
of the MAPI controls to create mail-aware applications with Visual Basic 4.0. These
programs must be run on a workstation that has access to simple MAPI services through a
MAPI-compliant mail server. MAPI-compatible servers from Microsoft include the Microsoft
Mail Server for DOS, Microsoft Exchange Server for NT, Microsoft Mail for Windows for
Workgroups, Windows 95 Exchange Mail, or any other fully MAPI-compliant system (such as
WordPerfect Office).</p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>Visual Basic 4.0 Professional Edition ships with two OCX controls that provide access
to all the MAPI services you'll need to create fully functional electronic mail
applications using Visual Basic. The <tt><font FACE="Courier">MAPISession</font></tt>
control provides access to everything you'll need to sign on and sign off any
MAPI-compliant server. The <tt><font FACE="Courier">MAPIMessage</font></tt> control
provides access to the MAPI routines that allow you to read, compose, address, and send
messages using the session established with the <tt><font FACE="Courier">MAPISession</font></tt>
control. This section of the chapter will review the MAPI-related properties and methods
of each of these controls. </p>
<h3><a NAME="TheMAPISessionControl">The <tt><font SIZE="4" FACE="Courier">MAPISession</font></tt><font
SIZE="4"> Control</font></a></h3>
<p>The <tt><font FACE="Courier">MAPISession</font></tt> control is used to establish a
link between your Visual Basic program (the mail client) and the electronic mail server.
The MAPI-compliant mail server must be installed and available to the Visual Basic client
program. If your program will run on a traditional network server, the mail server may be
Microsoft Mail installed on a network workstation. If you are running Windows for
Workgroups or Windows 95, Microsoft Mail or Microsoft Exchange can act as a mail server.
There are other mail servers available for both file server and peer-to-peer networks. </p>
<p>There are two methods and seven properties for the <tt><font FACE="Courier">MAPISession</font></tt>
control that are directly MAPI related. The two following sections identify these
components of the <tt><font FACE="Courier">MAPISession</font></tt> control, and describe
their meaning and their use in Visual Basic programs. </p>
<h4>Methods of the <tt><font FACE="Courier">MAPISession</font></tt> Control</h4>
<p>There are only two <tt><font FACE="Courier">MAPISession</font></tt> methods: <tt><font
FACE="Courier">SignOn</font></tt> and <tt><font FACE="Courier">SignOff</font></tt>. The <tt><font
FACE="Courier">SignOn</font></tt> method is used to begin a MAPI session. By default, the <tt><font
FACE="Courier">SignOn</font></tt> method provides a logon dialog box that prompts the user
for valid logon information. The exact nature of the logon dialog box depends on the mail
server. The Microsoft Mail logon dialog box prompts the user for a valid username and
password (see Figure 7.1). </p>
<p><a HREF="f7-1.gif"><b>Figure 7.1 : </b><i>Default logon dialog box for Microsoft Mail.</i></a>
</p>
<p>The default logon dialog box for the Microsoft Exchange Mail product simply asks the
user to select the desired mail services profile (see Figure 7.2). </p>
<p><a HREF="f7-2.gif"><b>Figure 7.2 : </b><i>Default logon dialog box for Microsoft
Exchange Mail.</i></a> </p>
<p>If a valid username and password are supplied by way of the <tt><font FACE="Courier">SignOn</font></tt>
method, the <tt><font FACE="Courier">MAPISession</font></tt> control returns a unique
value in the <tt><font FACE="Courier">SessionID</font></tt> property. This unique value is
used in all message transactions to identify the link between the client application and
the mail server. We'll talk more about the <tt><font FACE="Courier">SessionID</font></tt>
property in the next section of this chapter. </p>
<p>The <tt><font FACE="Courier">SignOff</font></tt> method of the <tt><font FACE="Courier">MAPISession</font></tt>
control does just what you'd expect-it safely ends your link to the mail server. There is
no dialog box associated with the <tt><font FACE="Courier">SignOff</font></tt> method. </p>
<p>In order to use the <tt><font FACE="Courier">MAPISession</font></tt> control methods,
you must first place the <tt><font FACE="Courier">MAPISession</font></tt> control on a
Visual Basic form. The form is invisible at run-time and is only used to provide the
methods and properties needed to establish a MAPI session between your Visual Basic
program and the mail server. </p>
<p>As an example of the <tt><font FACE="Courier">MAPISession</font></tt> control, start a
Visual Basic project. Use the <tt><font FACE="Courier">Tools | Custom Controls</font></tt>
menu item to add the Microsoft MAPI controls to your project. Place a <tt><font
FACE="Courier">MAPISession</font></tt><font FACE="AGaramond Bold"> </font>control on the
form and add the three lines of code in Listing 7.1 to the <tt><font FACE="Courier">Form_Load</font></tt>
event. </p>
<hr>
<blockquote>
<b><p>Listing 7.1. A tiny e-mail client.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Private Sub Form_Load()<br>
'<br>
MAPISession1.SignOn<br>
MAPISession1.SignOff<br>
End<br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<p>Save the form as <tt><font FACE="Courier">CDG0701.FRM</font></tt> and the project as <tt><font
FACE="Courier">CDG0701.VBP</font></tt>, and run the project. You'll see the default logon
dialog box provided by your mail server. Once you sign in, the Visual Basic program
immediately signs you out and ends. You'll add more features as we go along. </p>
<h4>Properties of the <tt><font FACE="Courier">MAPISession</font></tt> Control</h4>
<p>The <tt><font FACE="Courier">MAPISession</font></tt> control has seven MAPI-related
properties. These properties all deal with options that you can set to control the
behavior of the control at logon time. </p>
<p>The <tt><font FACE="Courier">Action</font></tt> property can be used to invoke the
sign-on or sign-off methods. This property was present in Visual Basic 3.0 and has been
replaced by the <tt><font FACE="Courier">SignOn</font></tt> and <tt><font FACE="Courier">SignOff</font></tt>
methods discussed earlier. </p>
<p>The <tt><font FACE="Courier">DownloadMail</font></tt> property is used to control
whether new messages are downloaded from the mail server at logon time. By default, all
new messages are forced into the user's mailbox at logon time. You can set this property
to <tt><font FACE="Courier">False</font></tt> to prevent this from happening. In the code
example in Listing 7.2, the <tt><font FACE="Courier">DownloadMail</font></tt> property has
be set to <tt><font FACE="Courier">False</font></tt> before invoking the <tt><font
FACE="Courier">SignOn</font></tt> method. </p>
<hr>
<blockquote>
<b><p>Listing 7.2. Setting the <tt><font FACE="Courier">DownloadMail</font></tt> property
of the <tt><font FACE="Courier">MAPISession</font></tt> control.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Private Sub Form_Load()<br>
'<br>
MAPISession1.DownloadMail = False ' don't get new mail<br>
MAPISession1.SignOn<br>
MAPISession1.SignOff<br>
End<br>
'<br>
End Sub</font></tt> </p>
</blockquote>
<hr>
<p>The <tt><font FACE="Courier">LogonUI</font></tt> property can be used to turn off the
default logon screen provided by the mail server. You can set this property to <tt><font
FACE="Courier">False</font></tt> to suppress the mail server from presenting the logon
screen when you invoke the <tt><font FACE="Courier">SignOn</font></tt> method. If you set
this property to <tt><font FACE="Courier">False</font></tt>, you must supply valid data in
the <tt><font FACE="Courier">UserName</font></tt><font FACE="AGaramond Bold"> </font>and <tt><font
FACE="Courier">Password</font></tt> properties, or an error will be reported. </p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -