我创建了一个简单的Outlook插件,用于将联系人从一个文件夹复制到另一个文件夹。(~5000个联系人)
为什么我需要这个?创建公共地址簿有一种奇怪的方式,如here所述。
那么为什么不复制公用文件夹中的所有联系人呢?我希望我的团队与我的联系人共享一个地址簿,但只有全名和电子邮件作为信息。
我添加了一个带有两个按钮的工具栏。选择文件夹并同步。
我的问题是,在一段时间后运行同步时我得到了
您的服务器管理员限制了您可以同时打开的项目数。尝试关闭已打开的邮件,或从正在撰写的未发送邮件中删除附件和图像。
我在每个Object中使用了Marshal.ReleaseComObject
,试图在Add之间添加一些延迟。但仍然得到同样的错误。
然后我在StackOverflow中找到了一条帖子,说要添加GC.Collect
来阻止上面的错误,但Outlook在同步结束时总是崩溃,有时候在中间。< / p>
任何帮助将不胜感激。
同步代码
private Task SynchronizeContactsSync()
{
return Task.Run(async () =>
{
if (!synchronizing)
{
synchronizing = true;
Outlook.Folder toFolder = null;
Outlook.Folder fromFolder = null;
try
{
if (!string.IsNullOrWhiteSpace(tEntryID) && !string.IsNullOrWhiteSpace(tStoreID) &&
!string.IsNullOrWhiteSpace(fStoreID) && !string.IsNullOrWhiteSpace(tEntryID))
{
toFolder = Application.Session.GetFolderFromID(tEntryID, tStoreID) as Outlook.Folder;
fromFolder = Application.Session.GetFolderFromID(fEntryID, fStoreID) as Outlook.Folder;
}
if (toFolder != null && fromFolder != null)
{
toFolder.InAppFolderSyncObject = false;
int currentItem = 0;
int itemCount = fromFolder.Items.Count;
//I dont want to use foreach because it keeps reference of each object until is done
//I cast it to list because i cant use for statement with fromFolder.Items
List<Outlook.ContactItem> items = fromFolder.Items.Cast<Outlook.ContactItem>().ToList(); ;
for (int i = 0; i < items.Count; i++)
{
//await Task.Delay(33);
await addContactSync(items[i], toFolder);
if (items[i] != null) Marshal.ReleaseComObject(items[i]);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
currentItem++;
syncText = "Synchronize progress " + currentItem + " of " + itemCount;
}
synchronizing = false;
syncText = "No Synchronize in progress";
}
MessageBox.Show("Done.", "Synchronize", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (toFolder != null) Marshal.ReleaseComObject(toFolder);
if (fromFolder != null) Marshal.ReleaseComObject(fromFolder);
toFolder.InAppFolderSyncObject = true;
}
catch (Exception ex)
{
if (toFolder != null) Marshal.ReleaseComObject(toFolder);
if (fromFolder != null) Marshal.ReleaseComObject(fromFolder);
toFolder.InAppFolderSyncObject = true;
synchronizing = false;
syncText = "No Synchronize in progress";
MessageBox.Show(ex.Message, "Synchronize", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Check you settings or please wait for the synchronization to finish.", "Synchronize", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
});
}
private Task addContactSync(Outlook.ContactItem item, Outlook.Folder toFolder)
{
return Task.Run(() =>
{
try
{
if (!string.IsNullOrWhiteSpace(item.Email1Address))
{
string filter = "[FullName] = '" + item.FullName + "'";// "[NickName] = '" + item.NickName + "' or [Email1Address] = '" + item.Email1Address + "' or
Outlook.ContactItem matches = (Outlook.ContactItem)toFolder.Items.Find(filter);
if (matches == null)
{
Outlook.ContactItem contact = toFolder.Items.Add(Outlook.OlItemType.olContactItem);
contact.FullName = item.FullName;
contact.NickName = item.NickName;
contact.Email1Address = item.Email1Address;
contact.Email2Address = item.Email2Address;
contact.Save();
Marshal.ReleaseComObject(contact);
itemSyncCount++;
lastItemSync = DateTime.Now;
}
else
{
matches.Email1Address = item.Email1Address;
matches.Email2Address = item.Email2Address;
matches.Save();
itemSyncCount++;
lastItemSync = DateTime.Now;
}
if (item != null) Marshal.ReleaseComObject(item);
if (matches != null) Marshal.ReleaseComObject(matches);
}
}
catch (Exception ex)
{
Marshal.ReleaseComObject(item);
MessageBox.Show(ex.Message, "Contact", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
});
}
答案 0 :(得分:1)
不要对Outlook集合使用任何LINQ语句。或者你将陷入这种情况。例如:
for (int i = 0; i < items.Count; i++)
{
object item = items[i];
...
Marshal.ReleaseComObject(item); item = null;
}
代码行同时为文件夹中的每个项创建一个新的COM对象。相反,您可以迭代 for 循环中文件夹中的所有项目,并使用Marshal.ReleaseComObject方法立即释放对象。
WRAP_CONTENT
完成使用后,使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Outlook对象。如果您的加载项尝试枚举存储在Microsoft Exchange Server上的集合中超过256个Outlook项目,这一点尤为重要。如果您未及时发布这些对象,则可以达到Exchange对任何时候打开的最大项目数的限制。然后在Visual Basic中将变量设置为Nothing(C#中为null)以释放对该对象的引用。请在Systematically Releasing Objects文章中详细了解相关内容。