IMAP附件检索命令 [英] IMAP attachment retrieving command

查看:68
本文介绍了IMAP附件检索命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IMAP在邮件客户端上工作,正在寻找用于接收邮件附件的命令.

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

推荐答案

使用 FETCH命令.但是,您有两种使用方式的选择.

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

首先,您可以逐字检索整个电子邮件.在这种情况下,您将需要在客户端中包括一个MIME解析器以弄清消息的结构. (每个平台至少有一个或两个流行的MIME解析器;由于您没有告诉我们您要编码的内容,因此我不建议您使用该解析器.)一旦您从MIME解析器中获取了消息结构,便可以需要一些客户端逻辑来确定哪些部分是附件.值得参考一下 RFC 2183 .通常,以Content-Disposition开头以"attachment"开头的部分将成为附件,但是所有邮件客户端作者都经历了反复试验的阶段才能正确处理.为了下载整个电子邮件,您需要发出IMAP命令

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[]

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

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)

一旦确定了感兴趣的部分,就可以为可显示的消息正文发出FETCH.然后,客户可以只列出邮件附件(从BODY响应中解析出来),然后如果用户单击邮件附件,可以返回并FETCH.因此,您要发出的IMAP命令将遵循以下原则:

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

这篇关于IMAP附件检索命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆