How to fetch the email body in go ( with imap )?

huangapple go评论99阅读模式
英文:

How to fetch the email body in go ( with imap )?

问题

如何在Go语言中使用IMAP获取电子邮件正文?到目前为止,我只能找到标题,但没有指示在哪里查找正文。

英文:

How to fetch the email body in go ( with imap ) ?. So far I could find the headers but there is no indication where to look for the body.

答案1

得分: 1

我找到了获取正文文本的方法。以下是代码:

c.Select("INBOX", true)
//fmt.Print("\nMailbox status:\n", c.Mailbox)

// 获取最近10封邮件的头部信息
set, _ := imap.NewSeqSet("4:*")
//if c.Mailbox.Messages >= 10 {
//	set.AddRange(c.Mailbox.Messages-9, c.Mailbox.Messages)
//} else {
//	set.Add("1:*")
//}
cmd, _ = c.UIDFetch(set, "RFC822.HEADER", "RFC822.TEXT")

// 在命令运行时处理响应
fmt.Println("\n最近的邮件:")
for cmd.InProgress() {
	// 等待下一个响应(无超时)
	c.Recv(-1)

	// 处理命令数据
	for _, rsp = range cmd.Data {
		header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"])
		uid := imap.AsNumber((rsp.MessageInfo().Attrs["UID"]))
		body := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.TEXT"])
		if msg, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil {
			fmt.Println("|--", msg.Header.Get("Subject"))
			fmt.Println("UID: ", uid)

			fmt.Println(string(body))
		}
	}
	cmd.Data = nil
	c.Data = nil
}

以上是获取邮件正文文本的代码。

英文:

I figured out how to get the body text. Here is the code:

c.Select("INBOX", true)
//fmt.Print("\nMailbox status:\n", c.Mailbox)

// Fetch the headers of the 10 most recent messages
set, _ := imap.NewSeqSet("4:*")
//if c.Mailbox.Messages >= 10 {
//	set.AddRange(c.Mailbox.Messages-9, c.Mailbox.Messages)
//} else {
//	set.Add("1:*")
//}
cmd, _ = c.UIDFetch(set, "RFC822.HEADER", "RFC822.TEXT")

// Process responses while the command is running
fmt.Println("\nMost recent messages:")
for cmd.InProgress() {
	// Wait for the next response (no timeout)
	c.Recv(-1)

	// Process command data
	for _, rsp = range cmd.Data {
		header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"])
		uid := imap.AsNumber((rsp.MessageInfo().Attrs["UID"]))
		body := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.TEXT"])
		if msg, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil {
			fmt.Println("|--", msg.Header.Get("Subject"))
			fmt.Println("UID: ", uid)

			fmt.Println(string(body))
		}
	}
	cmd.Data = nil
	c.Data = nil
}

答案2

得分: 0

发送一个针对BODY[]BODY.PEEK[]属性的FETCH命令。

有关更多信息,请参阅IMAP规范

英文:

Send a FETCH command for the BODY[] or BODY.PEEK[] attributes.

For more information, see the IMAP spec.

答案3

得分: 0

我遇到了类似的问题。
我找到了一个使用"Fetch"和"Uid"的示例命令。

$ UID FETCH uid (body[text])

这是我的C#代码示例:

string fileBody = ReceiveResponse($"$ UID FETCH {uidList[i]} (body[text])\r\n");
英文:

I had similar trouble.
I found this example command for use "Fetch" and "Uid".

$ UID FETCH uid (body[text])

and my C# code example

string fileBody = ReceiveResponse($"$ UID FETCH {uidList[i]} (body[text])\r\n");

huangapple
  • 本文由 发表于 2014年7月21日 06:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/24855510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定