vb.net 从 Outlook 子文件夹读取电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13376831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:07:42  来源:igfitidea点击:

Read emails from Outlook subfolder

vb.netoutlook

提问by Chris

How can I read mails from an Outlook subfolder? I can read from Inbox but I have no idea how to read from a folder that I have created in Inbox.

如何阅读 Outlook 子文件夹中的邮件?我可以从收件箱中读取,但我不知道如何从我在收件箱中创建的文件夹中读取。

Here my simple code for console application:

这是我的控制台应用程序的简单代码:

Sub Main()

    Dim otkApp As Outlook.Application = New Outlook.Application
    Dim otkMailItem = "IPM.Note"
    Dim otkNameSpace As Outlook.NameSpace = otkApp.GetNamespace("MAPI")
    Dim otkInboxFolder As Outlook.MAPIFolder = otkNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
    Dim otkMailItems As Outlook.Items = otkInboxFolder.Items
    Dim otkMessage As Outlook.MailItem
    Dim iCntr As Integer

    MsgBox(otkMailItems.Count)
    For iCntr = 1 To otkMailItems.Count
        If otkMailItems.Item(iCntr).MessageClass = otkMailItem Then
            otkMessage = otkMailItems.Item(iCntr)

            Console.WriteLine(iCntr)
            Console.WriteLine(otkMessage.SenderName)
            Console.WriteLine(otkMessage.Subject)
            Console.WriteLine(otkMessage.ReceivedTime)
            Console.WriteLine(otkMessage.Body)
            Console.WriteLine("______________________________")
        End If
    Next

    otkApp = Nothing
    otkNameSpace = Nothing
    otkMailItems = Nothing
    otkMessage = Nothing
End Sub

I think this line should be changed:

我认为这一行应该改变:

Dim otkInboxFolder As Outlook.MAPIFolder = otkNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

my folder from that I want to read the mails is:

我想阅读邮件的文件夹是:

Inbox -> domain.com -> [email protected]

采纳答案by vasa

you can use the Outlook.NameSpace.Folders("folder_name") property. every time you call this property it will return a MAPIFolder which also have the Folders property so you can use it if you want to access nested folder

您可以使用 Outlook.NameSpace.Folders("folder_name") 属性。每次调用此属性时,它都会返回一个 MAPIFolder,它也具有 Folders 属性,因此如果您想访问嵌套文件夹,则可以使用它

for example, let's say you want to access the folder named "Inbox" which is inside the folder "domain.com" which is inside the folder "[email protected]" you will be using the following:

例如,假设您要访问名为“收件箱”的文件夹,该文件夹位于文件夹“[email protected]”内的文件夹“domain.com”内,您将使用以下内容:

Dim otkInboxFolder As Outlook.MAPIFolder = otkNameSpace.Folders("[email protected]").Folders("domain.com").Folders("Inbox")

回答by Dmitry Streblechenko

If it is a subfolder of the Inbox folder, you can use the MAPIFolder.Folders collection to get to the child subfolders.

如果它是收件箱文件夹的子文件夹,您可以使用 MAPIFolder.Folders 集合来访问子子文件夹。

Dim otkInboxFolder As Outlook.MAPIFolder = otkNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim SubFolder = otkInboxFolder.Folders.Item*"TheSubfolderName")
Dim otkMailItems As Outlook.Items = SubFolder .Items