码迷,mamicode.com
首页 > 其他好文 > 详细

IMAP协议学习

时间:2014-09-05 15:56:01      阅读:1816      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   数据   art   

服务器监听端口:143

一次IMAP4rev1连接组成:

1、建立客户端到服务器的网络连接;

2、服务器初始欢迎;

3、剩下就是重复客户端、服务器的交互, 交互基本形式为:

  客户端发送命令

  服务器返回请求数据

  服务器返回完成响应

  

  客户端命令引发操作。每个客户端命令以一个标识作为前缀(典型的有字母、数字构成的短字符串,如:A0001,A0002,等等)――它称为“标签”。客户端为每个命令生成不同的“标签”,也称每个客户端命令都是标签化的。

  IMAP4rev1服务器端的协议接收端,从客户端读取命令行,解析该命令行及其参数,并传送服务器数据(非标签化,以*或+开头)及一个服务器命令(标签化,有与客户端请求命令相同的前缀)完成结果的响应。

协议概述:

一旦客户端和服务器间的连接建立完成,一个IMAP4rev1连接就会处于4种状态中的某一种。
1.未认证状态 :在未认证状态下,大多数命令在得到许可前,客户端必须提供认证证书。若非连接已经是预认证了的,一个连接开始时,就进入了未认证状态。
2. 认证状态 :在认证状态下,客户端是认证了的,它必须先于影响邮件的命令被许可前,选择一个邮箱以访问。当一个预认证连接开始,被认可的认证证书已经提供,选择一个邮箱发生错误后,或者一个成功的CLOSE命令后,就进入了认证状态。
3. 选中状态 :在一个选中状态,一个邮箱被选中以访问。当一个邮箱被成功选中时,就进入了这个状态。
4. 注销状态 :在注销状态下,连接正在被终止。一个客户端请求(通过LOGOUT命令),或者客户端、服务器的单方面动作,都会导致进入这个状态。

之所以提及到状态,是因为IMAP协议的很多指令只有在处于某种状态下才会有效。下面我就为大家罗列一下指令的作用的权限吧。

1.在任何状态下都有效的指令(全局命令):CAPALIBILY,NOOP,LOGOUT。

2.未认证状态下有效的指令(未认证状态命令):STARTTLS,AUTHENTICATE,LOGIN和全局命令。

3.认证状态下有效的指令(认证状态命令):SELECT,EXAMINE,CREATE,DELETE,RENAME,SUBSCRIBE,UNSUBSCRIBE,LIST,LSUB,STATUS,APPEND和全局命令

4.在选中状态下有效的指令(选中状态指令):CHECK,CLOSE,EXPUNGE,SEARCH,FETCH,STORE,COPY,UID和全局命令、认证状态命令

IMAP命令、响应、语法:

固定格式,可直接参考IMAP协议RFC3501中文文档 6、7、9章;

实例:

一个用telnet操作imap命令的实例

bubuko.com,布布扣
  1 * OK Coremail System IMap Server Ready(126com[75c606d72bf436dfbce6e08e565f41f7])
  2 
  3 C1 LOGIN username@126.com "password"
  4 
  5 C1 OK LOGIN completed
  6 
  7 C2 CAPABILITY
  8 
  9 * CAPABILITY IMAP4rev1 XLIST SPECIAL-USE ID LITERAL+ STARTTLS XAPPLEPUSHSERVICE UIDPLUS X-CM-EXT-1
 10 
 11 C2 OK CAPABILITY completed
 12 
 13 C3 LIST "" *
 14 
 15 * LIST () "/" "INBOX"
 16 
 17 * LIST (\Drafts) "/" "&g0l6P3ux-"
 18 
 19 * LIST (\Sent) "/" "&XfJT0ZAB-"
 20 
 21 * LIST (\Trash) "/" "&XfJSIJZk-"
 22 
 23 * LIST (\Junk) "/" "&V4NXPpCuTvY-"
 24 
 25 * LIST () "/" "&dcVr0pCuTvY-"
 26 
 27 * LIST () "/" "&V4NXPnux-"
 28 
 29 * LIST () "/" "&U9GQAQ-"
 30 
 31 C3 OK LIST Completed
 32 
 33 C4 LSUB "" *
 34 
 35 C4 OK LSUB Completed
 36 
 37 C5 NOOP
 38 
 39 C5 OK NOOP completed
 40 
 41 C6 STATUS "INBOX" (MESSAGES RECENT UIDVALIDITY)
 42 
 43 * STATUS "INBOX" (MESSAGES 32 RECENT 0 UIDVALIDITY 1)
 44 
 45 C6 OK STATUS completed
 46 
 47 C7 STATUS "&g0l6P3ux-" (MESSAGES RECENT UIDVALIDITY)
 48 
 49 * STATUS "&g0l6P3ux-" (MESSAGES 1 RECENT 0 UIDVALIDITY 2)
 50 
 51 C7 OK STATUS completed
 52 
 53 C8 STATUS "&XfJT0ZAB-" (MESSAGES RECENT UIDVALIDITY)
 54 
 55 * STATUS "&XfJT0ZAB-" (MESSAGES 8 RECENT 0 UIDVALIDITY 3)
 56 
 57 C8 OK STATUS completed
 58 
 59 C9 STATUS "&XfJSIJZk-" (MESSAGES RECENT UIDVALIDITY)
 60 
 61 * STATUS "&XfJSIJZk-" (MESSAGES 0 RECENT 0 UIDVALIDITY 4)
 62 
 63 C9 OK STATUS completed
 64 
 65 C10 STATUS "&V4NXPpCuTvY-" (MESSAGES RECENT UIDVALIDITY)
 66 
 67 * STATUS "&V4NXPpCuTvY-" (MESSAGES 0 RECENT 0 UIDVALIDITY 5)
 68 
 69 C10 OK STATUS completed
 70 
 71 C11 NOOP
 72 
 73 C11 OK NOOP completed
 74 
 75 C12 SELECT "INBOX"
 76 
 77 * 32 EXISTS
 78 
 79 * 0 RECENT
 80 
 81 * OK [UIDVALIDITY 1] UIDs valid
 82 
 83 * FLAGS (\Answered \Seen \Deleted \Draft \Flagged)
 84 
 85 * OK [PERMANENTFLAGS (\Answered \Seen \Deleted \Draft \Flagged)] Limited
 86 
 87 C12 OK [READ-WRITE] SELECT completed
 88 
 89 C13 FETCH 1:32 (UID)
 90 
 91 * 1 FETCH (UID 1407140116)
 92 
 93 * 2 FETCH (UID 1407140117)
 94 
 95 * 3 FETCH (UID 1407140118)
 96 
 97 * 4 FETCH (UID 1407140119)
 98 
 99 * 5 FETCH (UID 1407140120)
100 
101 * 6 FETCH (UID 1407140121)
102 
103 * 7 FETCH (UID 1407140122)
104 
105 * 8 FETCH (UID 1407140123)
106 
107 * 9 FETCH (UID 1407140124)
108 
109 * 10 FETCH (UID 1407140125)
110 
111 * 11 FETCH (UID 1407140126)
112 
113 * 12 FETCH (UID 1407140127)
114 
115 * 13 FETCH (UID 1407140128)
116 
117 * 14 FETCH (UID 1407140129)
118 
119 * 15 FETCH (UID 1407140130)
120 
121 * 16 FETCH (UID 1407140131)
122 
123 * 17 FETCH (UID 1407140132)
124 
125 * 18 FETCH (UID 1407140133)
126 
127 * 19 FETCH (UID 1407140134)
128 
129 * 20 FETCH (UID 1407140135)
130 
131 * 21 FETCH (UID 1407140136)
132 
133 * 22 FETCH (UID 1407140137)
134 
135 * 23 FETCH (UID 1407140138)
136 
137 * 24 FETCH (UID 1407140139)
138 
139 * 25 FETCH (UID 1407140140)
140 
141 * 26 FETCH (UID 1407140141)
142 
143 * 27 FETCH (UID 1407140142)
144 
145 * 28 FETCH (UID 1407140143)
146 
147 * 29 FETCH (UID 1407140144)
148 
149 * 30 FETCH (UID 1407140145)
150 
151 * 31 FETCH (UID 1407140146)
152 
153 * 32 FETCH (UID 1407140147)
154 
155 C13 OK Fetch completed
156 
157 C14 NOOP
158 
159 C14 OK NOOP completed
160 
161 C15 SELECT "&g0l6P3ux-"
162 
163 * 1 EXISTS
164 
165 * 0 RECENT
166 
167 * OK [UIDVALIDITY 2] UIDs valid
168 
169 * FLAGS (\Answered \Seen \Deleted \Draft \Flagged)
170 
171 * OK [PERMANENTFLAGS (\Answered \Seen \Deleted \Draft \Flagged)] Limited
172 
173 C15 OK [READ-WRITE] SELECT completed
174 
175 C16 FETCH 1:1 (UID)
176 
177 * 1 FETCH (UID 1407141539)
178 
179 C16 OK Fetch completed
180 
181 C17 NOOP
182 
183 C17 OK NOOP completed
184 
185 C18 SELECT "&XfJT0ZAB-"
186 
187 * 8 EXISTS
188 
189 * 0 RECENT
190 
191 * OK [UIDVALIDITY 3] UIDs valid
192 
193 * FLAGS (\Answered \Seen \Deleted \Draft \Flagged)
194 
195 * OK [PERMANENTFLAGS (\Answered \Seen \Deleted \Draft \Flagged)] Limited
196 
197 C18 OK [READ-WRITE] SELECT completed
198 
199 C19 FETCH 1:8 (UID)
200 
201 * 1 FETCH (UID 1407140524)
202 
203 * 2 FETCH (UID 1407140525)
204 
205 * 3 FETCH (UID 1407140526)
206 
207 * 4 FETCH (UID 1407140527)
208 
209 * 5 FETCH (UID 1407140528)
210 
211 * 6 FETCH (UID 1407140529)
212 
213 * 7 FETCH (UID 1407140530)
214 
215 * 8 FETCH (UID 1407140531)
216 
217 C19 OK Fetch completed
218 
219 C20 NOOP
220 
221 C20 OK NOOP completed
222 
223 C21 SELECT "&XfJSIJZk-"
224 
225 * 0 EXISTS
226 
227 * 0 RECENT
228 
229 * OK [UIDVALIDITY 4] UIDs valid
230 
231 * FLAGS (\Answered \Seen \Deleted \Draft \Flagged)
232 
233 * OK [PERMANENTFLAGS (\Answered \Seen \Deleted \Draft \Flagged)] Limited
234 
235 C21 OK [READ-WRITE] SELECT completed
236 
237 C22 NOOP
238 
239 C22 OK NOOP completed
240 
241 C23 SELECT "&V4NXPpCuTvY-"
242 
243 * 0 EXISTS
244 
245 * 0 RECENT
246 
247 * OK [UIDVALIDITY 5] UIDs valid
248 
249 * FLAGS (\Answered \Seen \Deleted \Draft \Flagged)
250 
251 * OK [PERMANENTFLAGS (\Answered \Seen \Deleted \Draft \Flagged)] Limited
252 
253 C23 OK [READ-WRITE] SELECT completed
254 
255 * OK Coremail System IMap Server Ready(126com[75c606d72bf436dfbce6e08e565f41f7])
IMAP COMMANDS


 

参考资料:IMAP协议RFC3501中文文档

     IMAP协议指令学习以及代码(C/C++)示例

IMAP协议学习

标签:style   blog   http   color   os   io   ar   数据   art   

原文地址:http://www.cnblogs.com/91Kesson/p/3957983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!