C# Outlook电子邮件搜索崩溃
我正在编写一个代码,获取一个列表,并检查邮件主题中是否有项目,我试图查看所有邮件以与列表进行比较,但程序在收到190封左右的邮件后崩溃(文件夹中有289封邮件) 我在网上搜索这个问题的解决方案,但什么都不管用C# Outlook电子邮件搜索崩溃,c#,winforms,outlook,C#,Winforms,Outlook,我正在编写一个代码,获取一个列表,并检查邮件主题中是否有项目,我试图查看所有邮件以与列表进行比较,但程序在收到190封左右的邮件后崩溃(文件夹中有289封邮件) 我在网上搜索这个问题的解决方案,但什么都不管用 oApp = new Microsoft.Office.Interop.Outlook.Application(); // Get the MAPI namespace. oNS = oApp.GetNamespace("mapi");
oApp = new Microsoft.Office.Interop.Outlook.Application();
// Get the MAPI namespace.
oNS = oApp.GetNamespace("mapi");
// Log on by using the default profile or existing session (no dialog box).
oNS.Logon(Missing.Value, Missing.Value, false, true);
// Alternate logon method that uses a specific profile name.
// TODO: If you use this logon method, specify the correct profile name
// and comment the previous Logon line.
//oNS.Logon("profilename",Missing.Value,false,true);
//Get the Inbox folder.
oInbox = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
//Get the Items collection in the Inbox folder.
oItems = oInbox.Items;
MessageBox.Show(oItems.Count+"");
oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oItems.GetLast();
public void SearchEmail()
{
if (counter<oItems.Count)
{
try
{
string s;
if (oMsg != null)
{
s = oMsg.Subject;
for (int i = 0; i < allTickets.Count; i++)
{
label2.Text = oMsg.Subject;
string ticketID = allTickets[i].ToString();
if (s.Contains(ticketID) && oMsg.UnRead)
{
unreadTickets.Add(ticketID);
}
}
}
oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oItems.GetPrevious();
counter++;
button2.Text = counter + "";
}
//Error handler.
catch (Exception x)
{
//MessageBox.Show(x.ToString());
}
}
}
oApp=new Microsoft.Office.Interop.Outlook.Application();
//获取MAPI命名空间。
oNS=oApp.GetNamespace(“mapi”);
//使用默认配置文件或现有会话登录(无对话框)。
oNS.Logon(Missing.Value、Missing.Value、false、true);
//使用特定配置文件名称的备用登录方法。
//TODO:如果使用此登录方法,请指定正确的配置文件名称
//并注释上一个登录行。
//oNS.Logon(“profilename”,缺少.Value,false,true);
//获取收件箱文件夹。
oInbox=oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
//获取收件箱文件夹中的项目集合。
oItems=oInbox.Items;
MessageBox.Show(oItems.Count+“”);
oMsg=(Microsoft.Office.Interop.Outlook.MailItem)oItems.GetLast();
公共无效搜索电子邮件()
{
if(counter请记住,收件箱可以包含除MailItem
以外的项目,例如ReportItem
或MeetingItem
。这将解释强制转换错误。您需要将项目视为通用对象,并使用反射读取Class
属性(由所有OOM对象公开)-如果是43(olMailItem
),您可以将其强制转换为MailItem
。或者您可以使用“as”运算符并检查null
还请记住,除非您首先调用Items,否则Items集合不会按任何特定顺序排序。Sort
并指定排序顺序;否则,您将从Items中获得什么。GetLast
未确定。欢迎这样做,您还应该发布错误消息你提出了你的问题,那么其他人可以更好地理解这个问题并帮助你。你确定所有237个邮件都是邮件吗?omsg在哪里定义的?请将此设置为a嘿,伙计们,谢谢你的帮助!我试图使代码更易读和理解请查看,@BugFinder我将所有收件箱消息复制到excel以查看how有许多邮件,并且是正确的数字。正确的数字是哪一行显示null?oMsg=(Microsoft.Office.Interop.Outlook.MailItem)oItems.GetPrevious();如果(s.Contains(ticketID)&&oMsg.UnRead),则会在@BugFinder中出现错误