28一/090
用c#从Outtlook 2007里读取email信息
首先要添加对Microsoft.Office.Interop.Outlook的引用。
using Microsoft.Office.Interop.Outlook之后,操作Outlook的代码如下。
注意,代码中演示了如何取出特定文件夹,和取出inbox等特殊文件夹:
Application myApp = new ApplicationClass();
NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
MAPIFolder folder;
folder = myApp.Session.Folders["邮箱 - Xiangpeng Zhao"].Folders["Maillists"].Folders["Windows 7"];
//folder = mapiNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
foreach (MailItem mail in folder.Items) {
string subject = mail.Subject;
string body = mail.Body; // this will trigger the security warning...
string senderName = mail.SenderName;
string senderEmail = mail.SenderEmailAddress; // security warning
}
如何跳过讨厌的Outtlook安全警告!
运行上面的代码,Outlook就会弹出一个对话框:“A program is trying to access e-mail address information stored in Outlook…”必须按下“Allow”才能访问email信息。怎么跳过这个对话框?
这个问题的解决方法比我想像得复杂。针对不同的场景,可能需要不同的方案。Outlook "Object Model Guard" Security Issues for Developers是一篇非常好的文章,介绍了各种方法。
因为我只想读取我自己机器上的email,所以就装了个免费的Outlook插件Advanced Security for Outlook,它可以自动替换Outlook的安全警告对话框,取而代之的是一个允许自定义信任程序的对话框:
这确实很方便。