dslkchyv673627
2015-09-16 17:45
浏览 267
已采纳

Golang IMAP:将邮件移动到另一个文件夹

I'm lost on where to look in the references to accomplish this; I've tried several iterations of code and each fails. Slightly edited, but enough to get the gist...

// Make connection
imConnection, err := imap.DialTLS(strAddress, nil)

// Defer disconnect
defer func(){
    imConnection.Logout(30*time.Second)
}

// Authenticate
imConnection.Login(strUname, strPass)

//Select the folder with messages I want to move
imConnection.Select(`[Gmail]\Movethese`, false)

// Create a set
set, _ = imap.NewSeqSet("1:*")

// It's my understanding that moving messages means copying them over, then
// deleting the original messages?
cmd, _ := imConnection.UIDCopy(set, `[Gmail]\Destination`)

This seems to silently fail. This to me looked like it should select everything in the "Movethese" folder and copy them to "Destination." What am I missing in properly copying them over? Is there a simple way to move individual messages that match a certain subject line string?

Also I wasn't sure if the source directory would have to be set to False for R/W when selected, but it doesn't seem to make a difference.

This is importing the github.com/mxk/go-imap/imap package

图片转代码服务由CSDN问答提供 功能建议

我不知道在哪里可以找到引用来完成此操作; 我尝试了几次代码迭代,但每次都失败。 稍作编辑,但足以获得要点...

  //建立连接
imConnection,错误:= imap.DialTLS(strAddress,nil)
 
 // 延迟断开连接
defer func(){
 imConnection.Logout(30 * time.Second)
} 
 
 // Authenticate 
imConnection.Login(strUname,strPass)
 
 //选择包含消息的文件夹 我想移动
imConnection.Select(`[Gmail] \ Movethese`,false)
 
 //创建一个集合
set,_ = imap.NewSeqSet(“ 1:*”)
 
 // 我的理解是,移动邮件意味着要复制它们,然后
 //删除原始邮件?
cmd,_:= imConnection.UIDCopy(set,`[Gmail] \ Destination`)
    
 
 

这似乎无声地失败了。 在我看来,这应该选择“ Movethese”文件夹中的所有内容并将其复制到“ Destination”。 在正确复制它们时我缺少什么? 是否有一种简单的方法来移动与特定主题行字符串匹配的单个消息?

而且我不确定在以下情况下是否必须将源目录设置为False 选择,但似乎没有什么不同。

这将导入 github.com/mxk/go-imap/imap包

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongque8332 2015-09-17 13:00
    已采纳

    Try to add error checking to the two commands that name mailboxes; the backslash is a special character in source code so I expect gmail is giving you an error such as No such mailbox: [Gmail]Movethese.

    Handling errors is generally a good idea, particularly in cases such as this, when you know there is an error somewhere.

    BTW, the copy/delete sequence is a bit oldfashioned. Most IMAP servers support UID MOVE as an atomic command these days, and IIRC gmail is among the supporters. Can't check now, though, so don't trust me.

    打赏 评论

相关推荐 更多相似问题