简体   繁体   中英

How to search ALL Unread messages IMAP CMD / go

I'm developing an email client and for some reasons the IMAP server doesn't send me back all the unread messages. Out of 2915 emails it sends back only 2888. I've tried to mark them as read/unread consecutively and it seems that the "Read" command works and marks them all read, unread works too but when I search for unread messages I get don't get 27 of them.

Any hint as to why that happens? I suspect there is an IMAP glitch.

Some code

const (
    //serch
    SearchUnseen = "UNSEEN"
    SearchSeen   = "SEEN"
    SearchALL    = "ALL"
)
const (
    FlagSeen   = `\Seen`
    FlagUnseen = `\Unseen`
)

const (
    //flag types
    FlagSet    = "+FLAGS"
    FlagRemove = "-FLAGS"
)

type Request struct {
    Label string //label: INBOX, Sent
    Attr  string //attr: RFC822.HEADER , []BODY if is Fetch
    //OR "ALL", "UNSEEN" if is SEARCH
    //http://tools.ietf.org/html/rfc3501#section-6.4.4
    SqID string //sequences 1:44 , 1:*, 1,2,3 (UID)
}

cmd, err = c.UIDSearch(set, r.Attr)



cmd, err := imap.Wait(c.UIDStore(set, flagAction, imap.NewFlagSet(flag)))
    if err != nil {
        log.Error(err)
        return
    }

You're doing some really weird things and you should read how Imap and the lib work.

To sum up.

You have to select your mailbox depending on what you will want to achieve. If you want to only download email select it as read only. If you want to update some emails(seen, deleted) select it as writable. See my answer here .

If you want to download a list of UIDS, simply use UIDSearch with only one arg specifying the range you want to get. You can use UIDSearch("1:*")

If you want to get the UIDs of the unread emails: UIDSearch("1:* NOT SEEN")

UIDSearch does NOT care about Attr since it will only get UIDS and not bodys.

If you want the body, use UIDFetch and give it a *imap.SeqSet with the differents attributes you want to get. Example: UIDFetch(set, "RFC822.HEADER", "UID")

To sum up, read the documentation or browse the code with sourceGraph (really awesome)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM