如何将邮件标记为未读?

I wanna to mark message by IMAP as unread in go. I don't know, what parameters give to "Replace" function. I'm using http://godoc.org/github.com/mxk/go-imap/imap

It's my code:

set, _ := imap.NewSeqSet("")
set.AddNum(45364) // 45364 is message's UID
_, err = imap.Wait(c.UIDStore(set, "+FLAGS", imap.Replace()))

Taking a look at RFC3501 and the documentation for Replace, it looks a bit messy. Investigating the source for Replace, it just wants a []string with the keywords from RFC3501. So, for example

flags := []string{}
// ....
_, err = imap.Wait(c.UIDStore(set, "+FLAGS", imap.Replace(flags)))
// Since the "\Seen" is not in splice, the message will be unseen

Please notice that Replace really removes all the flags. You have to handle (add to the splice as string values) what you want to preserve:

  • \Seen
  • \Answered
  • \Flagged
  • \Deleted
  • \Draft
  • \Recent

You can get the previous values from the MessageInfo struct / Flags:

type MessageInfo struct {
    Attrs        FieldMap  // All returned attributes
    Seq          uint32    // Message sequence number
    UID          uint32    // Unique identifier (optional in non-UID FETCH)
    Flags        FlagSet   // Flags that are set for this message (optional)
    InternalDate time.Time // Internal to the server message timestamp (optional)
    Size         uint32    // Message size in bytes (optional)
}

You can do it another way. You can remove the flag. Example:

flags := `\Seen`
tmpSet, _ := imap.NewSeqSet("")
tmpSet.AddNum(emailUid)
_, err = imap.Wait(c.UIDStore(tmpSet, "-FLAGS", imap.NewFlagSet(flags)))