如何解决VB.Net-即使关闭Outlook也可以阅读电子邮件
现在我有下面的代码,即使关闭Outlook也可以执行。我需要的是能够在每次收到新电子邮件(Outlook关闭)时运行此代码。我将进行哪些补充/更改?任何示例代码?谢谢
Dim serverURI As New Uri("https://.....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
MsgBox("this is" & i.Subject)
Exit For
Next
解决方法
您可以参考以下代码:
Module Module1
Sub Main()
Dim oApp As Outlook.Application = Nothing
oApp = New Outlook.Application()
Dim [nameSpace] As Outlook.[NameSpace] = oApp.GetNamespace("MAPI")
Dim items As Outlook.Items = Nothing
Try
[nameSpace].Logon("","",False,Missing.Value)
Dim folder = [nameSpace].Folders("xxx@xx.com").Folders("Inbox")
items = folder.Items
AddHandler items.ItemAdd,AddressOf Items_ItemAdd
Console.WriteLine("yes")
Catch __unusedException1__ As Exception
[nameSpace].Logon("",True,True)
End Try
Console.ReadKey()
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
Console.WriteLine("success")
Dim myOlMItem As Outlook.MailItem
myOlMItem = CType(Item,Outlook.MailItem)
Console.WriteLine(myOlMItem.Subject)
End Sub
End Module
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。