mailkit

MailKit IMAP - return the email date, the email size and then a method to delete


I have the following that downloads mail...

Using vClient As New ImapClient(New ProtocolLogger("C:\Temp\imap3.log"))
vClient.Connect("outlook.office365.com", 993, SecureSocketOptions.SslOnConnect)
vClient.Authenticate(vT2)
vClient.Inbox.Open(FolderAccess.ReadWrite)
vNumber = vClient.Inbox.Count
If vNumber > 0 Then

    Using wr As New StreamWriter("C:\Temp\EmailMessages.txt", True)
        ' vClient.Inbox.Open(FolderAccess.ReadOnly)
        Dim UIDS = vClient.Inbox.Search(SearchQuery.All)
        Dim vItems = vClient.Inbox.Fetch(UIDS, MessageSummaryItems.InternalDate Or MessageSummaryItems.Flags)
        For i As Integer = 0 To UIDS.Count - 1
            Dim vAttachments As Integer = 0
            Dim Message = vClient.Inbox.GetMessage(i)
            Dim vSubject As String = Message.Subject
            If vSubject = "" Then
                vSubject = "No Subject"
            End If
            Dim vBody As String = Message.HtmlBody
            If vBody = "" Then
                vBody = "No message was included"
            End If
            Dim vSender As String = Message.From.Mailboxes.FirstOrDefault().Address
            Dim vSenderName As String = Message.From.Mailboxes.FirstOrDefault().Name

            Dim vSize As String = MessageSummaryItems.Size
            Dim vTempDate As String = MessageSummaryItems.InternalDate
            Message.WriteTo(String.Format("{0}.eml", i))
            vMessage = Message.ToString
            ' wr.WriteLine(vMessage)
            Dim vInboxID As Integer = 0
            strSQL = "INSERT INTO Mail_Inbox (Inbox_From, Inbox_Subject, Inbox_Body, Inbox_SenderName, Inbox_Size, Inbox_TempDate, Inbox_Content) VALUES ("
            strSQL += "'" & vSender & "', "
            strSQL += "'" & SubmitText(vSubject) & "', "
            strSQL += "'" & SubmitHTML(vBody) & "',"
            strSQL += "'" & SubmitText(vSenderName) & "', "
            strSQL += "'" & vSize & "', "
            strSQL += "'" & SubmitText(vTempDate) & "', "
            strSQL += "'" & SubmitHTML(vMessage) & "')"
            vInboxID = InsertDataReturnID_Data(strSQL, 0, "238", True)

            For Each attachment As MimeEntity In Message.Attachments
                Dim vFilename = If(attachment.ContentDisposition?.FileName, attachment.ContentType.Name)
                Dim vByte() As Byte = Nothing
                Using vStream As New MemoryStream
                    If TypeOf attachment Is MessagePart Then
                        Dim rfc822 = CType(attachment, MessagePart)
                        rfc822.Message.WriteTo(vStream)
                        vByte = vStream.ToArray

                    Else
                        Dim vPart = CType(attachment, MimePart)
                        vPart.Content.DecodeTo(vStream)
                        vByte = vStream.ToArray
                    End If
                    If InsertEmailAttachment(vByte, vInboxID, vFilename) = False Then
                        'There was an error here
                    End If
                End Using
                vAttachments += 1
            Next
            If vAttachments > 0 Then
                strSQL = "UPDATE Mail_Inbox SET Flag_Attachment = '" & vAttachments & "' WHERE Inbox_ID = " & vInboxID
                InsertData_Data(strSQL, 0, "278", True)
            End If

            ' vClient.Inbox.AddFlags(UniqueId, MessageFlags.Deleted, True)

        Next

    End Using
End If
vClient.Disconnect(True)
End Using

Ignore the StreamWriter (just used initially to test, it now writes to a DB)

There are three items I am trying to format. The date of the email, the size of the email and a method to delete everything on the server once they have been downloaded.

I can see that the dates are being fetched...

C: A00000007 UID FETCH 2,5,8,12,15,37,40 (FLAGS INTERNALDATE)
S: * 1 FETCH (FLAGS () INTERNALDATE "01-Feb-2024 08:16:36 +0000" UID 2)
S: * 2 FETCH (FLAGS () INTERNALDATE "01-Feb-2024 15:15:02 +0000" UID 5)
S: * 3 FETCH (FLAGS () INTERNALDATE "01-Feb-2024 16:44:16 +0000" UID 8)
S: * 4 FETCH (FLAGS () INTERNALDATE "02-Feb-2024 07:57:12 +0000" UID 12)
S: * 5 FETCH (FLAGS () INTERNALDATE "24-Feb-2024 13:53:39 +0000" UID 15)
S: * 6 FETCH (FLAGS () INTERNALDATE "25-Feb-2024 08:02:54 +0000" UID 37)
S: * 7 FETCH (FLAGS () INTERNALDATE "25-Feb-2024 13:48:07 +0000" UID 40)
S: A00000007 OK FETCH completed.

But the data I am getting for all the records is inbox_size = 64 and Inbox_Date (as string) = 32

I tried a few variations of Inbox.AddFlags(UniqueId, MessageFlags.Deleted, True) to delete all the records once they are downloaded but can't get the syntax right.

Other than that, everything else works.

Any pointers would be appreciated.


Solution

  • Change this:

    Dim Message = vClient.Inbox.GetMessage(i)
    

    to this:

    Dim Message = vClient.Inbox.GetMessage(vItems[i].UniqueId)
    

    Then, to get strings representing the size and date, change these lines:

    Dim vSize As String = MessageSummaryItems.Size
    Dim vTempDate As String = MessageSummaryItems.InternalDate
    

    to this:

    Dim vSize As String = vItems[i].Size
    Dim vTempDate As String = vItems[i].InternalDate
    

    Then, to delete the message, change this line:

    vClient.Inbox.AddFlags(UniqueId, MessageFlags.Deleted, True)
    

    to this:

    vClient.Inbox.AddFlags(vItems[i].UniqueId, MessageFlags.Deleted, True)