⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch9.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 5 页
字号:
SIZE="4"> Routines</font></a></b></h3>

<p>The next set of routines will check the schedule control file for the message of the 
day and automatically format and send messages to all subscribers on the mailing list. 
This is handled with three routines. The first is the high-level routine that is called 
from the command button routine. The other two routines handle the details of reading the 
schedule file, reading the subscriber file, and composing and sending the messages. </p>

<p>Create a new routine called <tt><font FACE="Courier">SendMail</font></tt> and add the 
code shown in Listing 9.18. </p>

<hr>

<blockquote>
  <b><p>Listing 9.18. Adding the <tt><font FACE="Courier">SendMail</font></tt> routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub SendMail()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' read mail<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Status &quot;&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;ControlsLoad<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MAPIStart<br>
  &nbsp;&nbsp;&nbsp;&nbsp;ProcessSubList<br>
  &nbsp;&nbsp;&nbsp;&nbsp;MAPIEnd<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Status &quot;Outbound processing complete.&quot; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;MsgBox &quot;Outbound processing complete&quot;, vbInformation, 
  &quot;SendMail&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>The <tt><font FACE="Courier">SendMail</font></tt> routine first clears the status box 
and loads the master control file. Then the <tt><font FACE="Courier">MAPIStart</font></tt> 
routine is called. Once the MAPI session is established, the routine calls <tt><font
FACE="Courier">ProcessSubList</font></tt> to handle all processing of the subscriber list. 
After the list is processed, the <tt><font FACE="Courier">MAPIEnd</font></tt> routine is 
called and the status box is updated along with message to the user announcing the 
completion of the processing. </p>

<p>Next add the <tt><font FACE="Courier">ProcessSubList</font></tt> subroutine, and enter 
the code shown in Listing 9.19. </p>

<hr>

<blockquote>
  <b><p>Listing 9.19. Adding the <tt><font FACE="Courier">ProcessSubList</font></tt> 
  routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub ProcessSubList()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' read sublist to send messages<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cErr As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If bErr &lt;&gt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bErr = False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cSubList As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim nSubList As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cListSked As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim nListSked As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cSkedFile As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cFileDate As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cFileName As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cFileTitle As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cLine As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim nPos1 As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim nPos2 As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cSubList = ControlSetting(&quot;ListSubs&quot;) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;cListSked = ControlSetting(&quot;ListSchedule&quot;) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;cSkedFile = Format(Now, &quot;YYMMDD&quot;) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Status &quot;Opening Schedule File [&quot; &amp; cListSked &amp; 
  &quot;]...&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;nListSked = FreeFile<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Open cListSked For Input As nListSked <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Do While Not EOF(nListSked)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line Input #nListSked, cLine<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nPos1 = InStr(cLine, &quot;,&quot;)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If nPos1 &lt;&gt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cFileDate = 
  Left(cLine, nPos1 - 1)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nPos2 = InStr(nPos1 + 1, cLine, 
  &quot;,&quot;)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If nPos2 &lt;&gt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cFileName = 
  Mid(cLine, nPos1 + 1, nPos2 - (nPos1 + 1))<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If nPos2 + 1 &lt; Len(cLine) Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cFileTitle = 
  Mid(cLine, nPos2 + 1, 255)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cFileTitle = 
  cFileName<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If cFileDate = cSkedFile Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Do<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Loop<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Close nListSked<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Status &quot;Opening Subscriber List [&quot; &amp; cSubList &amp; 
  &quot;]...&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;nSubList = FreeFile<br>
  Listing 9.19. continued<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Open cSubList For Input As nSubList<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Do While Not EOF(nSubList)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line Input #nSubList, cLine<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If Left(cLine, 1) &lt;&gt; &quot;;&quot; 
  Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ProcessSubListMsg 
  cLine, cFileName, cFileTitle<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Loop<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>The main job of the <tt><font FACE="Courier">ProcessSubList</font></tt> routine is to 
open the schedule file, and see if there is a message to send for today's date. If one is 
found, the routine opens the subscriber control file and calls the <tt><font
FACE="Courier">ProcessSubListMsg</font></tt> routine to compose and send the message. </p>

<p>Finally, add the <tt><font FACE="Courier">ProcessSubListMsg</font></tt> routine and 
enter the code that appears in Listing 9.20. </p>

<hr>

<blockquote>
  <b><p>Listing 9.20. Adding the <tt><font FACE="Courier">ProcessSubListMsg</font></tt> 
  routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub ProcessSubListMsg(cListAddr As String, cFile As 
  String, cTitle As <font FACE="ZAPFDINGBATS">&Acirc;</font>String)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' send out message<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim nFile As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cLine As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cMsgBody As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cType As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cAddr As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim cName As String<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim objMsg As Object<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim objRecip As Object<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim nPos1 As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Dim nPos2 As Integer<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' parse address line<br>
  &nbsp;&nbsp;&nbsp;&nbsp;nPos1 = InStr(cListAddr, &quot;^&quot;) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;If nPos1 &lt;&gt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cName = Left(cListAddr, nPos1 - 1)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;nPos2 = InStr(nPos1 + 1, cListAddr, &quot;^&quot;) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;If nPos2 &lt;&gt; 0 Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cAddr = Mid(cListAddr, nPos1 + 1, nPos2 - 
  (nPos1 + 1))<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Sub<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;cType = Mid(cListAddr, nPos2 + 1, 255) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' now create message<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Status &quot;Sending Msg to &quot; &amp; cName &amp; 
  &quot;...&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' get message text<br>
  &nbsp;&nbsp;&nbsp;&nbsp;nFile = FreeFile<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Open cFile For Input As nFile<br>
  &nbsp;&nbsp;&nbsp;&nbsp;While Not EOF(nFile)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line Input #nFile, cLine<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cMsgBody = cMsgBody &amp; EOL &amp; cLine<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Wend<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Close #nFile<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' now build a new message<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objMsg = objMAPISession.Outbox.Messages.Add <br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.subject = ControlSetting(&quot;ListName&quot;) &amp; &quot; 
  [&quot; &amp; cTitle &amp; &quot;]&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.Text = cMsgBody<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' create the recipient<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objRecip = objMsg.Recipients.Add<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If cType = &quot;MS&quot; Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Name = cName ' handle local users<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Type = mapiTo<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Resolve <br>
  &nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Name = cType &amp; &quot;:&quot; 
  &amp; cAddr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.address = cType &amp; 
  &quot;:&quot; &amp; cAddr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Type = mapiTo<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' send the message<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.Update<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.Send showDialog:=False<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>The most important part of the <tt><font FACE="Courier">ProcessSubListMsg</font></tt> 
routine is the last section of code that composes and addresses the message. There are two 
main processes in this part of the routine. The first process is the creation of a new <tt><font
FACE="Courier">Message</font></tt> object: </p>

<blockquote>
  <tt><font FACE="Courier"><p>&nbsp;&nbsp;&nbsp;' now build a new message <br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objMsg = objMAPISession.Outbox.Messages.Add <br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.subject = ControlSetting(&quot;ListName&quot;) &amp; &quot; 
  [&quot; &amp; cTitle &amp; &quot;]&quot;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objMsg.Text = cMsgBody</font></tt> </p>
</blockquote>

<p>The second process is the creation of a new <tt><font FACE="Courier">Recipient</font></tt> 
object and the addressing of the message: </p>

<blockquote>
  <tt><font FACE="Courier"><p>' create the recipient<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objRecip = objMsg.Recipients.Add<br>
  &nbsp;&nbsp;&nbsp;&nbsp;If cType = &quot;MS&quot; Then<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Name = cName ' handle local users<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Type = mapiTo<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Resolve <br>
  &nbsp;&nbsp;&nbsp;&nbsp;Else<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Name = cType &amp; &quot;:&quot; 
  &amp; cAddr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.address = cType &amp; 
  &quot;:&quot; &amp; cAddr<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;objRecip.Type = mapiTo<br>
  &nbsp;&nbsp;&nbsp;&nbsp;End If</font></tt> </p>
</blockquote>

<p>Notice that addressing is handled a bit differently for <tt><font FACE="Courier">MS</font></tt>-type 
messages. Messages with the address type of <tt><font FACE="Courier">MS</font></tt> are 
addresses within the Microsoft addressing scheme-they're local addresses. To handle these 
items, you only need to load the <tt><font FACE="Courier">Name</font></tt> property, set 
the recipient type (<tt><font FACE="Courier">To:</font></tt>), and then call the MAPI <tt><font
FACE="Courier">Resolve</font></tt> method to force MAPI to look up the name in the address 
book(s). When the name is found, MAPI loads the <tt><font FACE="Courier">Address</font></tt> 
property with the complete transport and e-mail address for routing. This is how most MAPI 
messages are usually sent. </p>

<p>However, for messages of type other than <tt><font FACE="Courier">MS</font></tt>, it is 
likely that they are not in the locally available address books. These messages can still 
be sent if you load the <tt><font FACE="Courier">Address</font></tt> property of the 
message with both the transport type and the user's e-mail address. This is the way to 
handle processing for messages that were sent to you from someone who is not in your 
address book. This is known as <i>one-off addressing</i>. One-off addressing ignores the <tt><font
FACE="Courier">Name</font></tt> property and uses the <tt><font FACE="Courier">Address</font></tt> 
property to route the message. </p>

<p>That is all the code you need to send out daily messages to your subscriber list. The 
next set of routines will allow your application to scan incoming messages for mailing 
list-related items and process them as requested. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Tip</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>It is a good idea to save your project as you go along. The next set of routines are a 
      bit longer and you may want to take a break before continuing.</p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<h3><b><a NAME="CodingtheInboxRoutines">Coding the <tt><font SIZE="4" FACE="Courier">Inbox</font></tt><font
SIZE="4"> Routines</font></a></b></h3>

<p>The next set of routines handles the process of scanning the subject line of incoming 
messages for mailing-list commands. These commands are then processed and subscribers are 
added or dropped from the list and archive items are sent to subscribers as requested. </p>

<p>The <tt><font FACE="Courier">Inbox</font></tt> processing can recognize four different 
commands on the subject line. These commands are: 

<ul>
  <li><tt><font FACE="Courier">SUB</font></tt>-When this command appears in the subject line 
    of a message, the sender's name and address are added to the subscriber control file. </li>
  <li><tt><font FACE="Courier">UNSUB</font></tt>-When this command appears in the subject line 
    of a message, the sender's name and address are removed from the subscriber control file. </li>
  <li><tt><font 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -