私は作業を開始しましMailKit
たが、フェッチされたメッセージの本文が返されないという問題に遭遇しました。fetch
aと a の両方を試しましたがsearch
、うまくいきませんでした。
私のコードは以下の通りです:
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