Search code examples
goimap

How to mark a message as read , \Seen on IMAP ( Go )


I'm trying to mark a message/list of messages as "\SEEN" (permanently) using IMAP . However unlike fetch, search and friends there seem to be no Flag function on the imap package. Should I send raw commands with the UIDs ?


Solution

  • You have to select the mailbox as writable

    youImapConnection.Select(mailboxName, false) // true would be for readonly
    

    And then simply do the following

    seq, _ := imap.NewSeqSet("")
    err := seq.AddNum(612) // 612 is your UID
    _, err = imap.Wait(youImapConnection.UIDStore(seq, "+FLAGS", imap.NewFlagSet(`\Seen`))) // Here you tell to add the flag \Seen
    

    Finally you will have to expunge:

    _, err := imap.Wait(youImapConnection.Close(true)) // Here you tell to apply changes, necessary if you mark an Email as deleted
    

    And you should be good :-)

    And do not hesitate to browse the doc/source code, it's easy to understand and you'll find all you need.