VB.Net-收到新电子邮件时执行我的代码

VB.Net-收到新电子邮件时执行我的代码,vb.net,outlook,Vb.net,Outlook,有了VB.Net,我只想在Outlook收件箱中收到新邮件时触发一个事件或事件,即使Outlook关闭,我也需要它来工作。当该事件发生时,我将从收到的电子邮件中获取数据。在工作中,我们使用通常安装在Windows计算机上的Outlook应用程序(而不是web) 现在,我有一个代码,即使在Outlook关闭时也可以读取电子邮件,但我缺少上面的内容,即每次收到新电子邮件时,我都需要我的代码来工作。我有两种方法来做同样的事情(见下面的代码),这两种方法在Outlook关闭时都有效,但当新电子邮件到达并

有了VB.Net,我只想在Outlook收件箱中收到新邮件时触发一个事件或事件,即使Outlook关闭,我也需要它来工作。当该事件发生时,我将从收到的电子邮件中获取数据。在工作中,我们使用通常安装在Windows计算机上的Outlook应用程序(而不是web)

现在,我有一个代码,即使在Outlook关闭时也可以读取电子邮件,但我缺少上面的内容,即每次收到新电子邮件时,我都需要我的代码来工作。我有两种方法来做同样的事情(见下面的代码),这两种方法在Outlook关闭时都有效,但当新电子邮件到达并且Outlook关闭时,我需要执行这两种方法中的一种。 我是新手,所以如果你能简单一点,并提供示例/示例代码。非常感谢你的帮助,谢谢

#1-Microsoft.Exchange.WebServices

        Dim serverURI As New Uri("https://corpmail.....com/ews/exchange.asmx")
        Dim exch As New Microsoft.Exchange.WebServices.Data.ExchangeService()
        exch.Url = serverURI
        exch.UseDefaultCredentials = False
        exch.Credentials = New System.Net.NetworkCredential("username", "password",   
                                                                            "domain")

        Dim iv As ItemView = New ItemView(999)
        iv.Traversal = ItemTraversal.Shallow
        Dim inboxItems As FindItemsResults(Of Item) = Nothing
        inboxItems = exch.FindItems(WellKnownFolderName.Inbox, iv)

        For Each i As Item In inboxItems
            'Console.WriteLine(i.Subject)
            MsgBox("this is" & i.Subject)
            Exit For
        Next
#2

Try
            ' Create a folder named "inbox" under current directory
            ' to save the email retrieved.
            Dim localInbox As String = String.Format("{0}\inbox",      
                                                   Directory.GetCurrentDirectory())


            ' If the folder does not exist, create it.
            If Not Directory.Exists(localInbox) Then
                Directory.CreateDirectory(localInbox)
            End If


            Dim oServer As New MailServer("corpmail......com",
               "user", "password", ServerProtocol.Pop3)

            oServer.SSLConnection = True
            oServer.Port = 995

            Console.WriteLine("Connecting server ...")


            Dim oClient As New MailClient("TryIt")
            oClient.Connect(oServer)


            ' retrieve unread/new email only
            oClient.GetMailInfosParam.Reset()
            oClient.GetMailInfosParam.GetMailInfosOptions = 
                                                      GetMailInfosOptionType.NewOnly


            Dim infos As MailInfo() = oClient.GetMailInfos()
            Console.WriteLine("Total {0} unread email(s)", infos.Length)


            For i As Integer = infos.Length - 1 To infos.Length - 1
                Dim info As MailInfo = infos(i)
                Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}",
                        info.Index, info.Size, info.UIDL)


                ' Retrieve email from IMAP server
                Dim oMail As Mail = oClient.GetMail(info)


                Console.WriteLine("From: {0}", oMail.From.ToString())
                Console.WriteLine("Subject: {0}" & vbCr & vbLf, oMail.Subject)


                ' Generate an unique email file name based on date time.
                Dim fileName As String = _generateFileName(i + 1)
                Dim fullPath As String = String.Format("{0}\{1}", localInbox, 
                                                                            fileName)


                ' Save email to local disk
                oMail.SaveAs(fullPath, True)


            'mark unread email as read, next time this email won't be retrieved again
                If Not info.Read Then
                    oClient.MarkAsRead(info, True)
                End If

            Next


            ' Quit and expunge emails marked as deleted from IMAP server.
            oClient.Quit()
            Console.WriteLine("Completed!")

        Catch ep As Exception
            Console.WriteLine(ep.Message)
        End Try

你得写点东西来调查邮箱。您可以将其创建为服务,也可以创建作为计划任务每X分钟运行一次的常规exe。将其作为服务运行的问题是,它与任何特定的用户都没有关联,因此围绕用户和凭据的问题,您将有更多的工作要做。您需要确保在轮询时使用IMAP。我做了一个快速搜索,看起来有一些库可以提供帮助,所以我认为这是可以做到的。请确保您的凭据安全。我刚刚仔细查看了您的代码-您已经完成了75%的工作!看起来你所需要做的就是把你的工作放到一个文件夹中,然后按你需要的时间间隔运行。我本来想让应用程序保持运行,但在Outlook关闭的情况下收到新电子邮件时,我需要一些东西来捕捉。我找到了一种处理事件的方法,但只有在Outlook打开的情况下才有效。要总结,关闭Outlook,捕获新电子邮件,运行上面的代码#1对于任务计划程序,我必须使用控制台应用程序,还是它也可以在VB应用程序的表单加载上工作?