如何使用VB.Net通过Outlook从特定的人检索邮件?

时间:2011-03-16 07:52:46

标签: vb.net extract attachment outlook-2007

我从codeproject.com获得了以下代码:

    Dim objOL As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Dim objFolder As Outlook.Folders
    Dim Item As New Object
    Dim myItems As Outlook.Items
    Dim x As Int16

    objOL = New Outlook.Application()
    objNS = objOL.GetNamespace("MAPI")

    Dim olfolder As Outlook.MAPIFolder
    olfolder = objOL.GetNamespace("MAPI").PickFolder
    myItems = olfolder.Items

    Dim i As Integer
    For x = 1 To myItems.Count
        MessageBox.Show(myItems.Item(x).SenderName)
        MessageBox.Show(myItems.Item(x).SenderEmailAddress)
        MessageBox.Show(myItems.Item(x).Subject)
        MessageBox.Show(myItems.Item(x).Body)
        MessageBox.Show(myItems.Item(x).to)
        MessageBox.Show(myItems.Item(x).ReceivedByName)
        MessageBox.Show(myItems.Item(x).ReceivedOnBehalfOfName)
        MessageBox.Show(myItems.Item(x).ReplyRecipientNames)
        MessageBox.Show(myItems.Item(x).SentOnBehalfOfName)
        MessageBox.Show(myItems.Item(x).CC)
        MessageBox.Show(myItems.Item(x).ReceivedTime)
    Next x
    Dim Atmt As Outlook.Attachment

    For Each Atmt In Item.Attachment
        Dim filename As String = "C:\Email Attachments\" + Atmt.FileName
        Atmt.SaveAsFile(filename)
    Next Atmt

现在,我将默认文件夹设为收件箱。我想要做的是通过仅检索特定人的电子邮件并提取和保存他/她发送的任何附件来扩展功能。此外,当代码到达时,我收到以下错误 Dim Atmt as Outlook.Attachment部分:未找到“对象”类型的公共成员“附件”。我需要此功能来检索附件。我尝试了不同的东西,但没有任何效果。你能帮我吗?

2 个答案:

答案 0 :(得分:1)

更新2

Dim items As New Dictionary(Of String,String)     对于x = 1到myItems.Count         '来自特定人士的邮件         如果myItems.Item(x).SenderEmailAddress =“someone@mail.com”那么             对于每个Atmt As Outlook.Attachment in myItems(x).Attachment

            'A specific type of file
            If Atmt.FileName.Contains("hello") Then items("hello") = Atmt.FileName
            If Atmt.FileName.Contains("hello1") Then items("hello1") = Atmt.FileName
            If Atmt.FileName.Contains("hello2") Then items("hello2") = Atmt.FileName

            Dim filename As String = "C:\Email Attachments\" + Atmt.FileName
            Atmt.SaveAsFile(filename)

        Next Atmt
    End If
Next
For Each item As KeyValuePair(Of String, String) In items
    Console.WriteLine(item.Key & ":" & item.Value)
Next

试试此代码

Outlook.Attachment oAttach = myItems.Attachments[0];

在VB.Net中它就像

Dim oAttach as outlook.Attachment = myItems.Attachments(0);

希望有所帮助

答案 1 :(得分:1)

在您正在查看的codeproject示例代码中,附件位应该在循环内部,因此:

For x = 1 As Integer To myItems.Count
    For Each Atmt As Outlook.Attachment In myItems(x).Attachment
        Dim filename As String = "C:\Email Attachments\" + Atmt.FileName
        Atmt.SaveAsFile(filename)
    Next Atmt
Next