开发者

How to retrieve mail from only a specific person through Outlook using VB.Net?

I got the following code from 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 f开发者_开发百科ilename As String = "C:\Email Attachments\" + Atmt.FileName
        Atmt.SaveAsFile(filename)
    Next Atmt

Now, I got as far as making the default folder the Inbox. What I would like to do is to extend the functionality by retrieving only a specific person's emails and extracting and saving whatever attachments he/she sends. Also, I get the following error when the code reaches the

Dim Atmt as Outlook.Attachment part: Public member 'Attachment' on type 'Object' not found. I need this function to retrieve the attachments. I've tried different things, but nothing's worked. Can you please help me?


Update 2

Dim items As New Dictionary(Of String, String) For x = 1 To myItems.Count 'Mail from a specific person If myItems.Item(x).SenderEmailAddress = "someone@mail.com" Then For Each 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

Try this code

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

In VB.Net it will be like

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

Hope it helps


In the codeproject sample code you're looking at, the attachment bit is supposed to be inside the loop, so:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜