处理"dib"命令
  假定已经引用了 Outlook 2003 PIA,下一步是创建一个帮助器类 (OPineHelper),用该类定义一组执行批量处理的静态方法。首先,我们有一个名为 DisplayInbox() 的方法,该方法接受 ApplicationClass 类型作为其唯一参数。DisplayInbox() 的实现将获得当前的 MAPI 命名空间,以便检索收件箱文件夹中的每个 MailItem.在这里,我们将使用 MailItem 类型的各种属性,将接收时间、发件人名称和主题打印到控制台:
|  public static void DisplayInbox(ApplicationClass o){
 // Get items in my inbox.
 NameSpace outlookNS = o.GetNamespace("MAPI");
 MAPIFolder inboxFolder
 = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
 // Print out some basic info.
 Console.WriteLine("You have {0} e-mails.",
 inboxFolder.Items.Count);
 Console.WriteLine();
 foreach(object obj in inboxFolder.Items)
 {
 MailItem item = obj as MailItem;
 if(item != null)
 {
 Console.WriteLine("-> Received: {0}",
 item.ReceivedTime.ToString());
 Console.WriteLine("-> Sender: {0}", item.SenderName);
 Console.WriteLine("-> Subject: {0}", item.Subject);
 Console.WriteLine();
 }
 }
 }
 | 
  注意,我们将通过 Items 属性所获得的项当作一般 System.Objects,而不是所期望的 MailItem 类型。此外还要注意,我们执行了一个动态检查,以确定当前项是否可以被视为 MailItem(通过 C# 的 as 关键字),以及如果这样我们将与类型的各种属性交互。我们执行该动态检查的理由是 Outlook 收件箱的确可以包含超过 MailItem 类型的项(例如,满足请求)。如果将 foreach 逻辑设置为:
|  foreach(MailItem item in inboxFolder.Items){
 ...
 }
 | 
  那么,如果遇到 MailItem 以外的任何内容,就可以收到运行库异常。
  在任何情况下,除了 ReceivedTime、SenderName 和 Subject 属性,MailItem 类型还能够访问附件和重要性级别,以及内容的 HTML 表现形式(通过 HTMLBody 属性)。有关这方面的完整细节,请参阅 Outlook 2003 文档。
  处理"snm"命令
  OPineHelper 的下一个静态方法是 SendNewMail(),该方法负责代表用户创建和发送新的电子邮件。和前面看到的一样,我们将通过 ApplicationClass.CreateItem() 创建新的 MailItem 类型。阅读到这里,以下代码应当很容易理解:
|  public static void SendNewMail(ApplicationClass o){
 // Create a new MailItem.
 MailItem myMail =
 (MailItem)o.CreateItem(OlItemType.olMailItem);
 // Now gather input from user.
 Console.Write("Receiver Name: ");
 myMail.Recipients.Add(Console.ReadLine());
 Console.Write("Subject: ");
 myMail.Subject = Console.ReadLine();
 Console.Write("Message Body: ");
 myMail.Body = Console.ReadLine();
 // Send it!
 myMail.Send();
 }
 | 
  创建 (cn) 和显示 (dn) 注释
  假如我们实际需要做的只是重复用来创建新电子邮件和遍历现有电子邮件项的过程,那么随后两个静态方法是很简单的。在以下代码中,请注意由 OlItemType 和 OlDefaultFolders 枚举所指定值:
|  public static void CreateNote(ApplicationClass o){
 // Get access to notes.
 NoteItem myNote =
 (NoteItem)o.CreateItem(OlItemType.olNoteItem);
 Console.Write("Enter your note: ");
 myNote.Body = Console.ReadLine();
 // Now save the note.
 myNote.Save();
 }
 public static void DisplayNotes(ApplicationClass o)
 {
 // Get items in my inbox.
 NameSpace outlookNS = o.GetNamespace("MAPI");
 MAPIFolder notesFolder
 = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderNotes);
 // Print out some basic info.
 Console.WriteLine("You have {0} notes.",
 notesFolder.Items.Count);
 Console.WriteLine();
 foreach(NoteItem item in notesFolder.Items)
 {
 Console.WriteLine("-> Time Created: {0}", item.CreationTime);
 Console.WriteLine("-> Body: {0}", item.Body);
 Console.WriteLine();
 }
 }
 | 
  最后接触
  这里关心的最后的静态方法只是向最终用户显示一组选项:
| public static void DisplayOPineOptions() {
 Console.WriteLine("***** Welcome To OPine *****");
 Console.WriteLine("dib : Display Inbox");
 Console.WriteLine("snm : Send New Mail");
 Console.WriteLine("cn  : Create Note");
 Console.WriteLine("dn  : Display Notes");
 Console.WriteLine("q   : Quit");
 Console.WriteLine("****************************");
 }
 | 
  这将包装 OPine 帮助器类的创建过程;现在可以使用它。
  实现 Main() 方法
  到这里,我们准备实现 Main() 方法,该方法负责执行以下任务:
  * 创建 ApplicationClass 类型的实例
  * 通过 Console.ReadLine() 获得用户的命令选项
  * 接受用户提供的字符串,并执行合适的方法 OPineHelper
  给出这些要点后,下面是一个可能的实现:
 
|  static void Main(string[] args){
 // String to hold the user request.
 string userOption;
 // Create an Outlook application object.
 ApplicationClass outLookApp = new ApplicationClass();
 // Display info.
 OPineHelper.DisplayOPineOptions();
 do
 {
 Console.Write("\nPlease enter your command: ");
 userOption = Console.ReadLine();
 switch(userOption)
 {
 // Display Inbox (dib)
 case "dib":
 OPineHelper.DisplayInbox(outLookApp);
 break;
 // Create Note (cn)
 case "cn":
 OPineHelper.CreateNote(outLookApp);
 break;
 // Send New Mail (snm)
 case "snm":
 OPineHelper.SendNewMail(outLookApp);
 break;
 // Display Notes (dn)
 case "dn":
 OPineHelper.DisplayNotes(outLookApp);
 break;
 // Quit (q)
 case "q":
 userOption = "q";
 break;
 default:  // Anything else? Just display options.
 OPineHelper.DisplayOPineOptions();
 break;
 }
 }while(userOption != "q");
 }
 | 
  处理 NewMailEx 事件
  我们将添加到 OPine 中的最后一项功能是处理传入新电子邮件的能力。首先,在分配 ApplicationClass 类型之后处理 NewMailEx 事件:
|  // Create an Outlook application object.ApplicationClass outLookApp = new ApplicationClass();
 // Rig up the new message event.
 outLookApp.NewMailEx += new
 ApplicationEvents_11_NewMailExEventHandler(outLookApp_NewMailEx);
 | 
  ApplicationEvents_11_NewMailExEventHandler 委托的目标需要一个类型为 System.String 的参数。该字符串的值表示新的 MailItem 本身的 ID(可以通过 NameSpace.GetItemFromID() 方法来获得 MailItem)。
  在后面的事件处理程序中,注意我们使用 System.Windows.Forms.MessageBox 类型来通知用户有新的邮件,所以一定要添加对 System.Windows.Forms.dll 的引用(并使用指令集更新您的文件):
|  private static void outLookApp_NewMailEx(string EntryIDCollection){
 if(DialogResult.Yes ==
 MessageBox.Show("Do you want to see your message?",
 "You've got mail!", MessageBoxButtons.YesNo))
 {
 // Get the incoming MailItem based on ID.
 ApplicationClass o = new ApplicationClass();
 NameSpace outlookNS = o.GetNamespace("MAPI");
 MAPIFolder mFolder =
 o.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
 MailItem newMail = (MailItem)
 outlookNS.GetItemFromID(EntryIDCollection, mFolder.StoreID);
 // Now print out.
 Console.WriteLine("\n\n***** New Message *****");
 Console.WriteLine("-> Sender: {0}", newMail.SenderName);
 Console.WriteLine("-> Subject: {0}", newMail.Subject);
 Console.WriteLine("-> Body: {0}", newMail.Body);
 Console.WriteLine("***********************\n\n");
 }
 }
 | 
  这就是最后的步骤。现在我们可以执行编译,并对 OPine 进行测试(图 5)。

图 5. 运行中的 Opine
  我敢肯定,您可以找到很多方式来扩展和改进 OPine,包括从基于控制台的 UI 移动到图形 UI(通过 Windows 窗体)。尽管我显然知道你们很少生成命令行电子邮件程序,但我希望该示例已经阐明了通过自定义应用程序与 Outlook 交互的过程。
查看本文来源