【问题标题】:Unable to cast object of type 'Microsoft.Exchange.WebServices.Data.EmailMessage' to type 'Microsoft.Office.Interop.Outlook.MailItem'无法将“Microsoft.Exchange.WebServices.Data.EmailMessage”类型的对象转换为“Microsoft.Office.Interop.Outlook.MailItem”类型
【发布时间】:2017-09-23 09:59:11
【问题描述】:

我的目标是将昨天的邮件复制到我计算机中的外部文件夹中,我在"mailitem = Item" 这一行收到了这个错误。

下面是我的代码:

    Dim service As New ExchangeService(ExchangeVersion.Exchange2010_SP2)
    service.Credentials = New WebCredentials("user", "Password", "domain")
    service.AutodiscoverUrl("acce@pac.com.in")
    Dim pageSize As Integer = 100
    Dim offset As Integer = 0
    Dim view As New ItemView(pageSize, offset)
    Dim returnValue As FindItemsResults(Of Item)
    returnValue = service.FindItems(WellKnownFolderName.Inbox, view)
    Dim subject As String
    Dim receiveDate As Date
    Dim maxDate As Date = getLatestDate()
    For Each Item In returnValue
        receiveDate = Item.DateTimeReceived
        If receiveDate > maxDate Then
            Dim strDirPLE As String
            Dim strDirCust As String
            Dim strSaveName As String
            strDirPLE = "D:\test1\"
            strDirCust = "D:\test2\"
            Dim mailitem As Microsoft.Office.Interop.Outlook.MailItem
            mailitem = Item
            subject = Item.Subject.Replace(":", "")
            subject = Item.Subject.Replace(".", "")
            strSaveName = subject & Format(Now, " ddmmyyyyhhnnss") & ".msg"
             If (mailitem.From.Contains("@pac.com.in")) Then
                If (Item.Subject.Contains("CId") AndAlso Item.Subject.Contains("pId")) Then

                    mailitem.SaveAs(strcon1)
                Else
                    mailitem.SaveAs(strcon2)
                End If
            End If

        End If
        Exit For
    Next
End Sub

错误

无法将“Microsoft.Exchange.WebServices.Data.EmailMessage”类型的对象转换为“Microsoft.Office.Interop.Outlook.MailItem”类型

【问题讨论】:

  • 不相关,但您在 subject 行上有一个小错误。你正在设置它,然后你正在重置它。在第二个.Replace 上,您应该使用subject.Replace(".", "")。你可以把两者放在一条线上
  • 你为什么一直删除你的代码?
  • 我问过你为什么,但你没有回应。我现在可以请您不要破坏您自己的帖子吗?

标签: vb.net exchangewebservices


【解决方案1】:

我建议将其保存为 EML 而不是 MSG。抱歉 vb.net 不是我的第一语言。

Shared Sub ExportMIMEEmail(service As ExchangeService)
    Dim inbox As Folder = Folder.Bind(service, WellKnownFolderName.Inbox)
    Dim view As New ItemView(1)
    view.PropertySet = New PropertySet(BasePropertySet.IdOnly)
    ' This results in a FindItem call to EWS.
    Dim results As FindItemsResults(Of Item) = inbox.FindItems(view)
    For Each item As var In results
        Dim props As New PropertySet(EmailMessageSchema.MimeContent)
        ' This results in a GetItem call to EWS.
        Dim email = EmailMessage.Bind(service, item.Id, props)
        Dim emlFileName As String = "C:\export\email.eml"
        ' Save as .eml.
        Using fs As New FileStream(emlFileName, FileMode.Create, FileAccess.Write)
            fs.Write(email.MimeContent.Content, 0, email.MimeContent.Content.Length)
        End Using
    Next
End Sub

这是从https://msdn.microsoft.com/en-us/library/office/dn672317%28v=exchg.150%29.aspx?f=255&MSPPError=-2147217396修改的

【讨论】:

  • 虽然不是问题,但 Avi 可能希望将 EWS 的便捷事件处理程序作为下一步添加自动化级别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
相关资源
最近更新 更多