MailPoper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.IO;
  7. namespace Ant.Service.Utilities
  8. {
  9. public class MailPoper
  10. {
  11. public static List<MailMessageEx> Receive()
  12. {
  13. PopSetting ps = PopConfig.Create().PopSetting;
  14. return Receive(ps.Server, ps.Port, ps.UseSSL, ps.UserName, ps.Password);
  15. }
  16. public static List<MailMessageEx> Receive(string hostname, int port, bool useSsl, string username, string password)
  17. {
  18. using (Pop3Client client = new Pop3Client(hostname, port, useSsl, username, password))
  19. {
  20. client.Trace += new Action<string>(Console.WriteLine);
  21. client.Authenticate();
  22. client.Stat();
  23. List<MailMessageEx> maillist = new List<MailMessageEx>();
  24. MailMessageEx message = null;
  25. foreach (Pop3ListItem item in client.List())
  26. {
  27. message = client.RetrMailMessageEx(item);
  28. if (message != null)
  29. {
  30. client.Dele(item);
  31. maillist.Add(message);
  32. }
  33. }
  34. client.Noop();
  35. client.Rset();
  36. client.Quit();
  37. return maillist;
  38. }
  39. }
  40. }
  41. public class PopSetting
  42. {
  43. private string _server;
  44. public string Server
  45. {
  46. get { return _server; }
  47. set { _server = value; }
  48. }
  49. private int _port;
  50. public int Port
  51. {
  52. get { return _port; }
  53. set { _port = value; }
  54. }
  55. private bool _usessl;
  56. public bool UseSSL
  57. {
  58. get { return _usessl; }
  59. set { _usessl = value; }
  60. }
  61. private string _username;
  62. public string UserName
  63. {
  64. get { return _username; }
  65. set { _username = value; }
  66. }
  67. private string _password;
  68. public string Password
  69. {
  70. get { return _password; }
  71. set { _password = value; }
  72. }
  73. }
  74. public class PopConfig
  75. {
  76. private static PopConfig _popConfig;
  77. private string ConfigFile
  78. {
  79. get
  80. {
  81. string configPath = ConfigurationManager.AppSettings["PopConfigPath"];
  82. if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
  83. {
  84. configPath = HttpContext.Current.Request.MapPath("/Config/PopSetting.config");
  85. }
  86. else
  87. {
  88. if (!Path.IsPathRooted(configPath))
  89. configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "PopSetting.config"));
  90. else
  91. configPath = Path.Combine(configPath, "PopSetting.config");
  92. }
  93. return configPath;
  94. }
  95. }
  96. public PopSetting PopSetting
  97. {
  98. get
  99. {
  100. XmlDocument doc = new XmlDocument();
  101. doc.Load(this.ConfigFile);
  102. PopSetting popSetting = new PopSetting();
  103. popSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
  104. popSetting.Port = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("Port").InnerText);
  105. popSetting.UseSSL = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("UseSSL").InnerText);
  106. popSetting.UserName = doc.DocumentElement.SelectSingleNode("User").InnerText;
  107. popSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
  108. return popSetting;
  109. }
  110. }
  111. //public static Save()
  112. //{
  113. //}
  114. public static PopConfig Create()
  115. {
  116. if (_popConfig == null)
  117. {
  118. _popConfig = new PopConfig();
  119. }
  120. return _popConfig;
  121. }
  122. }
  123. }