在 Outlook 中读取未读邮件的问题
Issues reading unread mails in Outlook
我正在尝试为 Outlook 开发加载项。因为我想在收到新邮件时删除附件。
所以我在 NewMailEx
事件上调用我的函数。它工作正常。在该功能中,我尝试在 Outlook 收件箱中查找未读邮件。从那些我删除附件。
我的问题是:当我打开 Outlook 时,我收到的第一封邮件没有显示在收件箱中(不是在 outlook c# 代码中),所以我无法从该邮件中删除附件。
从第二封邮件(第一封邮件之后),我可以得到收到的邮件,所以我可以删除附件。
每当我关闭并重新打开 Outlook 时,我收到的第一封邮件都会出现这个问题。
收到第一封邮件时没有未读邮件。
private void Application_NewMailEx(object Item)
{
string senderEmailid = string.Empty;
outlookNameSpace = this.Application.GetNamespace("MAPI");
Outlook.Application myApp = new Outlook.Application();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Attachments attachments;
int test = myInbox.Items.Count;
Microsoft.Office.Interop.Outlook.Recipients recipients = ((Outlook.MailItem)myInbox.Items[test]).Recipients;
Outlook.Items unreadItems = myInbox.Items.Restrict("[Unread]=true");
if (unreadItems.Count > 0)
{
foreach (Outlook.MailItem mail in unreadItems)
{
Outlook.Recipient recip;
Outlook.ExchangeUser exUser;
string sAddress;
if (mail.SenderEmailType.ToLower() == "ex")
{
recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(mail.SenderEmailAddress);
exUser = recip.AddressEntry.GetExchangeUser();
sAddress = exUser.PrimarySmtpAddress;
}
else
{
sAddress = mail.SenderEmailAddress.Replace("'", "");
}
string[] emailAddressPart = sAddress.Split('@');
string strSenderDomain = emailAddressPart[1];
if (lstDomain.Any(item => item.Contains(strSenderDomain)))
{
attachments = mail.Attachments;
int nAttachmentCount = mail.Attachments.Count;
if (nAttachmentCount > 0)
{
int j = nAttachmentCount;
for (int i = 1; i <= nAttachmentCount; i++)
{
mail.Attachments[j].Delete();
j--;
}
}
}
}
}
}
对此你无能为力。来自 NewMailEx
上的文档:
Also, the event will fire only if Outlook is running. In other words, it will not fire for the new items that are received in the Inbox when Outlook was not open.
这意味着您必须在打开 Outlook 时手动调用您的方法来查看所有未读邮件。
应用程序 class 的 NewMailEx 事件不是搜索未读电子邮件的最佳位置。对于 Microsoft Outlook 处理的每个接收项目,此事件都会触发一次。该项目可以是多种不同项目类型中的一种,例如 MailItem、MeetingItem 或 SharingItem。 EntryIDsCollection 字符串包含对应于该项目的条目 ID。另一种方法是处理 Items class.
的 ItemAdd 事件
相反,您可以等到 Outlook 完成同步项目和 运行 用于搜索未读电子邮件的代码。在 Microsoft Outlook 使用指定的 Send/Receive 组完成同步用户文件夹后,将立即触发 SyncObject class 的 SyncEnd 事件。
您也可以考虑处理 Startup 或 MAPILogonComplete 事件。但是它们在同步完成之前就被触发了。考虑在 Outlook 启动后使用计时器 运行 该过程。
您可以在以下系列文章中了解处理传入电子邮件的可能方法:
Outlook NewMail event unleashed: the challenge (NewMail, NewMailEx, ItemAdd)
Outlook NewMail unleashed: writing a working solution (C# example)
我还建议打破调用链并在单独的代码行上声明每个 属性 或方法调用。使用 MSDN 中的 System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects 文章。
我正在尝试为 Outlook 开发加载项。因为我想在收到新邮件时删除附件。
所以我在 NewMailEx
事件上调用我的函数。它工作正常。在该功能中,我尝试在 Outlook 收件箱中查找未读邮件。从那些我删除附件。
我的问题是:当我打开 Outlook 时,我收到的第一封邮件没有显示在收件箱中(不是在 outlook c# 代码中),所以我无法从该邮件中删除附件。
从第二封邮件(第一封邮件之后),我可以得到收到的邮件,所以我可以删除附件。
每当我关闭并重新打开 Outlook 时,我收到的第一封邮件都会出现这个问题。
收到第一封邮件时没有未读邮件。
private void Application_NewMailEx(object Item)
{
string senderEmailid = string.Empty;
outlookNameSpace = this.Application.GetNamespace("MAPI");
Outlook.Application myApp = new Outlook.Application();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Attachments attachments;
int test = myInbox.Items.Count;
Microsoft.Office.Interop.Outlook.Recipients recipients = ((Outlook.MailItem)myInbox.Items[test]).Recipients;
Outlook.Items unreadItems = myInbox.Items.Restrict("[Unread]=true");
if (unreadItems.Count > 0)
{
foreach (Outlook.MailItem mail in unreadItems)
{
Outlook.Recipient recip;
Outlook.ExchangeUser exUser;
string sAddress;
if (mail.SenderEmailType.ToLower() == "ex")
{
recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(mail.SenderEmailAddress);
exUser = recip.AddressEntry.GetExchangeUser();
sAddress = exUser.PrimarySmtpAddress;
}
else
{
sAddress = mail.SenderEmailAddress.Replace("'", "");
}
string[] emailAddressPart = sAddress.Split('@');
string strSenderDomain = emailAddressPart[1];
if (lstDomain.Any(item => item.Contains(strSenderDomain)))
{
attachments = mail.Attachments;
int nAttachmentCount = mail.Attachments.Count;
if (nAttachmentCount > 0)
{
int j = nAttachmentCount;
for (int i = 1; i <= nAttachmentCount; i++)
{
mail.Attachments[j].Delete();
j--;
}
}
}
}
}
}
对此你无能为力。来自 NewMailEx
上的文档:
Also, the event will fire only if Outlook is running. In other words, it will not fire for the new items that are received in the Inbox when Outlook was not open.
这意味着您必须在打开 Outlook 时手动调用您的方法来查看所有未读邮件。
应用程序 class 的 NewMailEx 事件不是搜索未读电子邮件的最佳位置。对于 Microsoft Outlook 处理的每个接收项目,此事件都会触发一次。该项目可以是多种不同项目类型中的一种,例如 MailItem、MeetingItem 或 SharingItem。 EntryIDsCollection 字符串包含对应于该项目的条目 ID。另一种方法是处理 Items class.
的 ItemAdd 事件相反,您可以等到 Outlook 完成同步项目和 运行 用于搜索未读电子邮件的代码。在 Microsoft Outlook 使用指定的 Send/Receive 组完成同步用户文件夹后,将立即触发 SyncObject class 的 SyncEnd 事件。
您也可以考虑处理 Startup 或 MAPILogonComplete 事件。但是它们在同步完成之前就被触发了。考虑在 Outlook 启动后使用计时器 运行 该过程。
您可以在以下系列文章中了解处理传入电子邮件的可能方法:
Outlook NewMail event unleashed: the challenge (NewMail, NewMailEx, ItemAdd)
Outlook NewMail unleashed: writing a working solution (C# example)
我还建议打破调用链并在单独的代码行上声明每个 属性 或方法调用。使用 MSDN 中的 System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects 文章。