惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
Cloudbric
Cloudbric
C
CERT Recently Published Vulnerability Notes
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
C
Cisco Blogs
T
Tenable Blog
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
I
Intezer
AWS News Blog
AWS News Blog
IT之家
IT之家
博客园 - 司徒正美
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Spread Privacy
Spread Privacy
S
SegmentFault 最新的问题
博客园 - Franky
人人都是产品经理
人人都是产品经理
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
Visual Studio Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Hacker News: Front Page
Latest news
Latest news
Scott Helme
Scott Helme
腾讯CDC
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
Arctic Wolf
S
Securelist
雷峰网
雷峰网
The GitHub Blog
The GitHub Blog
Project Zero
Project Zero
Google DeepMind News
Google DeepMind News
P
Palo Alto Networks Blog
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
云风的 BLOG
云风的 BLOG
Security Archives - TechRepublic
Security Archives - TechRepublic
The Last Watchdog
The Last Watchdog
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 最新话题
S
Schneier on Security
NISL@THU
NISL@THU
Jina AI
Jina AI
M
MIT News - Artificial intelligence

博客园 - 夏狼哉

.Net framework 的浏览器定义文件 Windows按名称排序问题 IE的文档模式, 及Textarea呈现bug一例 SilverLight与WCF服务双工通讯第二篇:Net.Tcp binding SilverLight与WCF服务双工通讯第一篇:PollingDuplexHttpBinding SilverLight通过Net.TCP(NetTCPBinding)方式调用WCF服务 WCF与Asp.Net Web Application的深度整合方法 分布式版本控制系统Mercurial(二):web server的架设 分布式版本控制系统Mercurial(一):Mercurial基本功能介绍 C#将汉字转换为拼音首字母 nbsp空格 VS2010 targetFramework 错误 在网络上共享条码打印机 WCF身份验证之二:使用MessageHeader进行验证 WCF身份验证之一:使用证书进行验证 Windows CE 5.0 Emulator [条码打印]使用斑马语言(ZPL)打印汉字 C#打印条码与ZPL 应用程序在网络上通信的实现(C#)
C#与Outlook交互收发邮件
夏狼哉 · 2012-03-11 · via 博客园 - 夏狼哉

.Net对POP3邮件系统已经集成了相应的功能,但是如果是基于Exchange server的邮件系统,相对就比较复杂。如果仅仅是发送,可以简单地调用CDO来实现(参见我以前的一篇文章http://www.cnblogs.com/Moosdau/archive/2007/09/28/908834.html),但是如果要接收或进行其它一些更复杂一些操作,CDO就无法实现。

事实上,Exchange Server 2003根本不支持与.Net直接交互,据说Exchange Server 2007开放了一组Web Service接口,如果使用了Exchange Server 2007,则可以直接通过Web Service接口直接与Exchange server交互,不过我们公司目前还是使用exchange server 2003,所以也没有测试这组接口要如何使用。

对使用exchange server 2003的环境来说,代价最低的应该说就是调用outlook的功能了,以下列举与outlook交互的一些常用操作。

首先,在项目中添加对outlook组件的引用(Project—>Add Reference—>切换到COM标签页—>选择Microsoft Outlook 14.0 Object Library),这里outlook的具体版本号取决于本地安装的outlook版本,我安装的是outlook 2010, 所以显示的版本号是14.0,这个关系不大,各个版本之间的代码似乎是完全相同的。

以下代码列举收件箱中的未读邮件:

            var app = new Microsoft.Office.Interop.Outlook.Application();
            var ns = app.GetNamespace("MAPI");
            ns.Logon("Outlook", Type.Missing, false, false);

            var inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            for (int i = 1; i <= inbox.Items.Count; i++)
            {
                if (inbox.Items[i].UnRead)
                {
                    txtMailList.Text += inbox.Items[i].Subject + System.Environment.NewLine;
                }
            }
            ns.Logoff();
            Marshal.ReleaseComObject(inbox);
            Marshal.ReleaseComObject(ns);
            Marshal.ReleaseComObject(app);
            inbox = null;
            ns = null;
            app = null;

上述代码第三行中出现的“Outlook”字样,这是Outlook自动创建的默认profile名称,如果曾经修改过这个profile,或者本地包含多个profile,或者不确定profile名称,请点击控制面板-->User Accounts-->邮件,如下图:

image

点击“显示配置文件”:

image

即可看到配置文件的名称。

用循环枚举收件箱的项目时,需要注意从1开始编号。

如果要读取本地数据文件中的邮件:

var localFolder = ns.Stores["Local"].GetRootFolder().Folders["Archieve"];

如果要删除文件中的邮件,注意每删除一封索引号都会重新编号,所以不能递增循环, 而必须从大到小递减循环。

如果需要调用exchange server解析别名的功能:

                string alias = "Marvin Yan";
                var recipient = app.Session.CreateRecipient(alias);
                if (!recipient.Resolve())
                {
                      //alias can't be recoganized.
                }

根据recipient获取smtp地址(username@server 格式的邮件地址):

string mailAddr = recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress;

如果要回复一封已有邮件:

                        var mail = item.Reply();
                        mail.To = item.SenderEmailAddress;
                        mail.Subject = "Hello";
                        mail.HTMLBody = "F.Y.I.<br />" + mail.HTMLBody;
                        mail.Send();

创建一封新的邮件并发送的代码如下:

            var mail = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            mail.HTMLBody = "Hello!";
            //Add an attachment.
            String attachName = "hello";
            int attachPos = (int)mail.Body.Length + 1;
            int attachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
            //now attached the file
            mail.Attachments.Add(@"C:\\hello.txt", attachType, attachPos, attachName);
            //Subject line
            mail.Subject = "test";
            // Add a recipient.
            var oRecip = mail.Recipients.Add("xx@xxx.com");
            oRecip.Resolve();
            // Send.
            mail.Send();