Exchange EWS获取电子邮件正文

时间:2015-04-23 18:45:07

标签: sql vb.net exchangewebservices exchange-server-2010

我无法从Exchange邮箱和SQL Server中获取邮件正文。我是否错误地装入了物品?

来自|到|正在处理电子邮件中的主题字段并进入SQL Server。

感谢任何帮助。

Dim service As New ExchangeService(ExchangeVersion.Exchange2010_SP1)
    service.Credentials = New WebCredentials("Jim@Test.com", "jimpassword")
    service.AutodiscoverUrl("jim@dTest.com")

    Dim View As New ItemView(1000)
    View.OrderBy.Add(EmailMessageSchema.DateTimeReceived, SortDirection.Descending)
    Dim Inbox As Folder = Folder.Bind(service, WellKnownFolderName.Inbox)

    Dim findResults As FindItemsResults(Of Item) = service.FindItems(WellKnownFolderName.Inbox, New ItemView(1000))

    Dim emailPropSet As New PropertySet()
    emailPropSet.RequestedBodyType = BodyType.Text
    emailPropSet.BasePropertySet = BasePropertySet.FirstClassProperties

    If findResults.Count > 0 Then
        service.LoadPropertiesForItems(findResults.Items, New PropertySet(BasePropertySet.FirstClassProperties))
    End If

    For Each item As Item In findResults.Items

        Dim CurrentEmail As EmailMessage = item.Bind(service, item.Id, emailPropSet)
        CurrentEmail.Load(emailPropSet)


        '#### Grab Email Information
        Dim E_Body As String = CurrentEmail.Body.Text


        Dim ConnectionString As String
        Dim cnn As SqlConnection
        Dim cmd As New SqlCommand

        ConnectionString = "Data Source=localsql;Initial Catalog=TESTDB;Persist Security Info=True;User ID=sa;Password=Password123"
        cnn = New SqlConnection(ConnectionString)

        Try
            cnn.Open()
            MsgBox("Connection is Open ! ")
            cmd.Connection = cnn
            cmd.CommandText = "INSERT INTO Emails ([MessageID], [From], [To], [CC], [Subject], [Body], [Received], [Sent]) VALUES ('" & E_ID & "', '" & E_From & "', '" & E_To & "', '" & E_ID & "', '" & E_Subject & "', '" & E_Body & "', '" & E_ID & "', '" & E_ID & "')"
            cmd.ExecuteNonQuery()
            MsgBox("Data is Successfully Inserted! ")
            cnn.Close()

        Catch ex As System.Data.SqlClient.SqlException

        Finally

        End Try
    Next

0 个答案:

没有答案