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

发布于 2024-10-22 02:09:13 字数 1600 浏览 6 评论 0原文

我从 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 部分:未找到类型“对象”上的公共成员“附件”。我需要此函数来检索附件。我尝试过不同的方法,但没有任何效果。你能帮我吗?

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 filename 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆 2024-10-29 02:09:13

更新 2 个

暗淡项目作为新字典(字符串、字符串)
对于 x = 1 到 myItems.Count
'来自特定人的邮件
如果 myItems.Item(x).SenderEmailAddress = "[电子邮件受保护]"然后
对于每个 Atmt 作为 Outlook.Attachment 在 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);

希望它有帮助

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 = "[email protected]" 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

眼眸里的那抹悲凉 2024-10-29 02:09:13

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文