如何在 outlook c# 中替换 MailItem
how to replace MailItem in outlook c#
我正在编写一个独立程序来将多个 PST 复制到一个新的 PST。当有重复的电子邮件时,我只想要一份,而不是全部。
就目前而言,我的代码是:
if (item is Outlook.MailItem)
{
Outlook.MailItem i = item as Outlook.MailItem;
Outlook.MailItem iCopy = i.Copy();
iCopy.Move(targetMAPIFolder);
}
Outlook 能够手动生成所需的结果,方法是选择:文件 > 打开 > 导入 > 从另一个程序或文件导入 > Outlook 数据文件 > 用导入的项目替换重复项。
感谢您的帮助!
你这里的主要问题是如何确定什么是重复的。如果您在单个 .PST 中移动它们,您可以比较 MailItem.Id 属性,因为这在单个 PST 中是独一无二的。当您从一个 pst 移动到另一个 pst 时,您可能想要查看您认为邮件项目上的哪些属性 'unique' 并比较它们。 (如果需要,您甚至可以使用哈希值)。
举个例子 -
var hash = String.Format("{0}{1}{2}{3}", item.To, item.From, item.CC, item.Subject, item.Body).GetHashCode();
应该给你一个哈希值来与目标 PST 中的现有项目进行比较。
或者只是比较您认为会显示重复的属性
示例 -
private bool CheckIsDuplicate(MailItem item)
{
//load the target pst
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
outlookNs.AddStore(@"D:\pst\Test.pst");
Microsoft.Office.Interop.Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);
//check for your mail item in the repository
var duplicateItem = (
from email in
emailFolder.Items.OfType<MailItem>()
where //here you could try a number of things a hash value of the properties or try using the item.I
email.SenderName == item.SenderName &&
email.To == item.To &&
email.Subject == item.Subject &&
email.Body == item.Body
select email
).FirstOrDefault();
return duplicateItem != null;
}
Outlook 对象模型不提供任何 属性 或检查重复项的方法。您需要比较项目的属性来决定是否需要复制特定项目。我建议使用 Items class 的 Find/FindNext 或 Restrict 方法来查找重复项。您也可以考虑使用应用程序 class 的 AdvancedSearch 方法。在 Outlook 中使用 AdvancedSearch 方法的主要好处是:
- 搜索在另一个线程中执行。您不需要手动 运行 另一个线程,因为 AdvancedSearch 方法 运行 会自动在后台运行它。
- 可以在任何位置(即超出某个文件夹的范围)搜索任何项目类型:邮件、约会、日历、便笺等。 Restrict 和 Find/FindNext 方法可以应用于特定的项目集合(请参阅 Outlook 中文件夹 class 的项目 属性)。
- 完全支持 DASL 查询(自定义属性也可用于搜索)。您可以在 MSDN 的 Filtering 文章中阅读更多相关信息。为了提高搜索性能,如果为商店启用了即时搜索,则可以使用即时搜索关键字(请参阅商店 class 的 IsInstantSearchEnabled 属性)。
- 最后,您可以随时使用搜索的停止方法停止搜索过程 class。
您可以在以下文章中详细了解这些方法:
- How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
- How To: Use Restrict method to retrieve Outlook mail items from a folder
- Advanced search in Outlook programmatically: C#, VB.NET
不要使用以下代码:
var duplicateItem = (
from email in
emailFolder.Items.OfType<MailItem>()
会很慢...
我正在编写一个独立程序来将多个 PST 复制到一个新的 PST。当有重复的电子邮件时,我只想要一份,而不是全部。
就目前而言,我的代码是:
if (item is Outlook.MailItem)
{
Outlook.MailItem i = item as Outlook.MailItem;
Outlook.MailItem iCopy = i.Copy();
iCopy.Move(targetMAPIFolder);
}
Outlook 能够手动生成所需的结果,方法是选择:文件 > 打开 > 导入 > 从另一个程序或文件导入 > Outlook 数据文件 > 用导入的项目替换重复项。
感谢您的帮助!
你这里的主要问题是如何确定什么是重复的。如果您在单个 .PST 中移动它们,您可以比较 MailItem.Id 属性,因为这在单个 PST 中是独一无二的。当您从一个 pst 移动到另一个 pst 时,您可能想要查看您认为邮件项目上的哪些属性 'unique' 并比较它们。 (如果需要,您甚至可以使用哈希值)。 举个例子 -
var hash = String.Format("{0}{1}{2}{3}", item.To, item.From, item.CC, item.Subject, item.Body).GetHashCode();
应该给你一个哈希值来与目标 PST 中的现有项目进行比较。
或者只是比较您认为会显示重复的属性
示例 -
private bool CheckIsDuplicate(MailItem item)
{
//load the target pst
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
outlookNs.AddStore(@"D:\pst\Test.pst");
Microsoft.Office.Interop.Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);
//check for your mail item in the repository
var duplicateItem = (
from email in
emailFolder.Items.OfType<MailItem>()
where //here you could try a number of things a hash value of the properties or try using the item.I
email.SenderName == item.SenderName &&
email.To == item.To &&
email.Subject == item.Subject &&
email.Body == item.Body
select email
).FirstOrDefault();
return duplicateItem != null;
}
Outlook 对象模型不提供任何 属性 或检查重复项的方法。您需要比较项目的属性来决定是否需要复制特定项目。我建议使用 Items class 的 Find/FindNext 或 Restrict 方法来查找重复项。您也可以考虑使用应用程序 class 的 AdvancedSearch 方法。在 Outlook 中使用 AdvancedSearch 方法的主要好处是:
- 搜索在另一个线程中执行。您不需要手动 运行 另一个线程,因为 AdvancedSearch 方法 运行 会自动在后台运行它。
- 可以在任何位置(即超出某个文件夹的范围)搜索任何项目类型:邮件、约会、日历、便笺等。 Restrict 和 Find/FindNext 方法可以应用于特定的项目集合(请参阅 Outlook 中文件夹 class 的项目 属性)。
- 完全支持 DASL 查询(自定义属性也可用于搜索)。您可以在 MSDN 的 Filtering 文章中阅读更多相关信息。为了提高搜索性能,如果为商店启用了即时搜索,则可以使用即时搜索关键字(请参阅商店 class 的 IsInstantSearchEnabled 属性)。
- 最后,您可以随时使用搜索的停止方法停止搜索过程 class。
您可以在以下文章中详细了解这些方法:
- How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
- How To: Use Restrict method to retrieve Outlook mail items from a folder
- Advanced search in Outlook programmatically: C#, VB.NET
不要使用以下代码:
var duplicateItem = (
from email in
emailFolder.Items.OfType<MailItem>()
会很慢...