+1 vote

Hi,

As I cycle through emails in the inbox and bring them into a datagridview I would like to bold font any that are unread, but cant find a property to represent this.

Something like:

if email = unread then set row in datagridview to bold. (code below)

I am happy with how to do the .NET part, just need to identify the unread ones.

I am using imap.PeekMessageByUID and only marking messages as read once opened locally.

Thanks,

Dave

Using imap As New Imap
    Dim mAttachmentCount As String = ""
    Dim mEmailFound As Boolean = False
    Dim mEMailFrom(2) As String
    Dim mEmailTo(2) As String

    imap.Connect("mail@mail.com")      
    imap.UseBestLogin("fred@mail.com", "password")
    imap.SelectInbox()

    For Each uid As String In imap.GetAll()

        Dim email As IMail = New MailBuilder().CreateFromEml(
            imap.PeekMessageByUID(uid))

        For Each mRow As DataGridViewRow In DataGridView1.Rows
            If mRow.Cells("UID").Value = uid Then
                mEmailFound = True
            End If
        Next

        If mEmailFound = False Then 'Add the email
            mEMailFrom = Split(email.From.ToString, "Address=")
            mEMailFrom(0) = Replace(mEMailFrom(0), "Name=", "")
            mEMailFrom(0) = Replace(mEMailFrom(0), "'", "")
            mEMailFrom(1) = Replace(mEMailFrom(1), "'", "")

            mEmailTo = Split(email.To.ToString, "Address=")
            mEmailTo(0) = Replace(mEmailTo(0), "Name=", "")
            mEmailTo(0) = Replace(mEmailTo(0), "'", "")
            mEmailTo(1) = Replace(mEmailTo(1), "'", "")

            mAttachmentCount = email.Attachments.ToString
            mAttachmentCount = Replace(mAttachmentCount, 
                "Count = ", "")    

            DataGridView1.Rows.Add(email.Date, 
                mEMailFrom(0), 
                mEMailFrom(1), 
                mEmailTo(0), 
                mEmailTo(1), 
                email.Subject, 
                email.Text, 
                mAttachmentCount, 
                uid)
        End If
    Next
    imap.Close()
End Using
by

1 Answer

0 votes

There are several ways of doing this.

1.
Use Imap.Search(Flag.Unseen) to obtain list UIDs you should mark as unread:

Dim uids As List(Of Long) = imap.Search(Flag.Unseen)

Then you can use this list when filling the grid.

2.
Alternatively use Imap.GetMessageInfoByUID to download most common fields only: subject,
from, to, attachment list:

Using imap As New Imap()
    imap.Connect("imap.example.com")    ' or ConnectSSL
    imap.UseBestLogin("user", "password")

    imap.SelectInbox()
    Dim uids As List(Of Long) = imap.Search(Flag.Unseen)
    Dim infos As List(Of MessageInfo) = 
    imap.GetMessageInfoByUID(uids)

    For Each info As MessageInfo In infos
        Dim subject = info.Envelope.Subject
        Dim from = info.Envelope.From
        Dim [to] = info.Envelope.[To]
    Next
    imap.Close()
 End Using

https://www.limilabs.com/blog/get-email-information-from-imap-fast

Then you can examine Flags property on MessageInfo object:

Dim isSeen As Boolean = info.Flags.Contains(Flag.Seen)

This solution is very fast as you don't download all messages (plus attachments).

by (297k points)
...