c#实现outlook2003邮件内容读取

网上用来读取outlook express的信息很少,又想来获得邮件信息便于分析;用了一种变通的办法.将outlook express的邮件信息导入outlook 2003再用程序来取得其中的信息.用c#实现.并用正则的方法提取了邮件内容中的邮件地址
有兴趣的网友共同研究

 

          Outlook.ApplicationClass olApp = new Outlook.ApplicationClass();
                Outlook.NameSpace ns = olApp.GetNamespace("MAPI");

                Outlook.MAPIFolder selectFolder = null;
                Outlook.MailItem mi = null;

                // 获得收件箱信息
                selectFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                this.label1.Text = "收件箱:共有" + selectFolder.Items.Count.ToString() + "封邮件";
                this.label1.Refresh();

              
                dt.Columns.Add("1", typeof(string));
                dt.Columns.Add("2", typeof(string));
                dt.Columns.Add("3", typeof(string));
                dt.Columns.Add("4", typeof(string));
                dt.Columns.Add("5", typeof(string));
                DataRow dr = null;

               
                string isread = "未读";
                int i = 1; 
                foreach (object item in selectFolder.Items)
                {
                    mi = item as Outlook.MailItem;
                    if (mi.UnRead == false) { isread = "已读"; }
                    dr = dt.NewRow();
                    dr["1"] = i.ToString();
                    dr["2"] = mi.Subject;
                    dr["3"] = mi.CreationTime.ToString();
                    dr["4"] = isread;
                    dr["5"] = mi.Body;

                    dt.Rows.Add(dr);

                   i++;
                    this.label3.Text ="正在读取:"+ i.ToString();
                    this.label3.Refresh();
                }
                this.dataGridView1.DataSource = dt;

 

源代码 ::http://download.csdn.net/source/2097751

以下是使用 C#Outlook 中收取指定邮件附件的示例代码: ```csharp using System; using System.IO; using Microsoft.Office.Interop.Outlook; namespace OutlookAttachmentDemo { class Program { static void Main(string[] args) { Application outlookApp = new Application(); NameSpace outlookNS = outlookApp.GetNamespace("MAPI"); // 登录邮箱账号 outlookNS.Logon("YourEmailAddress", null, false, false); MAPIFolder inboxFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox); // 搜索指定的邮件 string subject = "Test email"; Items items = inboxFolder.Items.Restrict("[Subject] = '" + subject + "'"); if (items.Count == 0) { Console.WriteLine("No email found with subject: " + subject); return; } // 获取第一封符合条件的邮件 MailItem mailItem = (MailItem)items[1]; // 保存邮件附件 foreach (Attachment attachment in mailItem.Attachments) { if (attachment.FileName.EndsWith(".txt")) { string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), attachment.FileName); attachment.SaveAsFile(filePath); Console.WriteLine("Attachment saved to: " + filePath); } } // 退出邮箱账号 outlookNS.Logoff(); } } } ``` 在上面的代码中,我们首先使用 `Application` 和 `NameSpace` 类创建了一个 Outlook 应用程序实例,并登录了指定的邮箱账号。然后我们搜索指定主题的邮件,并获取第一封符合条件的邮件。最后,我们遍历邮件附件,如果附件的文件名以 `.txt` 结尾,则将其保存到桌面上。 请注意,为了运行此示例代码,您需要将 Outlook 安装在您的计算机上,并添加对 `Microsoft.Office.Interop.Outlook` 程序集的引用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lyflcear

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值