gpt4 book ai didi

Golang Gmail imap 搜索卡住

转载 作者:数据小太阳 更新时间:2023-10-29 03:03:01 27 4
gpt4 key购买 nike

我正在尝试搜索特定发件人或特定标志。当我选择收件箱并对特定字符串进行搜索时,搜索 api 会卡住。我不确定我做错了什么。我也找不到太多例子。

criteria := imap.NewSearchCriteria()
criteria.WithoutFlags = []string{"\\Seen"}
uids, err := c.Search(criteria)
if err != nil {
log.Println(err)
}
seqset := new(imap.SeqSet)
seqset.AddNum(uids...)
section := &imap.BodySectionName{}
items := []imap.FetchItem{imap.FetchEnvelope, imap.FetchFlags, imap.FetchInternalDate, section.FetchItem()}
messages := make(chan *imap.Message)
go func() {
done <- c.Fetch(seqset, items, messages)
}()
msg := <-messages //its getting stuck at this point. Nothing happens i dont get messages.

最佳答案

当您的搜索可以返回多条消息时,您需要确保从messages channel 读取所有内容,否则程序将挂起。所以不是:

msg := <-messages

像这样在 channel 上循环:

for msg := range messages {
// process msg
}

这是我的工作示例,它从环境中提取连接详细信息。这是来自 emersion's imap lib 的示例的修改版本:

package main

import (
"crypto/tls"
"flag"
"fmt"
"log"
"os"
"time"

"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
)

func main() {
hours := flag.Int("hours", 24, "look this many hours in the past for messages")
flag.Parse()
if *hours > 0 {
*hours = *hours * -1
}

host := os.Getenv("IMAP_HOST")
port := os.Getenv("IMAP_PORT")
user := os.Getenv("IMAP_USER")
pass := os.Getenv("IMAP_PASS")
tlsn := os.Getenv("IMAP_TLS_SERVERNAME")
if port == "" {
port = "993"
}

connStr := fmt.Sprintf("%s:%s", host, port)

tlsc := &tls.Config{}
if tlsn != "" {
tlsc.ServerName = tlsn
}

c, err := client.DialTLS(connStr, tlsc)
if err != nil {
log.Fatal(err)
}
log.Println("Connected")
defer c.Logout()

if err := c.Login(user, pass); err != nil {
log.Fatal(err)
}
log.Println("Authenticated")

mbox, err := c.Select("INBOX", false)
if err != nil {
log.Fatal(err)
}
log.Println("Flags for INBOX:", mbox.Flags)

criteria := imap.NewSearchCriteria()
//criteria.WithFlags = []string{imap.SeenFlag}
criteria.Since = time.Now().Add(time.Duration(*hours) * time.Hour)
uids, err := c.Search(criteria)
if err != nil {
log.Println(err)
}
seqset := new(imap.SeqSet)
seqset.AddNum(uids...)
log.Printf("Search complete, found %d messages", len(uids))

section := &imap.BodySectionName{}
items := []imap.FetchItem{imap.FetchEnvelope, imap.FetchFlags, imap.FetchInternalDate, section.FetchItem()}
messages := make(chan *imap.Message)
done := make(chan error, 1)
go func() {
done <- c.Fetch(seqset, items, messages)
log.Println("Fetch complete")
}()
for msg := range messages {
if msg != nil {
log.Printf("got message with address %p\n", msg)
} else {
log.Println("no messages matched criteria")
}
}
if err := <-done; err != nil {
log.Fatal(err)
}
}

关于Golang Gmail imap 搜索卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50862938/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com