【问题标题】:How to retrieve mail from only a specific person through Outlook using VB.Net?如何使用 VB.Net 通过 Outlook 仅从特定人员那里检索邮件?
【发布时间】:2011-03-16 07:52:46
【问题描述】:

我从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 部分时,我收到以下错误:找不到类型“对象”的公共成员“附件”。我需要此函数来检索附件。我尝试了不同的东西,但没有任何效果。你能帮帮我吗?

【问题讨论】:

  • 您得到的错误是因为您将项目创建为新对象并且您没有为其分配任何内容。

标签: vb.net extract attachment outlook-2007


【解决方案1】:

更新 2

Dim items As New Dictionary(Of String, String) 对于 x = 1 到 myItems.Count '来自特定人的邮件 如果 myItems.Item(x).SenderEmailAddress = "someone@mail.com" 那么 对于 myItems(x).Attachment 中的每个 Atmt 作为 Outlook.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);

希望对你有帮助

【讨论】:

  • @vs dev, @ho1:你们俩都是对的! myItems(x).Attachments 工作!混合您的答案有效。非常感谢! :)
  • @hvs dev:我如何检索特定发件人的邮件和附件?
  • 您可以随时检查 SenderEmailAddress 属性对吗?将 if 条件放入循环中,仅当发件人名称与您提供的名称匹配时才进行附件提取。
  • @vs dev:在通过 VB/.Net 使用 Outlook 时,我是个菜鸟。谢谢你让我知道这个属性。你能给我语法和正确的用法吗?另外,我怎样才能从这个特定的人那里只获取特定类型的文件?
  • @vs dev: 非常感谢!!:) 完美!谢谢! :)
【解决方案2】:

在您正在查看的 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

【讨论】:

  • 如何检索特定发件人的邮件和附件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多