C# -- 从 Outlook 中的非默认帐户读取电子邮件 [英] C# -- Read Emails from non default accounts in Outlook

查看:46
本文介绍了C# -- 从 Outlook 中的非默认帐户读取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C#(不是我的默认收件箱)简单地读取和打印 Outlook 中特定文件夹和另一个收件箱中的电子邮件内容.我发现很难在网上找到这样的例子,而且我自己也失败了.我也知道如何打印默认帐户的电子邮件,而不是其他帐户.

I am trying to simply read and print the contents of emails from a specific folder and another inbox in Outlook using C# (not my default inbox). I'm finding it difficult to find examples of this on the web and have failed on my own. I know how to print the emails of the default account as well, just not additional ones.

我在这里的代码只是遍历所有收件箱的列表并打印出它们的名称.我想阅读的是集合中的第一个元素.我感谢您对此问题的任何帮助.谢谢.

My code here simply iterates over a list of all the inboxes and prints their names out. The one I want to read is the first element in the collection. I appreciate any help with this issue. Thanks.

using System;
using System.Collections;
using Microsoft.Office.Interop.Outlook;

public class StorageReplies {

    public static void Main() {
        Application app = new Microsoft.Office.Interop.Outlook.Application(); 
        _NameSpace ns = app.GetNamespace("MAPI");

        Folders folders = ns.Folders;

        foreach(MAPIFolder f in folders) {
            Console.WriteLine(f.Name);
        }
    }
}

推荐答案

您可以获取每个文件夹的 Store,然后调用 GetDefaultFolder 获取对应商店的收件箱文件夹的方法如下:

You could obtain the Store for each folder, and then call GetDefaultFolder method to obtain the inbox folder for the corresponding store like this:

foreach (MAPIFolder f in folders)
{
    MAPIFolder inbox_folder = f.Store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

    foreach (MailItem item in inbox_folder.Items)
    {
        //Access item here
    }
}

然而,与其这样做,不如循环访问Stores 属性直接像这样:

However, instead of doing that, it makes sense to loop through the Stores property directly like this:

Stores stores = ns.Stores;

foreach (Store store in stores)
{
    MAPIFolder inbox_folder = store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

    foreach (MailItem item in inbox_folder.Items)
    {
        //Access item here
    }                
}

这篇关于C# -- 从 Outlook 中的非默认帐户读取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆