IMAP 附件检索命令

发布于 2024-10-13 03:28:15 字数 44 浏览 3 评论 0原文

我正在使用 IMAP 开发邮件客户端,并且正在寻找用于接收消息附件的命令。

I am working on a mail client using IMAP and I am looking for the command for receiving the attachments of a message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳哥哥 2024-10-20 03:28:15

所有消息信息均使用 FETCH 命令检索。但是,对于如何使用它,您有两种选择。

首先,您可以逐字检索整个电子邮件。在这种情况下,您需要在客户端中包含一个 MIME 解析器来确定消息的结构。 (每个平台至少有一两个流行的 MIME 解析器;由于您没有告诉我们您正在编码的内容,所以我无法为您推荐一个。)一旦您从 MIME 解析器获得消息结构,您就可以将需要一些客户端逻辑来确定哪些部分是附件。值得一看 RFC 2183 来帮助您入门。一般来说,以“attachment”开头的 Content-Disposition 部分将是附件,但所有邮件客户端作者都会经历一个尝试和错误的阶段才能获得正确的结果。为了下载整个电子邮件,您需要发出 IMAP 命令

$ UID FETCH <uid> BODY.PEEK[]

其次,您可以通过发出 FETCH BODYSTRUCTURE(注意:没有方括号)让 IMAP 服务器为您解析消息结构。您必须自己解析返回的 BODYSTRUCTURE 数据; IMAP RFC 解释了格式并给出了一些示例。

# message, no attachments:
("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1" "FORMAT" "flowed") NIL NIL "7BIT" 1469 50 NIL NIL NIL NIL)

# message, one attachment
(("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "QUOTED-PRINTABLE" 56 1 NIL NIL NIL NIL)("AUDIO" "X-WAV" ("NAME" "voicemail.wav") NIL NIL "BASE64" 152364 NIL ("attachment" ("FILENAME" "voicemail.wav")) NIL NIL) "MIXED" ("BOUNDARY" "----_=_NextPart_001_01C4ACB3.5AA7B8E2") NIL NIL NIL)

一旦确定了您感兴趣的部分,您就可以为可显示的消息正文发出FETCH。然后,您的客户端可以只列出消息附件(从 BODY 响应中解析出来),然后可以返回并在用户单击它们时FETCH 它们。因此,您要发出的 IMAP 命令将类似于:

$ UID FETCH <uid> (BODY ENVELOPE)   # get structure and header info
$ UID FETCH <uid> (BODY[1])         # retrieving displayable body
$ UID FETCH <uid> (BODY[2])         # retrieving attachment on demand

All message info is retrieved using the FETCH command. You have two options on how to use it, however.

First, you can retrieve the entire email message, verbatim. In that case, you're going to need to include a MIME parser in your client to figure out the structure of the message. (Each platform has at least one or two popular MIME parsers; since you haven't told us what you're coding in, I can't recommend one for you.) Once you get the message structure from your MIME parser, you'll need some client logic to determine which parts are attachments. It's worth looking at RFC 2183 to get you started. In general, parts with a Content-Disposition starting with "attachment" are going to be attachments, but all mail client authors go through a phase of trial and error getting it right. In order to download the entire email message, you'd issue the IMAP command

$ UID FETCH <uid> BODY.PEEK[]

Second, you can have the IMAP server parse the message structure for you by issuing a FETCH BODYSTRUCTURE (note: no square brackets). You'll have to parse the returned BODYSTRUCTURE data yourself; the IMAP RFC explains the format and gives a few examples.

# message, no attachments:
("TEXT" "PLAIN" ("CHARSET" "ISO-8859-1" "FORMAT" "flowed") NIL NIL "7BIT" 1469 50 NIL NIL NIL NIL)

# message, one attachment
(("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "QUOTED-PRINTABLE" 56 1 NIL NIL NIL NIL)("AUDIO" "X-WAV" ("NAME" "voicemail.wav") NIL NIL "BASE64" 152364 NIL ("attachment" ("FILENAME" "voicemail.wav")) NIL NIL) "MIXED" ("BOUNDARY" "----_=_NextPart_001_01C4ACB3.5AA7B8E2") NIL NIL NIL)

Once you've determined which parts you're interested in, you can issue a FETCH for the displayable message body. Your client can then just list the message attachments (parsed out of the BODY response) and can then go back and FETCH them if the user clicks on them. So the IMAP commands you'd be issuing would be along the lines of:

$ UID FETCH <uid> (BODY ENVELOPE)   # get structure and header info
$ UID FETCH <uid> (BODY[1])         # retrieving displayable body
$ UID FETCH <uid> (BODY[2])         # retrieving attachment on demand
客…行舟 2024-10-20 03:28:15

我相信您正在寻找的是 IMAP v4 FETCH 命令

I believe what you are looking for is the IMAP v4 FETCH command.

风蛊 2024-10-20 03:28:15

您可以使用 Context.IO 的文件资源来快速轻松地获取附件。

http://context.io/docs/2.0/accounts/files#get

You could use Context.IO's files resource to quickly and easily fetch attachments.

http://context.io/docs/2.0/accounts/files#get

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文