如何获取 IMAP 消息的可见/不可见状态

我已经阅读了 go文档以及一般的 imap文档,但似乎无法找到获取特定消息状态的正确方法 - 知道它是被标记为已读还是未读。


这是我到目前为止所得到的:


// 

//Code that set up 'c' and 'cmd' ...

//

for cmd.InProgress() {

    // Wait for the next response (no timeout)

    c.Recv(-1)


    // Process command data

    for _, rsp = range cmd.Data {

        if err != nil {

            fmt.Println(err)

        }

        header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"])  // Contains subject, from data

        uid := imap.AsNumber(rsp.MessageInfo().Attrs["UID"])  // Message unique id

        body := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.TEXT"])  // Message body

        //seenState := imap.AsBytes(rsp.MessageInfo().Attrs["Flags"])

        if msg, err := mail.ReadMessage(bytes.NewReader(header)); msg != nil {

            if err != nil {

                fmt.Println(err)

            }

            //START CUSTOM

            if strings.Contains(msg.Header.Get("Subject"), genUUID()){

                fmt.Println(rsp.Label)

                fmt.Println(rsp.MessageInfo().Attrs["Flags"])

                fmt.Println(c.Status("INBOX", string(uid)))

            }

            //END CUSTOM

对于输出,我得到:


FETCH

<nil>

LAOYU10 STATUS "INBOX" (Þ) <nil>

我引用的文档让我相信,如果消息被标记为看不见,至少我的一种方法应该打印。我错过了什么?


编辑:我正在测试带有四封邮件的收件箱(gmail)。前两个已读,后两个未读。这是所有四个消息的输出。


FETCH

<nil>

SIHLB7 STATUS "INBOX" (Û) <nil>

FETCH

<nil>

SIHLB8 STATUS "INBOX" (Ü) <nil>

FETCH

<nil>

SIHLB9 STATUS "INBOX" (Ý) <nil>

FETCH

<nil>

SIHLB10 STATUS "INBOX" (Þ) <nil>



慕妹3146593
浏览 180回答 3
3回答

白猪掌柜的

需要注意的几件事,请确保您实际上是在 imap 请求中请求 flags 字段。如果你发出取,那么你就必须在传递"FLAGS"作为参数Fetch,另外,在标志属性Attrs是大小写敏感的,所以你需要rsp.MessageInfo().Attrs["FLAGS"]。下面是一个在 Gmail 中使用 imap 和 go-imap 库的工作示例,运行它GMAIL_EMAIL=email.address GMAIL_PASSWD=mypassword go run go_file.gopackage mainimport (&nbsp; &nbsp; "code.google.com/p/go-imap/go1/imap"&nbsp; &nbsp; "crypto/rand"&nbsp; &nbsp; "crypto/tls"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; label := "INBOX"&nbsp; &nbsp; email := os.Getenv("GMAIL_EMAIL")&nbsp; &nbsp; passwd := os.Getenv("GMAIL_PASSWD")&nbsp; &nbsp; conf := &tls.Config{&nbsp; &nbsp; &nbsp; &nbsp; Rand: rand.Reader,&nbsp; &nbsp; }&nbsp; &nbsp; c, err := imap.DialTLS("imap.gmail.com:993", conf)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic("Failed to connect")&nbsp; &nbsp; }&nbsp; &nbsp; defer c.Logout(30 * time.Second)&nbsp; &nbsp; c.Data = nil&nbsp; &nbsp; if c.Caps["STARTTLS"] {&nbsp; &nbsp; &nbsp; &nbsp; c.StartTLS(nil)&nbsp; &nbsp; }&nbsp; &nbsp; // Authenticate&nbsp; &nbsp; if c.State() == imap.Login {&nbsp; &nbsp; &nbsp; &nbsp; c.Login(email, passwd)&nbsp; &nbsp; }&nbsp; &nbsp; if c.State() != imap.Auth {&nbsp; &nbsp; &nbsp; &nbsp; panic("Authentication error")&nbsp; &nbsp; }&nbsp; &nbsp; c.Select(label, true)&nbsp; &nbsp; set, _ := imap.NewSeqSet("*")&nbsp; &nbsp; cmd, err := c.Fetch(set, "FLAGS", "UID")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic("Bad fetch command")&nbsp; &nbsp; }&nbsp; &nbsp; _, err = cmd.Result(imap.OK)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic("Bad fetch response")&nbsp; &nbsp; }&nbsp; &nbsp; for _, rsp := range cmd.Data {&nbsp; &nbsp; &nbsp; &nbsp; seen := false&nbsp; &nbsp; &nbsp; &nbsp; for _, flag := range imap.AsList(rsp.MessageInfo().Attrs["FLAGS"]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if flag == "\\Seen" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seen = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if seen {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Message %d has been read!\n", imap.AsNumber(rsp.MessageInfo().Attrs["UID"]))&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Message %d has been not been read!\n", imap.AsNumber(rsp.MessageInfo().Attrs["UID"]))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

千巷猫影

每条消息在 IMAP 中都有一个标志列表,其中一个称为\seen(不区分大小写,IMAP 中的大多数内容也是如此)。如果标志列表不包含该标志,则消息不可见。@jstedfast 的回答解释了如何获取标志列表。剩下的就是在空白处拆分并检查列表中的任何单词是否等于\seen。

慕姐8265434

fmt.Println(rsp.MessageInfo().Attrs["Flags"])<nil>因为没有设置标志,所以为你打印,这意味着消息是“看不见的”。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go