使用C#阅读Outlook邮件 [英] Reading Outlook Mail with C#
问题描述
当我尝试连接到Outlook邮件时,我正在使用以下代码.现在,我一定做错了,因为我尝试获取收件箱邮件,而我总是收到0封邮件(不是这种情况).这是我的代码
I am using the following code as I attempt to connect to my Outlook mail. Now, I must be doing something wrong because I try to get the inbox mails and I always get 0 mails (when this is not the case). This is my code
Microsoft.Office.Interop.Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
nameSpace.Logon("", "", Missing.Value, Missing.Value);
inboxFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Console.WriteLine("Folders: {0}", inboxFolder.Folders.Count);
我的Outlook配置文件中有几个电子邮件帐户.当我写以下内容
I have several email accounts in my Outlook profile. When I write the following
Console.WriteLine("Accounts: {0}",nameSpace.Accounts.Count);
Console.WriteLine("Name: {0}", nameSpace.Accounts[1].DisplayName);
帐户总数正确显示,我真正想要访问的帐户名称也正确显示(索引1).现在,问题是我需要访问该帐户中的特定文件夹.我该怎么做?
The total number of accounts is displayed correctly, and so is the name of the account i really want to access (index 1). Now, the problem is that I need to access a specific folder within that account. How do I do this?
推荐答案
我可以解决这个问题!实际上,这很容易.这是我如何访问所需文件夹的方法:
I could solve this! It was quite easy actually. Here is how I could access the desired folder:
// my-account@myserver.com is the name of my account
// Unsent mails is the name of the folder I wanted to access
inboxFolder = nameSpace.Folders["my-account@myserver.com"].Folders["Unsent mails"];
foreach (Microsoft.Office.Interop.Outlook.MailItem mailItem in inboxFolder.Items)
{
if (mailItem.UnRead) // I only process the mail if unread
{
Console.WriteLine("Accounts: {0}", mailItem.Body);
}
}
这篇关于使用C#阅读Outlook邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!