📄 ch11.htm
字号:
<html>
<head>
<title>Chapter 11 -- Creating a MAPI Email Agent</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 11</font></h1>
<h1><b><font SIZE="5" COLOR="#FF0000">Creating a MAPI Email Agent</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="#DesigningtheEmailAgent">Designing the Email Agent</a> <ul>
<li><a HREF="#FeaturesoftheMAPIEmailAgent">Features of the MAPI Email Agent</a> </li>
<li><a HREF="#StoringtheRulesinaControlFile">Storing the Rules in a Control File</a> </li>
</ul>
</li>
<li><a HREF="#CodingtheMAPIEmailAgentForms">Coding the MAPI Email Agent Forms</a> <ul>
<li><a HREF="#TheMainForm">The Main Form</a> </li>
<li><a HREF="#TheAddRuleForm">The Add Rule Form</a> </li>
<li><a HREF="#TheSetupForm">The Setup Form</a> </li>
</ul>
</li>
<li><a HREF="#CodingtheSupportRoutines">Coding the Support Routines</a> <ul>
<li><a HREF="#TheInitializationRoutines">The Initialization Routines</a> </li>
<li><a HREF="#TheListHandlingRoutines">The List-Handling Routines</a> </li>
<li><a HREF="#TheMessageProcessingRoutines">The Message Processing Routines</a> </li>
</ul>
</li>
<li><a HREF="#InstallingandTestingtheMAPIEmailAg">Installing and Testing the MAPI Email
Agent</a> <ul>
<li><a HREF="#SettingUpYourEmailFolders">Setting Up Your Email Folders</a> </li>
<li><a HREF="#BuildingMAPIEmailAgentActionsTests">Building MAPI Email Agent Actions, Tests,
and Rules</a> </li>
<li><a HREF="#RunningtheMAPIEmailAgent">Running the MAPI Email Agent</a> </li>
</ul>
</li>
<li><a HREF="#Summary">Summary</a> </li>
</ul>
<hr>
<p><font COLOR="#000000">In this chapter you'll learn how to use the OLE Messaging library
to create a stand-alone e-mail agent. This agent can scan your incoming mail and, based on
rules you establish, automatically handle messages for you. All actions are based on rules
you establish in a control file.</font> </p>
<p>Features of the MAPI Email Agent are:
<ul>
<li><font COLOR="#000000">Timed scanning of your inbox.</font> </li>
<li><font COLOR="#000000">Notification of message arrivals based on level of importance.</font>
</li>
<li><font COLOR="#000000">Automatic routing of incoming messages to other e-mail users.</font>
</li>
<li><font COLOR="#000000">Automatic replies to messages based on subject or sender.</font> </li>
<li><font COLOR="#000000">Automatic sorting of unread messages into separate folders based
on subject, sender, or level of importance.</font> </li>
</ul>
<p>The first part of the chapter will discuss the concept of e-mail agents and cover the
overall design of the program. The next sections will detail the coding of the forms and
support routines needed to complete the project. In the final section, you'll install and
test the MAPI Email Agent program. </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>The Microsoft Exchange Server clients allow users to establish and code their own mail
agent (called the <i>Inbox Assistant</i>). If you have the Microsoft Exchange Server
client, now is a good time to review the Inbox <tt><font FACE="Courier">Assistant</font></tt>
to get an idea of its features. The programming project covered in this chapter works
independently of the <tt><font FACE="Courier">Inbox Assistant</font></tt> and does not
require that users have Microsoft Exchange Server installed on their system. </p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>When you complete the programming project in this chapter, you'll have a fully
functional MAPI Email Agent that can be installed on any workstation that has access to a
MAPI-compliant e-mail system. Also, the techniques used to build this project can be
incorporated into other Windows programming projects to add message processing
capabilities. </p>
<h2><a NAME="DesigningtheEmailAgent"><b><font SIZE="5" COLOR="#FF0000">Designing the Email
Agent</font></b></a></h2>
<p>Before starting to code the project, it's a good idea to discuss the general features
and functions of an e-mail agent. Once you have a good idea of what e-mail agents can do,
then you can lay out the basic design features of the MAPI Email Agent programming project
covered in this chapter. </p>
<p>Basically, an e-mail agent is a program that "acts for you." It is a program
that reviews your messages and, based on information you have supplied, processes the
messages for you. Typically, e-mail agents process messages in the user's inbox. Users can
set up rules that tell the agent how to handle each new message. These rules can tell the
e-mail agent to check various parts of the incoming message and then take a specific
action. </p>
<p>For example, the agent can be instructed to look for all messages that are received
from your boss and then place those messages in a special folder called
"Urgent." Or the agent could be told that any message with the word
"SALES" in the subject line should be immediately forwarded to another user's
inbox and then erased from your inbox without any comment. You might also tell the agent
to automatically reply to all senders that you are out on vacation and will return next
week. </p>
<h3><a NAME="FeaturesoftheMAPIEmailAgent"><b>Features of the MAPI Email Agent</b></a></h3>
<p>For this chapter, you'll use Visual Basic 4.0 and the OLE Messaging library to build a
stand-alone MAPI Email Agent that has the following features:
<ul>
<li><i>Timed scanning of your inbox</i>-Users can start the program and allow it to run
unattended. It scans the inbox for new messages every <i>N</i> minutes. Users can also set
configuration values that start the program as an icon on the task bar or as a dialog box.
</li>
<li><i>Message notification</i>-Users can establish a rule that causes a dialog box to pop
up each time a specific message is received. This notification can be based on sender ID,
subject content, or level of importance. </li>
<li><i>Automatic forwarding</i>-Users can create rules that automatically forward messages
to other addresses. Users can also determine whether the original should be kept or
discarded. </li>
<li><i>Automatic replies</i>-Users can create rules that generate automated replies to
senders based on sender ID, subject content, or level of importance. </li>
<li><i>Automatic message copying or moving</i>-Users can create rules that copy or move
incoming messages to other folders in the user's message store. This feature can be used
to sort incoming message by sender ID, subject content, or level of importance. </li>
</ul>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>The features described here are just the start of what can be accomplished with an
e-mail agent. The number of options has been limited to keep this chapter focused on
design and coding issues instead of content. As you build your own agent, you can add many
other capabilities.</p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>To accomplish this, the MAPI Email Agent will keep track of rules created by the user.
These rules will have three parts: <i>tests</i>, <i>comparisons</i>, and <i>actions</i>.
The test portion of the rule performs a simple scan of the designated portion of the
message, searching for requested content. The MAPI Email Agent described in this chapter
is capable of inspecting three message parts:
<ul>
<li><i>SUBJECT</i>-Checks the <tt><font FACE="Courier">Subject</font></tt> property of the
message </li>
<li><i>SENDER</i>-Checks the <tt><font FACE="Courier">Sender.Name</font></tt> property of
the message </li>
<li><i>PRIORITY</i>-Checks the <tt><font FACE="Courier">Importance</font></tt> property of
the message </li>
</ul>
<p>For example, the test <tt><font FACE="Courier">SUBJECT MAPI</font></tt> tells the agent
to check the message subject for the word "MAPI." The phrase <tt><font
FACE="Courier">SENDER Boss</font></tt> tells the agent to check for messages sent to the
user from the e-mail ID "boss." </p>
<p>All tests must use a logical condition as part of the processing. The MAPI Email Agent
uses <i>comparisons</i> to do this. The program can check for the following four logical
conditions:
<ul>
<li><tt><font FACE="Courier">EQ</font></tt>-Equals (<tt><font FACE="Courier">SENDER Sharon
EQ</font></tt>) </li>
<li><tt><font FACE="Courier">GT</font></tt>-Greater Than (<tt><font FACE="Courier">PRIORITY
0 GT</font></tt>) </li>
<li><tt><font FACE="Courier">LT</font></tt>-Less Than (<tt><font FACE="Courier">PRIORITY 1
LT</font></tt>) </li>
<li><tt><font FACE="Courier">CI</font></tt>-Is Contained In (<tt><font FACE="Courier">SUBJECT
VB CI</font></tt>) </li>
</ul>
<p>You'll notice that the last value is able to check the selected message part for the
occurrence of a word or phrase. Note that all the comparisons are case-insensitive. It is
important to note that the LT and GT can be used with character data, too. </p>
<p>The last of the three portions of a rule is the <i>action</i>. This is the part of the
rule that tells the agent what action to take if the test criteria have been met. The MAPI
Email Agent can perform the following actions on a message:
<ul>
<li><tt><font FACE="Courier">MOVE</font></tt>-Move the message to another folder (<tt><font
FACE="Courier">MOVE Urgent</font></tt>) </li>
<li><tt><font FACE="Courier">COPY</font></tt>-Copy the message to another folder (<tt><font
FACE="Courier">COPY Archive</font></tt>) </li>
<li><tt><font FACE="Courier">FORWARD</font></tt>-Forward the message to another user (<tt><font
FACE="Courier">FORWARD mAmund@iac.net</font></tt>) </li>
<li><tt><font FACE="Courier">REPLY</font></tt>-Send reply text to the user (<tt><font
FACE="Courier">REPLY reply.txt</font></tt>) </li>
</ul>
<p>The agent allows users to determine whether the forwarded and reply messages are
retained or removed once the forward/reply is generated. </p>
<h3><a NAME="StoringtheRulesinaControlFile"><b>Storing the Rules in a Control File</b></a></h3>
<p>The MAPI Email Agent allows users to build tests and actions, and then use them to
create rules. All this information is stored in a text file similar to an INI file. This
file also contains general control information, such as the scan interval, whether the
agent should create a log file, the default log on profiles, and so on. Listing 11.1 shows
a sample rule file. </p>
<hr>
<blockquote>
<b><p>Listing 11.1. Sample rule file for the MAPI Email Agent.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>; ********************************************************
<br>
; MAPI Email Agent Control File<br>
; ******************************************************** <br>
;<br>
[General]<br>
Editor=notepad.exe<br>
ScanInterval=2<br>
LogFile=mea.log<br>
LogFlag=1<br>
RuleCount=3<br>
ActionCount=4<br>
TestCount=4<br>
Profile=MCA<br>
DeleteForwardFlag=0<br>
NotifyDialog=1<br>
DeleteReplyFlag=0<br>
MinimzeOnStart=0<br>
AutoStart=0<br>
LastUpdated=04/29/96 9:27:30 PM<br>
<br>
[Actions]<br>
Action0=MOVE MAPI<br>
Action1=MOVE Urgent<br>
Action2=FORWARD mamund@iac.net<br>
Action3=COPY SavedMail<br>
<br>
[Tests]<br>
Test0=SENDER MCA<br>
Test1=SENDER Boss<br>
Test2=SUBJECT SAPI<br>
Test3=SUBJECT MAPI<br>
<br>
[Rules]<br>
RuleName0=Boss's Mail<br>
RuleTest0=SENDER Boss<br>
RuleAction0=Move Urgent<br>
RuleCompare0=EQ<br>
RuleName1=Send To ISP<br>
RuleTest1=SENDER MCA<br>
RuleAction1=FORWARD mamund@iac.net<br>
RuleCompare1=EQ<br>
RuleName2=MAPI Mail<br>
RuleTest2=SUBJECT MAPI<br>
RuleAction2=MOVE MAPI<br>
RuleCompare2=CI</font></tt> </p>
</blockquote>
<hr>
<p>The next sections show you how to code the MAPI Email Agent forms and support routines
that will create and process the rules described here. </p>
<h2><a NAME="CodingtheMAPIEmailAgentForms"><b><font SIZE="5" COLOR="#FF0000">Coding the
MAPI Email Agent Forms</font></b></a></h2>
<p>You'll use Visual Basic 4.0 to create the three forms of the MAPI Email Agent. These
forms will allow you to start and stop message processing; add or delete new tests,
actions, and rules; and modify default configuration settings for the MAPI Email Agent. </p>
<p>The next three sections of this chapter outline the steps needed to layout and code
these three forms. If you haven't done so yet, start Visual Basic 4.0 now and create a new
project. </p>
<h3><a NAME="TheMainForm"><b>The Main Form</b></a></h3>
<p>The Main form of the MAPI Email Agent shows the current list of rules, tests, and
actions. You can also launch the message scan routine from this screen, access the setup
dialog box, and inspect the MAPI Email Agent log file. Figure 11.1 shows an example of the
Main form in run-time mode. </p>
<p><a HREF="f11-1.gif"><b>Figure 11.1 : </b><i>The MAPI Email Agent Main form.</i></a> </p>
<p>The Main form has a set of command button control arrays to handle the user selections.
The first control array covers the top row of buttons. These buttons handle the main
processing steps:
<ul>
<li><tt><font FACE="Courier">Start Timer</font></tt> starts the timer to count down to the
next message scan. </li>
<li><tt><font FACE="Courier">End Timer</font></tt> disables the timer. </li>
<li><tt><font FACE="Courier">Setup</font></tt> calls the configuration dialog box. </li>
<li><tt><font FACE="Courier">View Log</font></tt> displays the contents of the MAPI Email
Agent log file. </li>
<li><tt><font FACE="Courier">Refresh</font></tt> refreshes the list boxes from the control
file. </li>
<li><tt><font FACE="Courier">Exit</font></tt> ends the program. </li>
</ul>
<p>The second command button control array handles the adding and deleting of tests,
actions, and rules. To keep things simple for this project, the system is capable only of
adding or deleting rules. Existing rules cannot be edited and saved again. Also, only
basic input editing is performed by this program. In a production environment, this
program should be enhanced to add an improved user interface with additional input
checking and recovery. </p>
<p>Table 11.1 contains a list of all the controls used on the MAPI Email Agent main form
along with their property settings. Use this table along with Figure 11.1 to build the
MAPI Email Agent Main form.<br>
</p>
<p align="center"><b>Table 11.1. Controls for the MAPI Email Agent Main form.</b> </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><i>Control</i></td>
<td WIDTH="138"><i>Property</i> </td>
<td WIDTH="177"><i>Setting</i></td>
</tr>
<tr>
<td WIDTH="176"><tt><font FACE="Courier">VB.Form</font></tt> </td>
<td WIDTH="138"><tt><font FACE="Courier">Name</font></tt></td>
<td WIDTH="177">frmMEA</td>
</tr>
<tr>
<td WIDTH="176"><tt><font FACE="Courier"> </font></tt> </td>
<td WIDTH="138"><tt><font FACE="Courier">Caption</font></tt> </td>
<td WIDTH="177">"MAPI Email Agent"</td>
</tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -