删除Outlook 2007中重复邮件的C#代码
2009-03-13 11:27
369 查看
除夕的时候写过一个用来删除Outlook中重复邮件的程序,因为年底前Apply了多次配置不恰当的Rules导致在不同的子目录中出现了很多相同的邮件。(Outlook会把分别匹配各条规则的邮件,分发到各规则指定的文件夹中,出现多个拷贝,这是By design的)
当时写的代码,主要是为了完成这个目标,基本没有考虑效率问题,代码也很混乱。这个星期写了另一个控制Outlook的C#程序,就顺手把那个这个程序重写了一下,优化了不少。
当时写的代码,主要是为了完成这个目标,基本没有考虑效率问题,代码也很混乱。这个星期写了另一个控制Outlook的C#程序,就顺手把那个这个程序重写了一下,优化了不少。
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Office.Interop.Outlook; namespace CleanRedundantMail { class Program { static Queue<MailItem> mailQueue = new Queue<MailItem>(); static int numDeleted = 0; static void Main(string[] args) { Application outLookApp = new Application(); NameSpace outlookNS = outLookApp.GetNamespace("MAPI"); MAPIFolder inBox = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox); ListAllFolders(inBox); DeleteRedundant(outLookApp); Console.WriteLine(numDeleted); } private static void ListAllFolders(MAPIFolder folder) { ListAllMails(folder); foreach (MAPIFolder subfolder in folder.Folders) { ListAllFolders(subfolder); } } private static void ListAllMails(MAPIFolder folder) { DateTime tryTime; foreach (object obj in folder.Items) { MailItem olMail = obj as MailItem; if (olMail != null) { try { tryTime = olMail.ReceivedTime; mailQueue.Enqueue(olMail); } catch { } } } } private static void DeleteRedundant(Application outLookApp) { MailItem tempMail = outLookApp.CreateItem(OlItemType.olMailItem) as MailItem; tempMail.Subject = ""; foreach (MailItem olMail in mailQueue.OrderBy(mailinfo => mailinfo.ReceivedTime)) { if (olMail.ReceivedTime == tempMail.ReceivedTime && olMail.Subject == tempMail.Subject) { olMail.Delete(); numDeleted++; } else { tempMail = olMail; } } } } }
相关文章推荐
- 上一篇c#和outlook发送邮件使用的file类的部分代码
- 删除outlook的重复邮件
- 如何删除 Outlook 中的重复邮件?
- 批量删除outlook里面重复的邮件
- 用于 Outlook 2003 的删除重复邮件的插件(加载项)
- 使用C#模拟Outlook发送邮件,代码编译报错
- 如何删除 Outlook 中的重复邮件
- 恢复Outlook 2010/2007已被删除邮件的方法
- 删除Outlook客户端重复的邮件
- OUTLOOK自动删除重复邮件脚本
- Outlook中删除重复的邮件
- C# winform 通过调用outlook发送邮件
- 使用c#给outlook添加任务、发送邮件
- 编写高质量代码改善C#程序的157个建议——建议98:用params减少重复参数
- c#语言邮件发送参考代码
- C#代码片段 导入一个txt文本,快速过滤重复行
- outlook 2007中 邮件中无法显示图片的解决办法
- c# 发送邮件代码,带附件
- 如何删除outlook自动记忆的邮件地址 [ 光影人像 东海陈光剑 的博客 ]
- C#实现发送邮件——核心部分代码