通过 C# 访问 Outlook - 检查邮件发件人而非主题时出现 COMException?
Accessing Outlook via C# - COMException when checking a message's sender but not the subject?
我正在使用 Visual Studio 和 Outlook 2010。我可以连接到本地 Outlook 默认配置文件并列出收件箱中的邮件,但我只能获取 SentOn、Subject 和 EntryID 属性.一旦我尝试访问 SenderName 或 Body(以及其他一些),我就会收到以下异常:
System.Runtime.InteropServices.COMException was unhandled
HResult=-2147467260
Message=Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
Source=""
ErrorCode=-2147467260
StackTrace:
at Microsoft.Office.Interop.Outlook._MailItem.get_SenderName()
at reademail.Program.ReadMail() in h:\my documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 54
at reademail.Program.Main(String[] args) in h:\my documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
COMException 非常模糊,令人抓狂。我搜索了又搜索,找不到任何有用的东西。我试过延迟系统线程,我也试过每个项目的 MailItem.GetInspector
技巧。
我在公司环境中工作,我在我的机器上没有本地管理员权限,使用该软件的个人也没有。获得本地管理员会很麻烦,所以我想在开始这个过程之前真正确定这是问题所在。
这是产生错误的最少代码。如果有任何帮助,我将不胜感激!
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace MinimalExample
{
class Program
{
static void Main(string[] args)
{
//Correctly triggers my local Outlook to open, allowing me to select the desired profile
Outlook.Application app = new Outlook.Application();
Outlook._NameSpace ns = app.GetNamespace("MAPI");
//Correctly opens the Inbox and reports the correct stats
Outlook.MAPIFolder inboxFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Console.WriteLine("Folder Name: {0}", inboxFolder.Name);
Console.WriteLine("Num Items: {0}", inboxFolder.Items.Count.ToString());
for (int counter = 1; counter <= inboxFolder.Items.Count; counter++)
{
//This cast ensures you only deal with valid MailItems and not calendar issues
Outlook.MailItem item = inboxFolder.Items[counter] as Outlook.MailItem;
if (item != null)
{
//The following works fine
Console.WriteLine("Item: {0}", counter.ToString());
Console.WriteLine("EntryID: {0}", item.EntryID);
Console.WriteLine("Subject: {0}", item.Subject);
Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
//Enabling any of the following two lines causes the E_ABORT COMException
//Console.WriteLine("Sendername: {0}", item.SenderName);
Console.WriteLine("Body: {0}", item.Body);
}
}
Console.ReadLine();
}
}
}
编辑:我最后做了什么
将软件作为插件来回避安全问题,但这远非理想。我无法相信没有本地管理员访问权限,.NET 无法完成此类工作。如果有人知道在没有 本地管理员的情况下作为独立控制台应用程序 完成此操作的方法,我很想听听。非常感谢!
这是具有相同异常的另一类问题。
有这样的回答:
“......换句话说,如果你想完全访问 Outlook 对象模型,以便像 Send 和 SaveAs 这样的方法不受阻碍地工作,那就是你需要使用的 Outlook.Applicatiom 对象。”
这是另一个具有相同错误代码的错误代码:
urlDownloadToFile error 2147467260 in VB6
我认为这完全取决于您的帐户权限。
我正在使用 Visual Studio 和 Outlook 2010。我可以连接到本地 Outlook 默认配置文件并列出收件箱中的邮件,但我只能获取 SentOn、Subject 和 EntryID 属性.一旦我尝试访问 SenderName 或 Body(以及其他一些),我就会收到以下异常:
System.Runtime.InteropServices.COMException was unhandled
HResult=-2147467260
Message=Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
Source=""
ErrorCode=-2147467260
StackTrace:
at Microsoft.Office.Interop.Outlook._MailItem.get_SenderName()
at reademail.Program.ReadMail() in h:\my documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 54
at reademail.Program.Main(String[] args) in h:\my documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
COMException 非常模糊,令人抓狂。我搜索了又搜索,找不到任何有用的东西。我试过延迟系统线程,我也试过每个项目的 MailItem.GetInspector
技巧。
我在公司环境中工作,我在我的机器上没有本地管理员权限,使用该软件的个人也没有。获得本地管理员会很麻烦,所以我想在开始这个过程之前真正确定这是问题所在。
这是产生错误的最少代码。如果有任何帮助,我将不胜感激!
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace MinimalExample
{
class Program
{
static void Main(string[] args)
{
//Correctly triggers my local Outlook to open, allowing me to select the desired profile
Outlook.Application app = new Outlook.Application();
Outlook._NameSpace ns = app.GetNamespace("MAPI");
//Correctly opens the Inbox and reports the correct stats
Outlook.MAPIFolder inboxFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Console.WriteLine("Folder Name: {0}", inboxFolder.Name);
Console.WriteLine("Num Items: {0}", inboxFolder.Items.Count.ToString());
for (int counter = 1; counter <= inboxFolder.Items.Count; counter++)
{
//This cast ensures you only deal with valid MailItems and not calendar issues
Outlook.MailItem item = inboxFolder.Items[counter] as Outlook.MailItem;
if (item != null)
{
//The following works fine
Console.WriteLine("Item: {0}", counter.ToString());
Console.WriteLine("EntryID: {0}", item.EntryID);
Console.WriteLine("Subject: {0}", item.Subject);
Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
//Enabling any of the following two lines causes the E_ABORT COMException
//Console.WriteLine("Sendername: {0}", item.SenderName);
Console.WriteLine("Body: {0}", item.Body);
}
}
Console.ReadLine();
}
}
}
编辑:我最后做了什么
将软件作为插件来回避安全问题,但这远非理想。我无法相信没有本地管理员访问权限,.NET 无法完成此类工作。如果有人知道在没有 本地管理员的情况下作为独立控制台应用程序 完成此操作的方法,我很想听听。非常感谢!
这是具有相同异常的另一类问题。
有这样的回答: “......换句话说,如果你想完全访问 Outlook 对象模型,以便像 Send 和 SaveAs 这样的方法不受阻碍地工作,那就是你需要使用的 Outlook.Applicatiom 对象。”
这是另一个具有相同错误代码的错误代码:
urlDownloadToFile error 2147467260 in VB6
我认为这完全取决于您的帐户权限。