0

I started working with MailKit and have run into an issue where no body is returned for any of the fetched messages. I've tried both a fetch and a search, without luck.

My code is below:

    Private strErrMsg As String
Private objClient As ImapClient
Private objDataTable As DataTable

Private Sub frmEmalTest2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    If objClient IsNot Nothing Then
        If objClient.IsConnected Then
            objClient.Disconnect(True)
            objClient.Dispose()
        End If
    End If
End Sub

Private Sub frmEmalTest2_Load(sender As Object, e As EventArgs) Handles Me.Load
    objDataTable = New DataTable
    With objDataTable.Columns
        .Add("msgdate", Type.GetType("System.String"))
        .Add("sender", Type.GetType("System.String"))
        .Add("subject", Type.GetType("System.String"))
        .Add("msgid", Type.GetType("System.String"))
        .Add("attachments", Type.GetType("System.Int32"))
    End With

    grdMessages.DataSource = objDataTable

End Sub

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    Try
        If txtUserName.Text = "" Then
            Exit Sub
        End If

        If txtPassword.Text = "" Then
            Exit Sub
        End If

        Dim logger = New ProtocolLogger(Console.OpenStandardError())
        objClient = New ImapClient(logger)

        Dim credentials = New NetworkCredential(txtUserName.Text, txtPassword.Text)
        Dim uri = New Uri("imaps://imap.gmail.com")

        With objClient
            .Connect(uri)

            .AuthenticationMechanisms.Remove("XOAUTH2")

            .Authenticate(credentials)
        End With

        lblMsg.Text = "Connected"
    Catch ex As Exception
        strErrMsg = ex.Message
        lblMsg.Text = "Connection failed!"
    End Try
End Sub

Private Sub btnMessages_Click(sender As Object, e As EventArgs) Handles btnMessages.Click
    Dim objRow As DataRow
    Dim objMultipart As BodyPartMultipart
    Dim objBasic As BodyPartBasic
    Dim objMessage As IMessageSummary
    Dim intAttachments As Integer = 0
    Dim objMessages As IList(Of IMessageSummary) = Nothing
    Try
        If Not objClient.IsConnected Then
            Exit Sub
        End If

        objClient.Inbox.Open(FolderAccess.[ReadOnly])

        objMessages = objClient.Inbox.Fetch(0, -1, MessageSummaryItems.All).ToList()

        If objMessages.Count > 0 Then
            lblRecCnt.Text = objMessages.Count.ToString + " message(s)"
        Else
            lblRecCnt.Text = "(no messages)"
        End If

        objDataTable.Rows.Clear()
        If objMessages.Count > 0 Then
            For Each objMessage In objMessages
                intAttachments = 0

                objBasic = TryCast(objMessage.Body, BodyPartBasic)
                objMultipart = TryCast(objMessage.Body, BodyPartMultipart)

                objRow = objDataTable.NewRow
                objRow("msgid") = objMessage.UniqueId
                objRow("msgdate") = objMessage.Date.ToString("M/d/yyyy h:mmtt")
                objRow("subject") = objMessage.Envelope.Subject
                objRow("sender") = objMessage.Envelope.From.Mailboxes(0).Name + " (" + objMessage.Envelope.From.Mailboxes(0).Address + ")"

                If objMultipart Is Nothing Then
                    If objBasic IsNot Nothing AndAlso objBasic.IsAttachment Then
                        intAttachments += 1
                    End If
                Else
                    For Each objItem As BodyPartBasic In objMultipart.BodyParts.OfType(Of BodyPartBasic)()
                        Select Case objItem.ContentType.MediaType
                            Case "APPLICATION", "IMAGE"
                                intAttachments += 1
                        End Select
                    Next objItem
                End If
                objRow("attachments") = intAttachments

                objDataTable.Rows.Add(objRow)

                If objRow("attachments") > 0 Then
                    grdMessages.Rows(objDataTable.Rows.Count - 1).Cells(0).Value = My.Resources.attach
                End If
            Next
        End If

    Catch ex As Exception
        strErrMsg = ex.Message
    End Try
End Sub
jstedfast
  • 35,744
  • 5
  • 97
  • 110
user680891
  • 177
  • 1
  • 2
  • 12
  • What do you mean, exactly, by "no body is returned"? If you wan to use the IsAttachment property, you need to use `MessageSummaryItems.BodyStructure` but `All` is just an alias that contains `Body`, it does not include `BodyStructure`. – jstedfast Sep 03 '15 at 11:41
  • Both the following lines return Nothing objBasic = TryCast(objMessage.Body, BodyPartBasic) objMultipart = TryCast(objMessage.Body, BodyPartMultipart) – user680891 Sep 03 '15 at 19:03

1 Answers1

0

My fault!!

If I change MessageSummaryItems.All to MessageSummaryItems.Full I can see the body. However, it adds about 5 seconds to the fetch time.

user680891
  • 177
  • 1
  • 2
  • 12