我需要制作一个 VB .net 工具来阅读电子邮件和保存附件。我的公司最近从本地 Exchange 迁移到 Office 365。我已经阅读了 2 天的 EWS 教程,并搜索了 StackOverflow,但无法通过授权访问 O365 邮箱的最基本步骤。
我从这篇文章中获取了原始代码:htp://www.c-sharpcorner.com/UploadFile/jj12345678910/reading-email-and-attachment-from-microsoft-exchange-server/。我在使用 Telerik 转换器将其转换为 VB 时遇到了一些麻烦,但我认为我做对了。每次我尝试使用 FindItemsResults 方法时,它都会以“(401) Unauthorized”停止。
提出问题的说明指出我应该包含指向我已经找到的内容以及为什么它不起作用的链接,但我的 SO 声誉只允许我 2 个链接。这是我尝试过的:
阅读此页面后,我已经尝试了所有可能的用户代码和域组合:htps://stackoverflow.com/questions/10107872/ews-connections-issues-401-unauthorized
我正在尝试阅读自己的邮箱,所以这个没有帮助:htps://stackoverflow.com/questions/43346498/401-unauthorized-access-when-using-ews-to-connect-to-mailbox
我的连接看起来与此页面上的连接相同,但在我的项目中使用它并没有通过与以前相同的未经授权的错误:htps://stackoverflow.com/questions/29009295/ews-managed-api-retrieving-e-来自office365-exchange-server 的邮件
这是我的代码:
Public Class Form1
Public Exchange As ExchangeService
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstMsg.Clear()
lstMsg.View = View.Details
lstMsg.Columns.Add("Date", 150)
lstMsg.Columns.Add("From", 250)
lstMsg.Columns.Add("Subject", 400)
lstMsg.Columns.Add("Has Attachment", 50)
lstMsg.Columns.Add("Id", 100)
lstMsg.FullRowSelect = True
End Sub
Public Sub ConnectToExchangeServer()
Me.lblMsg.Text = "Connecting to Exchange Server"
lblMsg.Refresh()
Try
'Exchange = New ExchangeService(ExchangeVersion.Exchange2013)
Exchange = New ExchangeService()
Exchange.TraceEnabled = True
'Exchange.UseDefaultCredentials = True
Exchange.Credentials = New WebCredentials("DoeJohn", "mypasswd", "mycorp.com")
'Exchange.AutodiscoverUrl("DoeJohn@mycorp.mail.onmicrosoft.com", AddressOf MyRedirectionURLValidationCallback)
'Exchange.AutodiscoverUrl("John.Doe@mycorp.com", AddressOf MyRedirectionURLValidationCallback)
Exchange.Url = New System.Uri("https://outlook.office365.com/ews/exchange.asmx")
lblMsg.Text = "Connected to Exchange Server"
lblMsg.Refresh()
Catch ex As Exception
MsgBox("Fatal Error in Connect: " & ex.Message)
End
End Try
End Sub
Public Function MyRedirectionURLValidationCallback(RedirectionURL As String) As Boolean
Dim Result As Boolean = False
Dim RedirectionURI As Uri = New Uri(RedirectionURL)
If RedirectionURI.Scheme = "https" Then
Return True
End If
Return False
End Function
Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
Call ConnectToExchangeServer()
Dim ts As TimeSpan = New TimeSpan(0, -1, 0, 0)
Dim MyDate As DateTime = DateTime.Now.Add(ts)
Dim MyFilter As SearchFilter.IsGreaterThanOrEqualTo = New SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, MyDate)
If Exchange IsNot Nothing Then
Dim FindResults As FindItemsResults(Of Item) =
Exchange.FindItems(WellKnownFolderName.Inbox, MyFilter, New ItemView(50))
Dim NewRow As ListViewItem
For Each MyItem As Item In FindResults
Dim Message As EmailMessage = EmailMessage.Bind(Exchange, MyItem.Id)
NewRow = New ListViewItem(Message.DateTimeReceived.ToString())
NewRow.SubItems.Add(Message.From.Name.ToString())
NewRow.SubItems.Add(Message.Subject)
NewRow.SubItems.Add(Message.HasAttachments.ToString())
NewRow.SubItems.Add(Message.Id.ToString())
lstMsg.Items.Add(NewRow)
Next
Else
End If
End Sub
与 Outlook 中的 Test Email AutoConfiguration 相比,我已确认 AutoDiscover 正确地找到了服务器。
一个有趣的旁注——在我的公司迁移到 Office 365 之后,我注意到我有两个新的 SMTP 邮件地址。如果我自己打开 Outlook 属性,我会看到:
这意味着现在有人可以通过旧地址 john.doe@mycorp.com 和现在的 doejohn@mycorp.mail.onmicrosoft.com 向我发送邮件。新地址基于我的域用户代码。我从 gmail 帐户测试了 microsoft 并且它可以工作。
总而言之,这是我的问题: 1. 当我尝试阅读收件箱时,为什么会收到 (401) Unauthorized 错误?2. Office 365 是否希望我使用我的域帐户或邮箱名称作为用户凭据?3. 在 WebCredentials 声明的域部分,我是使用公司的 mycorp.com 还是使用 Office 365 的域 outlook.office365.com?
如果你已经读到这里,非常感谢!