C# Outlook加载项崩溃或服务器管理员限制了您可以同时打开的项目数
我创建了一个简单的Outlook外接程序,用于将联系人从一个文件夹复制到另一个文件夹。(~5000个联系人) 为什么我需要这个?如前所述,创建公共通讯簿有一种奇怪的方法 那么为什么不复制公用文件夹中的所有联系人呢?我希望我的团队有一个与我的联系人共享的通讯录,但只有全名和电子邮件作为信息 我添加了一个带有两个按钮的工具栏。选择文件夹并同步 我的问题是,当运行同步一段时间后,我会 服务器管理员限制了您可以同时打开的项目数。尝试关闭已打开的邮件,或从正在撰写的未发送邮件中删除附件和图像 我在每个对象中使用了C# Outlook加载项崩溃或服务器管理员限制了您可以同时打开的项目数,c#,outlook,outlook-addin,C#,Outlook,Outlook Addin,我创建了一个简单的Outlook外接程序,用于将联系人从一个文件夹复制到另一个文件夹。(~5000个联系人) 为什么我需要这个?如前所述,创建公共通讯簿有一种奇怪的方法 那么为什么不复制公用文件夹中的所有联系人呢?我希望我的团队有一个与我的联系人共享的通讯录,但只有全名和电子邮件作为信息 我添加了一个带有两个按钮的工具栏。选择文件夹并同步 我的问题是,当运行同步一段时间后,我会 服务器管理员限制了您可以同时打开的项目数。尝试关闭已打开的邮件,或从正在撰写的未发送邮件中删除附件和图像 我在每个对象
Marshal.ReleaseComObject
,试图在添加之间添加一些延迟。但仍然会得到相同的错误
然后我在StackOverflow中发现一篇帖子,上面说添加GC.Collect
,它停止了上面的错误,但是Outlook总是在同步结束时崩溃,有时在同步过程中崩溃
任何帮助都将不胜感激
同步代码
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);
}
});
}
private Task SynchronizeContactsSync()
{
返回任务。运行(异步()=>
{
如果(!正在同步)
{
同步=真;
Outlook.Folder-toFolder=null;
Outlook.Folder fromFolder=null;
尝试
{
如果(!string.IsNullOrWhiteSpace(tEntryID)&&!string.IsNullOrWhiteSpace(tStoreID)&&
!string.IsNullOrWhiteSpace(fStoreID)和&!string.IsNullOrWhiteSpace(tEntryID))
{
toFolder=Application.Session.GetFolderFromID(tEntryID,tStoreID)作为Outlook.Folder;
fromFolder=Application.Session.GetFolderFromID(fEntryID,fStoreID)作为Outlook.Folder;
}
if(toFolder!=null&&fromFolder!=null)
{
toFolder.InAppFolderSyncObject=false;
int currentItem=0;
int itemCount=fromFolder.Items.Count;
//我不想使用foreach,因为它会保留每个对象的引用,直到完成为止
//我将其强制转换为列表,因为我无法将FORM语句与fromFolder.Items一起使用
List items=fromFolder.items.Cast().ToList();
对于(int i=0;i
{
尝试
{
如果(!string.IsNullOrWhiteSpace(item.Email1Address))
{
字符串筛选器=“[FullName]=”“+item.FullName+”;/“[昵称]=”“+item.昵称+””或[Email1Address]=”“+item.Email1Address+”;或
Outlook.ContactItem匹配=(Outlook.ContactItem)toFolder.Items.Find(filter);
if(匹配==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();
元帅发布对象(联系人);
itemSyncCount++;
lastItemSync=DateTime.Now;
}
其他的
{
matches.Email1Address=item.Email1Address;
matches.Email2Address=item.Email2Address;
匹配。保存();
itemSyncCount++;
lastItemSync=DateTime.Now;
List<Outlook.ContactItem> items = fromFolder.Items.Cast<Outlook.ContactItem>().ToList(); ;
for (int i = 0; i < items.Count; i++)
{
object item = items[i];
...
Marshal.ReleaseComObject(item); item = null;
}