需要在收件箱中查找特定的邮件,保存附件以共享,然后将电子邮件移动到其他文件夹VB.Net

时间:2019-05-22 13:27:42

标签: vb.net outlook visual-studio-2017 console-application move

我可以连接到Outlook帐户,可以找到我的消息,可以保存附件以共享,只是无法弄清楚保存附件后如何移动到其他文件夹。一旦一切正常,将从SQL表传递要查找的电子邮件详细信息和帐户详细信息,并循环处理它们。

Imports System.IO
Imports Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Data.SqlClient
Imports System.Diagnostics.Tracing
Imports System.Net.Mail

Module Module1

Sub Main()
    Dim outlookApp As New Outlook.Application()

    Dim outlookSession As Outlook.NameSpace = outlookApp.Session
    outlookSession.Logon("UserID", "password", True, True)
    Console.WriteLine(outlookSession.CurrentUser)

Dim inboxFolder As Outlook.MAPIFolder = outlookSession.GetDefaultFolder (Outlook.OlDefaultFolders.olFolderInbox)
    Dim Subfolder As Outlook.MAPIFolder 'Tried it also just like the  inbox folder no difference
Subfolder = inboxFolder.Folders("Move to folder name")' Errors here operation failed, object could not be found


    Dim inboxItems As Outlook.Items = inboxFolder.Items
    For Each outlookItem As Object In inboxItems
        Dim mailItem As Outlook._MailItem = TryCast(outlookItem, Outlook._MailItem)
        If mailItem IsNot Nothing Then
            'Console.WriteLine(mailItem.Subject)
            If mailItem.SenderEmailAddress = "Reporting@successfactors.com" Then
                Dim attachments As Outlook.Attachments = mailItem.Attachments
                For Each attachment As Outlook.Attachment In attachments
                    'Just to test
                    Console.WriteLine("From " & mailItem.SenderEmailAddress)
                    Console.WriteLine("Subject " & mailItem.Subject)
                    Console.WriteLine("Attachment  " & attachment.FileName)
                    Console.WriteLine(inboxFolder.Items)
                    'Attachment does get saved
                    attachment.SaveAsFile("\\fileshare\folder\" & attachment.FileName)



                    Marshal.ReleaseComObject(attachment)
                Next attachment
                mailItem.Move(Subfolder) 'doesn't move
                'Subfolder = GetObject.Folders("DailyDataFeed1")
                'mailItem.Move(DestFldr:=Subfolder)
                Marshal.ReleaseComObject(attachments)
            End If




        End If

        Marshal.ReleaseComObject(outlookItem)
    Next outlookItem

    Marshal.ReleaseComObject(inboxItems)
    Marshal.ReleaseComObject(inboxFolder)
    Marshal.ReleaseComObject(outlookSession)
End Sub

最终模块

我希望已处理的邮件将被移至子文件夹

0 个答案:

没有答案
相关问题