下面的代码十次中有九次有效,但在某些情况下我会收到以下错误:您的服务器管理员限制了您可以同时打开的项目数量。尝试关闭您已打开的邮件或从您正在撰写的未发送邮件中删除附件和图像。
我检查了它试图提取的电子邮件,它们都是正常的消息。没有任何会议或类似的东西。我什至清理了一些电子邮件。
Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application();
NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
var mail = myInbox.Items;
foreach (object items in mail)
{
var item = items as MailItem;
if (item != null)
{
if (!senderEmail.Equals(String.Empty) && senderName.Equals(String.Empty) && emailSubject.Equals(String.Empty))
{
try
{
if (((MailItem)item).SenderEmailAddress.ToLower().Contains(senderEmail.ToLower()))
{
if (count <= 40)
{
if (((MailItem)item).SenderEmailAddress.Contains(""))
{
var senderEmailAddress = ((MailItem)item).SenderEmailAddress.Remove(((MailItem)item).SenderEmailAddress.IndexOf(""), 32);
resultsGrid.Rows.Add(count, ((MailItem)item).Subject, ((MailItem)item).SenderName, senderEmailAddress, ((MailItem)item).CreationTime.ToString());
resultsGrid.AutoResizeColumns();
}
else if (((MailItem)item).SenderEmailAddress.Contains(""))
{
var senderEmailAddress = ((MailItem)item).SenderEmailAddress.Remove(((MailItem)item).SenderEmailAddress.IndexOf(""), 75);
resultsGrid.Rows.Add(count, ((MailItem)item).Subject, ((MailItem)item).SenderName, senderEmailAddress, ((MailItem)item).CreationTime.ToString());
resultsGrid.AutoResizeColumns();
}
else
{
resultsGrid.Rows.Add(count, ((MailItem)item).Subject, ((MailItem)item).SenderName, ((MailItem)item).SenderEmailAddress, ((MailItem)item).CreationTime.ToString());
resultsGrid.AutoResizeColumns();
}
count++;
}
else
{
resultsGrid.Rows.Add(String.Empty, "Total items in Mailbox: " + myInbox.Items.Count, String.Empty, String.Empty, String.Empty);
break;
}
}
}
catch (COMException e)
{
resultsGrid.Rows.Add(e.Message);
resultsGrid.AutoResizeColumns();
break;
}
continue;
}
}