gpt4 book ai didi

c# - 使用 C# 在 outlook 中解析电子邮件

转载 作者:太空狗 更新时间:2023-10-30 01:08:21 26 4
gpt4 key购买 nike

我正在编写一个程序来阅读我所有的 outlook 电子邮件,最终搜索将更加具体,但现在我想阅读收件箱中的所有电子邮件。由于某种原因,我有代码运行读取我想要的最多 169...

namespace reademail
{
static class Program
{
public static Microsoft.Office.Interop.Outlook.Application myApp;

public static void Main(string[] args)
{
// myApp = new Microsoft.Office.Interop.Outlook.Application();
//myApp.NewMailEx += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailExEventHandler(OutlookNewMailReceived);
ReadMail();
}

static void ReadMail()
{
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;


app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
//ns.Logon(null, null, false, false);

inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
// subFolder = inboxFolder.Folders["Inbox"]; //folder.Folders[1]; also works
Console.WriteLine("Folder Name: {0}, EntryId: {1}", inboxFolder.Name, inboxFolder.EntryID);
Console.WriteLine("Num Items: {0}", inboxFolder.Items.Count.ToString());

//System.IO.StreamWriter strm = new System.IO.StreamWriter("C:/Test/Inbox.txt");
for (int counter = 1; counter <= inboxFolder.Items.Count; counter++)
{
Console.Write(inboxFolder.Items.Count + " " + counter);
item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[counter];
Console.WriteLine("Item: {0}", counter.ToString());
Console.WriteLine("Subject: {0}", item.Subject);
Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
Console.WriteLine("Sendername: {0}", item.SenderName);
Console.WriteLine("Body: {0}", item.Body);
//strm.WriteLine(counter.ToString() + "," + item.Subject + "," + item.SentOn.ToShortDateString() + "," + item.SenderName);

}
//strm.Close();
}


}
}

循环读取多达 169 封电子邮件然后崩溃,它还在似乎是任意日期开始读取电子邮件...我不确定是什么阻止它读取所有电子邮件...

Folder Name: Inbox, EntryId: 000000003527EA8DB4FFC04EB6ABA4DE31CB4BA40100C6D3EBA
DBDB57E438D0B53C5FB515CC50000660627C70000
Num Items: 1048
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObje
ct' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operatio
n failed because the QueryInterface call on the COM component for the interface
with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following er
ror: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERF
ACE)).
at CallSite.Target(Closure , CallSite , Object )
at reademail.Program.ReadMail() in C:\Documents and Settings\DBubel\my docume
nts\visual studio 2010\Projects\reademail\reademail\Program.cs:line 60

按任意键继续。 . .

最佳答案

我的猜测是您的收件箱中有不应用 Microsoft.Office.Interop.Outlook.MailItem 接口(interface)的项目,因此代码在循环并尝试转换时崩溃。如果您使用的是 .NET4,因为它支持动态,一种可能的解决方法是不强制转换对象,而是将其传递给动态变量。


动态项目 = inboxFolder.Items[counter];

这对我有用,因为您的代码在处理收件箱文件夹中的 session 邀请时遇到问题。

完整修改代码:

 namespace reademail
{
static class Program
{
public static Microsoft.Office.Interop.Outlook.Application myApp;

public static void Main(string[] args)
{
// myApp = new Microsoft.Office.Interop.Outlook.Application();
//myApp.NewMailEx += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailExEventHandler(OutlookNewMailReceived);
ReadMail();
}

static void ReadMail()
{
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
//Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;


app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
//ns.Logon(null, null, false, false);

inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
// subFolder = inboxFolder.Folders["Inbox"]; //folder.Folders[1]; also works
Console.WriteLine("Folder Name: {0}, EntryId: {1}", inboxFolder.Name, inboxFolder.EntryID);
Console.WriteLine("Num Items: {0}", inboxFolder.Items.Count.ToString());

//System.IO.StreamWriter strm = new System.IO.StreamWriter("C:/Test/Inbox.txt");
for (int counter = 1; counter <= inboxFolder.Items.Count; counter++)
{
Console.Write(inboxFolder.Items.Count + " " + counter);
dynamic item = inboxFolder.Items[counter];
//item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[counter];
Console.WriteLine("Item: {0}", counter.ToString());
Console.WriteLine("Subject: {0}", item.Subject);
Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
Console.WriteLine("Sendername: {0}", item.SenderName);
Console.WriteLine("Body: {0}", item.Body);
//strm.WriteLine(counter.ToString() + "," + item.Subject + "," + item.SentOn.ToShortDateString() + "," + item.SenderName);

}
//strm.Close();
}
}

}

关于c# - 使用 C# 在 outlook 中解析电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9673476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com