Golang IMAP:移动邮件到不同文件夹可能引发“指定的消息集无效”错误
来源:stackoverflow
时间:2024-03-15 20:42:28 266浏览 收藏
Go 语言中使用 IMAP 库时,在将邮件移动到不同文件夹时可能会遇到“指定的消息集无效”的错误。此错误通常是因为使用消息序号(SeqNum)而不是唯一标识符(UID)来移动邮件导致的。使用 UID 可以解决此问题,因为它不会随着其他邮件的移动而改变。
用例:我们有一个邮箱,用于接收来自客户的邮件。在进一步处理之前,我必须检查它们是否遵守协议(主题必须匹配正则表达式,每封邮件只能有一个附件等) 我有以下应用程序:
import (
"fmt"
"imaptest/src/db"
"io"
"log"
"path/filepath"
"strings"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-message"
"github.com/iglin/go-config"
)
(...)
//Channel for the messages
messages := make(chan *imap.Message, 10)
//Channel for retrieve if an error appeared
done := make(chan error, 1)
go func() {
seqset := new(imap.SeqSet)
seqset.AddRange(1, mbox.Messages)
//Fetch the messages and push them into the channel
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope, imap.FetchRFC822}, messages)
}()
//SeqSet for invalid messages
seqsetErrorMessages := new(imap.SeqSet)
//SeqSet for valid messages
seqsetValidMessages := new(imap.SeqSet)
for msg := range messages {
//Different checks like are there attachments
//Matches the subject with a specific regex
if isMessageValid(msg) {
//Message is valid
seqsetValidMessages.AddNum(msg.SeqNum)
} else {
//Message is invalid
seqsetErrorMessages.AddNum(msg.SeqNum)
}
}
//Check if there was an error when fetching the messages
if err := <-done; err != nil {
log.Fatal(err)
}
//Move all invalid messages to error
if !seqsetErrorMessages.Empty() {
if err := c.Move(seqsetErrorMessages, FOLDER_ERROR); err != nil {
log.Fatalf("Error on move to %s: %v", FOLDER_ERROR, err)
}
}
//Move all valid messages to toExport
if !seqsetValidMessages.Empty() {
if err := c.Move(seqsetValidMessages, FOLDER_OUT); err != nil {
log.Fatalf("Error on move to %s: %v", FOLDER_OUT, err)
}
}
folder_out 和 folder_error 是此邮箱中现有文件夹的常量字符串。
如果源文件夹的所有消息都有效(或无效),则一切正常 很好,所有邮件都已移动。
但是如果我同时存在有效和无效邮件的情况,我会收到以下错误:
移动时出错错误:指定的消息集无效。
我还尝试使用“msg.uid”和“c.uidmove()”而不是“msg.seqnum”和“c.move()”,但这会导致相同的结果。
如何解决这个问题?
正确答案
使用 UID 是此处的正确答案,但请确保您拥有 UID。 “1,4,9”作为 MSN 集和 UID 集都有效,但两者不可互换。您不能从服务器请求 MSN 集,然后将其与 UID 命令一起使用。
如果邮箱中有两封邮件,并且您希望将第一封邮件移至 FOLDER_ERROR,将第二封邮件移至 FOLDER_OUT,则您看到的问题很容易解释。您发送一条命令“将邮箱中的第一封邮件移至 FOLDER_ERROR”,服务器就会执行此操作。然后,您发送“将邮箱中的第二封邮件移至 FOLDER_OUT”,服务器会告诉您“此邮箱中只有一封邮件,没有第二封邮件可以移动”。
使用 UID 可以解决这个问题,因为它们是稳定的。无论您移动哪条其他消息,消息都会保留其 UID。
到这里,我们也就讲完了《Golang IMAP:移动邮件到不同文件夹可能引发“指定的消息集无效”错误》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
Golang · Go问答 | 3天前 | 并发 · channel · select · 性能排查 · Go问答 · select Go channel context default CPU飙高 忙等循环 ticker438 收藏
-
Golang · Go问答 | 3天前 | pprof · trace · 性能排查 · Go问答 · 服务安全 · Go pprof 生产环境 trace 安全入口 net/http/pprof 性能排障349 收藏
-
Golang · Go问答 | 3天前 | channel · 并发编程 · Go问答 · 背压 · 容量规划 · Goroutine channel 缓冲区 背压 Go问答 buffered channel 并发容量377 收藏
-
Golang · Go问答 | 3天前 | interface · 单元测试 · 架构设计 · repository · Go问答 · 单元测试 架构设计 interface 接口设计 Go问答 调用方定义 Repository212 收藏
-
Golang · Go问答 | 3天前 | JSON · time.Time · 接口设计 · Go问答 · encoding/json · encoding/json API响应 JSON序列化 time.Time omitempty Go问答 omitzero315 收藏
-
Golang · Go问答 | 3天前 | HTTP · Cookie · 浏览器 · cors · Go问答 · SameSite · cookie cors Secure SameSite Go问答 Set-Cookie 跨站请求 credentials246 收藏
-
Golang · Go问答 | 3天前 | 中间件 · Context · Go问答 · 架构模式 · 代码边界 · 中间件 context Context.Value Go问答 WithValue 请求作用域 业务参数269 收藏
-
Golang · Go问答 | 3天前 | JSON · 后端开发 · Go问答 · encoding/json · 接口解析 · JSON解析 encoding/json DisallowUnknownFields Go问答 RawMessage json.Decoder UseNumber151 收藏
-
Golang · Go问答 | 4天前 | HTTP · net/http · Go问答 · 流式响应 · ResponseController · net/http FLUSH 流式响应 Go问答 ResponseController FullDuplex 写超时161 收藏
-
Golang · Go问答 | 4天前 | HTTP · sse · Go问答 · 用户体验 · 流式响应 · Go EventSource SSE Go问答 Server-Sent Events 长任务进度 http.Flusher293 收藏
-
Golang · Go问答 | 4天前 | Timer · 性能优化 · time.After · Go问答 · Go 内存优化 Timer time.After Go问答 time.NewTimer Go1.23384 收藏
-
Golang · Go问答 | 4天前 | go · Context · 并发编程 · 接口超时 · 超时控制 goroutine泄漏 WithTimeout Go context Go问答 CancelFunc477 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习