Pop3Client.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Sockets;
  5. namespace Ant.Service.Utilities
  6. {
  7. /// <summary>
  8. /// The Pop3Client class provides a wrapper for the Pop3 commands
  9. /// that can be executed against a Pop3Server. This class will
  10. /// execute and return results for the various commands that are
  11. /// executed.
  12. /// </summary>
  13. public sealed class Pop3Client : IDisposable
  14. {
  15. private static readonly int DefaultPort = 110;
  16. private TcpClient _client;
  17. private Stream _clientStream;
  18. /// <summary>
  19. /// Traces the various command objects that executed during this objects
  20. /// lifetime.
  21. /// </summary>
  22. public event Action<string> Trace;
  23. private void OnTrace(string message)
  24. {
  25. if (Trace != null)
  26. {
  27. Trace(message);
  28. }
  29. }
  30. private string _hostname;
  31. /// <summary>
  32. /// Gets the hostname.
  33. /// </summary>
  34. /// <value>The hostname.</value>
  35. public string Hostname
  36. {
  37. get { return _hostname; }
  38. }
  39. private int _port;
  40. /// <summary>
  41. /// Gets the port.
  42. /// </summary>
  43. /// <value>The port.</value>
  44. public int Port
  45. {
  46. get { return _port; }
  47. }
  48. private bool _useSsl;
  49. /// <summary>
  50. /// Gets a value indicating whether [use SSL].
  51. /// </summary>
  52. /// <value><c>true</c> if [use SSL]; otherwise, <c>false</c>.</value>
  53. public bool UseSsl
  54. {
  55. get { return _useSsl; }
  56. }
  57. private string _username;
  58. /// <summary>
  59. /// Gets or sets the username.
  60. /// </summary>
  61. /// <value>The username.</value>
  62. public string Username
  63. {
  64. get { return _username; }
  65. set { _username = value; }
  66. }
  67. private string _password;
  68. /// <summary>
  69. /// Gets or sets the password.
  70. /// </summary>
  71. /// <value>The password.</value>
  72. public string Password
  73. {
  74. get { return _password; }
  75. set { _password = value; }
  76. }
  77. private Pop3State _currentState;
  78. /// <summary>
  79. /// Gets the state of the current.
  80. /// </summary>
  81. /// <value>The state of the current.</value>
  82. public Pop3State CurrentState
  83. {
  84. get { return _currentState; }
  85. }
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="Pop3Client"/> class using the default POP3 port 110
  88. /// without using SSL.
  89. /// </summary>
  90. /// <param name="hostname">The hostname.</param>
  91. /// <param name="username">The username.</param>
  92. /// <param name="password">The password.</param>
  93. public Pop3Client(string hostname, string username, string password)
  94. : this(hostname, DefaultPort, false, username, password) { }
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="Pop3Client"/> class using the default POP3 port 110.
  97. /// </summary>
  98. /// <param name="hostname">The hostname.</param>
  99. /// <param name="useSsl">if set to <c>true</c> [use SSL].</param>
  100. /// <param name="username">The username.</param>
  101. /// <param name="password">The password.</param>
  102. public Pop3Client(string hostname, bool useSsl, string username, string password)
  103. : this(hostname, DefaultPort, useSsl, username, password) { }
  104. /// <summary>
  105. /// Initializes a new instance of the <see cref="Pop3Client"/> class.
  106. /// </summary>
  107. /// <param name="hostname">The hostname.</param>
  108. /// <param name="port">The port.</param>
  109. /// <param name="useSsl">if set to <c>true</c> [use SSL].</param>
  110. /// <param name="username">The username.</param>
  111. /// <param name="password">The password.</param>
  112. public Pop3Client(string hostname, int port, bool useSsl, string username, string password)
  113. : this()
  114. {
  115. if (string.IsNullOrEmpty(hostname))
  116. {
  117. throw new ArgumentNullException("hostname");
  118. }
  119. if (port < 0)
  120. {
  121. throw new ArgumentOutOfRangeException("port");
  122. }
  123. if (string.IsNullOrEmpty(username))
  124. {
  125. throw new ArgumentNullException("username");
  126. }
  127. if (string.IsNullOrEmpty(password))
  128. {
  129. throw new ArgumentNullException("password");
  130. }
  131. _hostname = hostname;
  132. _port = port;
  133. _useSsl = useSsl;
  134. _username = username;
  135. _password = password;
  136. }
  137. /// <summary>
  138. /// Initializes a new instance of the <see cref="Pop3Client"/> class.
  139. /// </summary>
  140. private Pop3Client()
  141. {
  142. _client = new TcpClient();
  143. _currentState = Pop3State.Unknown;
  144. }
  145. /// <summary>
  146. /// Checks the connection.
  147. /// </summary>
  148. private void EnsureConnection()
  149. {
  150. if (!_client.Connected)
  151. {
  152. throw new Pop3Exception("Pop3 client is not connected.");
  153. }
  154. }
  155. /// <summary>
  156. /// Resets the state.
  157. /// </summary>
  158. /// <param name="state">The state.</param>
  159. private void SetState(Pop3State state)
  160. {
  161. _currentState = state;
  162. }
  163. /// <summary>
  164. /// Ensures the response.
  165. /// </summary>
  166. /// <param name="response">The response.</param>
  167. /// <param name="error">The error.</param>
  168. private void EnsureResponse(Pop3Response response, string error)
  169. {
  170. if (response == null)
  171. {
  172. throw new Pop3Exception("Unable to get Response. Response object null.");
  173. }
  174. if (response.StatusIndicator)
  175. {
  176. return;
  177. } //the command execution was successful.
  178. string errorMessage = string.Empty;
  179. if (string.IsNullOrEmpty(error))
  180. {
  181. errorMessage = response.HostMessage;
  182. }
  183. else
  184. {
  185. errorMessage = string.Concat(error, ": ", error);
  186. }
  187. throw new Pop3Exception(errorMessage);
  188. }
  189. /// <summary>
  190. /// Ensures the response.
  191. /// </summary>
  192. /// <param name="response">The response.</param>
  193. private void EnsureResponse(Pop3Response response)
  194. {
  195. EnsureResponse(response, string.Empty);
  196. }
  197. /// <summary>
  198. /// Traces the command.
  199. /// </summary>
  200. /// <param name="command">The command.</param>
  201. private void TraceCommand<TCommand, TResponse>(TCommand command)
  202. where TCommand : Pop3Command<TResponse>
  203. where TResponse : Pop3Response
  204. {
  205. if (Trace != null)
  206. {
  207. command.Trace += delegate(string message) { OnTrace(message); };
  208. }
  209. }
  210. /// <summary>
  211. /// Connects this instance and properly sets the
  212. /// client stream to Use Ssl if it is specified.
  213. /// </summary>
  214. private void Connect()
  215. {
  216. if (_client == null)
  217. {
  218. _client = new TcpClient();
  219. } //If a previous quit command was issued, the client would be disposed of.
  220. if (_client.Connected)
  221. {
  222. return;
  223. } //if the connection already is established no need to reconnect.
  224. SetState(Pop3State.Unknown);
  225. ConnectResponse response;
  226. using (ConnectCommand command = new ConnectCommand(_client, _hostname, _port, _useSsl))
  227. {
  228. TraceCommand<ConnectCommand, ConnectResponse>(command);
  229. response = command.Execute(CurrentState);
  230. EnsureResponse(response);
  231. }
  232. SetClientStream(response.NetworkStream);
  233. SetState(Pop3State.Authorization);
  234. }
  235. /// <summary>
  236. /// Sets the client stream. If UseSsl <c>true</c> then wrap
  237. /// the client's <c>NetworkStream</c> in an <c>SslStream</c>, if UseSsl <c>false</c>
  238. /// then set the client stream to the <c>NetworkStream</c>
  239. /// </summary>
  240. private void SetClientStream(Stream networkStream)
  241. {
  242. if (_clientStream != null)
  243. {
  244. _clientStream.Dispose();
  245. }
  246. _clientStream = networkStream;
  247. }
  248. /// <summary>
  249. /// Authenticates this instance.
  250. /// </summary>
  251. /// <remarks>A successful execution of this method will result in a Current State of Transaction.
  252. /// Unsuccessful USER or PASS commands can be reattempted by resetting the Username or Password
  253. /// properties and re-execution of the methods.</remarks>
  254. /// <exception cref="Pop3Exception">
  255. /// If the Pop3Server is unable to be connected.
  256. /// If the User command is unable to be successfully executed.
  257. /// If the Pass command is unable to be successfully executed.
  258. /// </exception>
  259. public void Authenticate()
  260. {
  261. Connect();
  262. //execute the user command.
  263. using (UserCommand userCommand = new UserCommand(_clientStream, _username))
  264. {
  265. ExecuteCommand<Pop3Response, UserCommand>(userCommand);
  266. }
  267. //execute the pass command.
  268. using (PassCommand passCommand = new PassCommand(_clientStream, _password))
  269. {
  270. ExecuteCommand<Pop3Response, PassCommand>(passCommand);
  271. }
  272. _currentState = Pop3State.Transaction;
  273. }
  274. /// <summary>
  275. /// Executes the POP3 DELE command.
  276. /// </summary>
  277. /// <param name="item">The item.</param>
  278. /// /// <exception cref="Pop3Exception">If the DELE command was unable to be executed successfully.</exception>
  279. public void Dele(Pop3ListItem item)
  280. {
  281. if (item == null)
  282. {
  283. throw new ArgumentNullException("item");
  284. }
  285. using (DeleCommand command = new DeleCommand(_clientStream, item.MessageId))
  286. {
  287. ExecuteCommand<Pop3Response, DeleCommand>(command);
  288. }
  289. }
  290. /// <summary>
  291. /// Executes the POP3 NOOP command.
  292. /// </summary>
  293. /// <exception cref="Pop3Exception">If the NOOP command was unable to be executed successfully.</exception>
  294. public void Noop()
  295. {
  296. using (NoopCommand command = new NoopCommand(_clientStream))
  297. {
  298. ExecuteCommand<Pop3Response, NoopCommand>(command);
  299. }
  300. }
  301. /// <summary>
  302. /// Executes the POP3 RSET command.
  303. /// </summary>
  304. /// <exception cref="Pop3Exception">If the RSET command was unable to be executed successfully.</exception>
  305. public void Rset()
  306. {
  307. using (RsetCommand command = new RsetCommand(_clientStream))
  308. {
  309. ExecuteCommand<Pop3Response, RsetCommand>(command);
  310. }
  311. }
  312. /// <summary>
  313. /// Executes the POP3 STAT command.
  314. /// </summary>
  315. /// <returns>A Stat object containing the results of STAT command.</returns>
  316. /// <exception cref="Pop3Exception">If the STAT command was unable to be executed successfully.</exception>
  317. public Stat Stat()
  318. {
  319. StatResponse response;
  320. using (StatCommand command = new StatCommand(_clientStream))
  321. {
  322. response = ExecuteCommand<StatResponse, StatCommand>(command);
  323. }
  324. return new Stat(response.MessageCount, response.Octets);
  325. }
  326. /// <summary>
  327. /// Executes the POP3 List command.
  328. /// </summary>
  329. /// <returns>A generic List of Pop3Items containing the results of the LIST command.</returns>
  330. /// <exception cref="Pop3Exception">If the LIST command was unable to be executed successfully.</exception>
  331. public List<Pop3ListItem> List()
  332. {
  333. ListResponse response;
  334. using (ListCommand command = new ListCommand(_clientStream))
  335. {
  336. response = ExecuteCommand<ListResponse, ListCommand>(command);
  337. }
  338. return response.Items;
  339. }
  340. /// <summary>
  341. /// Lists the specified message.
  342. /// </summary>
  343. /// <param name="message">The message.</param>
  344. /// <returns>A <c>Pop3ListItem</c> for the requested Pop3Item.</returns>
  345. /// <exception cref="Pop3Exception">If the LIST command was unable to be executed successfully for the provided message id.</exception>
  346. public Pop3ListItem List(int messageId)
  347. {
  348. ListResponse response;
  349. using (ListCommand command = new ListCommand(_clientStream, messageId))
  350. {
  351. response = ExecuteCommand<ListResponse, ListCommand>(command);
  352. }
  353. return new Pop3ListItem(response.MessageNumber, response.Octets);
  354. }
  355. /// <summary>
  356. /// Retrs the specified message.
  357. /// </summary>
  358. /// <param name="item">The item.</param>
  359. /// <returns>A MimeEntity for the requested Pop3 Mail Item.</returns>
  360. public MimeEntity RetrMimeEntity(Pop3ListItem item)
  361. {
  362. if (item == null)
  363. {
  364. throw new ArgumentNullException("item");
  365. }
  366. if (item.MessageId < 1)
  367. {
  368. throw new ArgumentOutOfRangeException("item.MessageId");
  369. }
  370. RetrResponse response;
  371. using (RetrCommand command = new RetrCommand(_clientStream, item.MessageId))
  372. {
  373. response = ExecuteCommand<RetrResponse, RetrCommand>(command);
  374. }
  375. MimeReader reader = new MimeReader(response.MessageLines);
  376. return reader.CreateMimeEntity();
  377. }
  378. public MailMessageEx Top(int messageId, int lineCount)
  379. {
  380. if (messageId < 1)
  381. {
  382. throw new ArgumentOutOfRangeException("messageId");
  383. }
  384. if (lineCount < 0)
  385. {
  386. throw new ArgumentOutOfRangeException("lineCount");
  387. }
  388. RetrResponse response;
  389. using (TopCommand command = new TopCommand(_clientStream, messageId, lineCount))
  390. {
  391. response = ExecuteCommand<RetrResponse, TopCommand>(command);
  392. }
  393. MimeReader reader = new MimeReader(response.MessageLines);
  394. MimeEntity entity = reader.CreateMimeEntity();
  395. MailMessageEx message = entity.ToMailMessageEx();
  396. message.Octets = response.Octets;
  397. message.MessageNumber = messageId;
  398. return entity.ToMailMessageEx();
  399. }
  400. /// <summary>
  401. /// Retrs the mail message ex.
  402. /// </summary>
  403. /// <param name="item">The item.</param>
  404. /// <returns></returns>
  405. public MailMessageEx RetrMailMessageEx(Pop3ListItem item)
  406. {
  407. MailMessageEx message = RetrMimeEntity(item).ToMailMessageEx();
  408. if (message != null)
  409. {
  410. message.MessageNumber = item.MessageId;
  411. }
  412. return message;
  413. }
  414. /// <summary>
  415. /// Executes the Pop3 QUIT command.
  416. /// </summary>
  417. /// <exception cref="Pop3Exception">If the quit command returns a -ERR server message.</exception>
  418. public void Quit()
  419. {
  420. using (QuitCommand command = new QuitCommand(_clientStream))
  421. {
  422. ExecuteCommand<Pop3Response, QuitCommand>(command);
  423. if (CurrentState.Equals(Pop3State.Transaction))
  424. {
  425. SetState(Pop3State.Update);
  426. } // Messages could have been deleted, reflect the server state.
  427. Disconnect();
  428. //Quit command can only be called in Authorization or Transaction state, reset to Unknown.
  429. SetState(Pop3State.Unknown);
  430. }
  431. }
  432. /// <summary>
  433. /// Provides a common way to execute all commands. This method
  434. /// validates the connection, traces the command and finally
  435. /// validates the response message for a -ERR response.
  436. /// </summary>
  437. /// <param name="command">The command.</param>
  438. /// <returns>The Pop3Response for the provided command</returns>
  439. /// <exception cref="Pop3Exception">If the HostMessage does not start with '+OK'.</exception>
  440. /// <exception cref="Pop3Exception">If the client is no longer connected.</exception>
  441. private TResponse ExecuteCommand<TResponse, TCommand>(TCommand command)
  442. where TResponse : Pop3Response
  443. where TCommand : Pop3Command<TResponse>
  444. {
  445. EnsureConnection();
  446. TraceCommand<TCommand, TResponse>(command);
  447. TResponse response = (TResponse)command.Execute(CurrentState);
  448. EnsureResponse(response);
  449. return response;
  450. }
  451. /// <summary>
  452. /// Disconnects this instance.
  453. /// </summary>
  454. private void Disconnect()
  455. {
  456. if (_clientStream != null)
  457. {
  458. _clientStream.Close();
  459. } //release underlying socket.
  460. if (_client != null)
  461. {
  462. _client.Close();
  463. _client = null;
  464. }
  465. }
  466. public void Dispose()
  467. {
  468. Disconnect();
  469. }
  470. }
  471. }