3

I'm using ImapX to read a Gmail email account and after reading them, I'd like to mark them as 'read' As I understand it, others have been successful with:

ImapX.FolderCollection folders = imapclient.Folders;
ImapX.MessageCollection messages = imapclient.Folders["INBOX"].Search("UNSEEN", true); 
foreach (var mess in messages)
{
 mess.Process(); 
}

But Gmail isn't "marking these messages as read". Any insight to what I'm missing?

user1484054
  • 31
  • 1
  • 3

2 Answers2

1

First of all, if you're using the old ImapX library, I invite you to upgrade to ImapX 2. It's being constantly developed and supported. There is also sample code for all common operations.

The Process method of a message doesn't mark the message as read, it only downloads the whole message including attachments. In your case, if you call the Search method setting the second parameter to true, you don't have to call it for every single message.

To mark a message as read simply use the AddFlag method of Message:

ImapX.Collections.FolderCollection folders = imapclient.Folders;
ImapX.Collections.MessageCollection messages = imapclient.Folders["INBOX"].Search("UNSEEN", true); 
foreach (var mess in messages)
{
    mess.AddFlag(ImapX.Flags.MessageFlags.Seen); 
}
Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
Pavel Azanov
  • 188
  • 1
  • 11
  • Hello there, I have one for you: why isn't this working and how can I debug it http://stackoverflow.com/questions/18985371/unable-to-retrieve-email-folders – AlexandruC Sep 24 '13 at 16:14
0

I tried this code with imapx library (ver1 old version) and it rut ok, only download unseen emails then set it "seen". you also check state of these activities in your email. NOticeL you have to turn on imapx protocol (gmail setting), and go to google account/enable 2 sms authentication/get app password to connect

Dim client As New ImapX.ImapClient("imap.gmail.com", 993, True)
    Dim result As Boolean = client.Connection()
    If result Then
        result = client.LogIn("id@gmail.com", "gmail password")
        If result Then
            MessageBox.Show("Log on successful", "Status...", MessageBoxButtons.OK, MessageBoxIcon.Information)
            MessageBox.Show("Please wait for some minutes...", "Status...", MessageBoxButtons.OK, MessageBoxIcon.Information)


            For Each m As ImapX.Message In client.Folders("INBOX").Search("UNSEEN", True)


                If check_stop_read_email = True Then
                    client.LogOut()
                    check_stop_read_email = False
                    Exit For
                End If

                Threading.Thread.Sleep(1000)
                DoEvents()

                Try
                    m.Process()
                Catch ex As Exception
                    Continue For
                End Try

                'Email content is m.HtmlBody.TextData
                'Subject is m.Subject


                m.SetFlag(ImapX.ImapFlags.SEEN)
                DoEvents()

            Next
            client.LogOut()
            MessageBox.Show("Done!")
        Else
            MessageBox.Show("Wrong username or password", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    Else
        MessageBox.Show("Connection_Failed", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
huy
  • 174
  • 1
  • 4