工控编程吧

标题: 上位机MFC POP3协议的封装类 [打印本页]

作者: qq263946146    时间: 2019-7-14 23:37
标题: 上位机MFC POP3协议的封装类
在Internet上,电子邮件是最广泛使用的一种程序。这样,对于程序员来讲,编写的程序不涉及到电子邮件的处理,现在已经不太可能了。
通常,电子邮件系统用SMTP作为发送协议,用POP3作为邮件接收协议。
下面为编写的一个封装POP3协议的类CPop。
可以复制使用
  1. /*--------------------------------------------------------------------
  2.      Pop.h : main header file for the POP application
  3. -----------------------------------------------------------------------*/

  4. #if !defined(AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_)
  5. #define AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_

  6. #define CONNECTION_CHECK 0
  7. #define USER_CHECK  1
  8. #define PASSWORD_CHECK 2
  9. #define QUIT_CHECK  3
  10. #define DELETE_CHECK 4
  11. #define RSET_CHECK  5
  12. #define STAT_CHECK  6
  13. #define NOOP_CHECK  7
  14. #define LIST_CHECK  8
  15. #define RETR_CHECK  9

  16. /////////////////////////////////////////////////////////////////////////////
  17. class CPop
  18. {
  19. public:
  20. BOOL List();
  21. CWordArray m_SizeOfMsg;
  22. CString GetErrorMessage();    // If there is any error this will return it method
  23. CString GetPassword();           // Getting Password stored in class
  24. void SetPassword(CString& Password);    // Setting Password in class
  25. CString GetUser();                   // Getting user name stored in class
  26. void SetUser(CString& User);  // Setting user name in class
  27. CString GetHost();                    // Getting Host name (email server name) stored in class
  28. void SetHost(CString& Host);    // Setting Host name (email server name) in class
  29. BOOL Connect();                    // Connecting to email server
  30. int GetTotalMailSize();                // it returns the Total Mail Size
  31. int GetNumberOfMails();            // It return the number of mails
  32. CString GetMsgContents();         
  33. BOOL Statistics();                    // issue the STAT command on email server
  34. BOOL Retrieve(int MsgNumber);    // Getting any particular mail message
  35. BOOL Reset();                        // issue the reset command on email server
  36. int  GetMessageSize(int MsgNumber);    // Return a size of any particular mail
  37. BOOL Noop();                     // issue the NOOP command on email server
  38. BOOL Disconnect();            // issue the QUIT command on email server
  39. BOOL Delete(int& MsgNumber); // Deleteing a particular message from email server
  40. BOOL Connect(CString& Host, CString& User, CString& Password);
  41. CPop();
  42. virtual ~CPop();

  43. private:
  44. CString m_ErrorMessage;
  45. BOOL CheckResponse(int ResponseType);
  46. CString m_Password;
  47. CString m_User;
  48. CString m_Host;
  49. CString m_MsgContents;
  50. int m_TotalSize;
  51. int m_NumberMail;
  52. CSocket m_PopServer;
  53. };
  54. /#endif // !defined(AFX_POP_H__A44B38B6_697C_11D1_881E_00001C302581__INCLUDED_)
  55.   

  56. /*-----------------------------------------------------------------------------------------------
  57. // Pop.cpp : Defines the class behaviors for the application.
  58. ---------------------------------------------------------------------------------------------------*/

  59. #include "stdafx.h"
  60. #include "Pop.h"

  61. #ifdef _DEBUG
  62. #define new DEBUG_NEW
  63. #undef THIS_FILE
  64. static char THIS_FILE[] = __FILE__;
  65. #endif

  66. ////////////////////////////////////////////////////////////////////
  67. // CPop Class
  68. //////////////////////////////////////////////////////////////////////

  69. //////////////////////////////////////////////////////////////////////
  70. // Construction/Destruction
  71. //////////////////////////////////////////////////////////////////////

  72. CPop::CPop()
  73. {
  74. m_PopServer.Create();
  75. }
  76. //{4EEC1C91-6BE1-11d1-8824-00001C302581}

  77. CPop::~CPop()
  78. {
  79. m_PopServer.Close();
  80. }

  81. BOOL CPop::Connect(CString & Host, CString & User, CString & Password)
  82. {
  83. char buf [512];

  84. if (!m_PopServer.Connect(Host,110)) // 110 Pop3 Port
  85. {
  86.   m_ErrorMessage = _T("Server cannot be connected");
  87.   return FALSE;
  88. }
  89. else
  90. {
  91.   if(CheckResponse(CONNECTION_CHECK)==FALSE)
  92.    return FALSE;

  93.   wsprintf (buf, "USER %s\r\n", (LPCSTR) User);
  94.   m_PopServer.Send(buf, strlen (buf));
  95.   if(CheckResponse(USER_CHECK)==FALSE)
  96.    return FALSE;

  97.   wsprintf (buf, "PASS %s\r\n", (LPCSTR) Password);
  98.   m_PopServer.Send(buf, strlen (buf));
  99.   if (CheckResponse(PASSWORD_CHECK)==FALSE)
  100.    return FALSE;

  101.   return TRUE;
  102. }

  103. }

  104. BOOL CPop::Delete(int & MsgNumber)
  105. {
  106. char buf [512];

  107. wsprintf (buf, "DELE %d\r\n",MsgNumber );
  108. m_PopServer.Send(buf, strlen (buf));
  109. if (CheckResponse(DELETE_CHECK)==FALSE)
  110.   return FALSE;
  111. else
  112.   return TRUE;
  113. }

  114. BOOL CPop::Disconnect()
  115. {
  116. char buf [512];

  117. wsprintf (buf, "QUIT \r\n");
  118. m_PopServer.Send(buf, strlen (buf));
  119. if (CheckResponse(QUIT_CHECK)==FALSE)
  120.   return FALSE;
  121. else
  122.   return TRUE;
  123. }

  124. BOOL CPop::Noop()
  125. {
  126. char buf [512];

  127. wsprintf (buf, "NOOP  \r\n");
  128. m_PopServer.Send(buf, strlen (buf));
  129. if (CheckResponse(NOOP_CHECK)==FALSE)
  130.   return FALSE;
  131. else
  132.   return TRUE;
  133. }

  134. // Return the Msg Size for given msg number
  135. int CPop::GetMessageSize(int MsgNumber)
  136. {
  137. if(m_SizeOfMsg.GetSize() < MsgNumber+1)
  138.   return 0;
  139. else
  140.   return m_SizeOfMsg[MsgNumber+1];
  141. }

  142. BOOL CPop::Reset()
  143. {
  144. char buf [512];

  145. wsprintf (buf, "RSET \r\n");
  146. m_PopServer.Send(buf, strlen (buf));
  147. if (CheckResponse(RSET_CHECK)==FALSE)
  148.   return FALSE;
  149. else
  150.   return TRUE;
  151. }

  152. // MsgContents will hold the msg body
  153. BOOL CPop::Retrieve(int  MsgNumber)
  154. {
  155. char buf [512];

  156. wsprintf (buf, "RETR %d\r\n",MsgNumber );
  157. m_PopServer.Send(buf, strlen (buf));
  158. if (CheckResponse(RETR_CHECK)==FALSE)
  159.   return FALSE;
  160. else
  161.   return TRUE;
  162. }

  163. BOOL CPop::Statistics()
  164. {
  165. char buf [512];

  166. wsprintf (buf, "STAT \r\n");
  167. m_PopServer.Send(buf, strlen (buf));
  168. if (CheckResponse(STAT_CHECK)==FALSE)
  169.   return FALSE;
  170. else
  171.   return TRUE;
  172. }

  173. CString CPop::GetMsgContents()
  174. {
  175. return m_MsgContents;
  176. }

  177. int CPop::GetNumberOfMails()
  178. {
  179. return m_NumberMail;
  180. }

  181. int CPop::GetTotalMailSize()
  182. {
  183. return m_TotalSize;
  184. }

  185. BOOL CPop::Connect()
  186. {
  187. Connect(m_Host, m_User, m_Password);
  188. return TRUE;
  189. }

  190. void CPop::SetHost(CString & Host)
  191. {
  192. m_Host = Host;
  193. }

  194. CString CPop::GetHost()
  195. {
  196. return m_Host;
  197. }

  198. void CPop::SetUser(CString & User)
  199. {
  200. m_User = User;
  201. }

  202. CString CPop::GetUser()
  203. {
  204. return m_User;
  205. }

  206. void CPop::SetPassword(CString & Password)
  207. {
  208. m_Password = Password;
  209. }

  210. CString CPop::GetPassword()
  211. {
  212. return m_Password;
  213. }

  214. BOOL CPop::CheckResponse(int ResponseType)
  215. {
  216. char buf [1000];

  217. for (int i=0;i<512;i++)
  218.   buf[i]='\0';

  219. m_PopServer.Receive(buf, sizeof(buf));

  220. switch (ResponseType)
  221. {
  222.   case CONNECTION_CHECK:
  223.    if (strnicmp(buf,"-ERR", 4) == 0)
  224.    {
  225.     m_ErrorMessage = _T("Bad Connection");
  226.     return FALSE;
  227.    }
  228.    break;

  229.   case USER_CHECK:
  230.    if (strnicmp(buf,"-ERR", 4) == 0)
  231.    {
  232.     m_ErrorMessage = _T("Bad User Name");
  233.     return FALSE;
  234.    }
  235.    break;
  236.   case PASSWORD_CHECK:
  237.    if (strnicmp(buf,"-ERR", 4) == 0)
  238.    {
  239.     m_ErrorMessage = _T("Bad Password Name");
  240.     return FALSE;
  241.    }
  242.    break;
  243.   case QUIT_CHECK:
  244.    if (strnicmp(buf,"-ERR", 4) == 0)
  245.    {
  246.     m_ErrorMessage = _T("Error occured during QUIT");
  247.     return FALSE;
  248.    }
  249.    break;
  250.   case DELETE_CHECK:
  251.    if (strnicmp(buf,"-ERR", 4) == 0)
  252.    {
  253.     m_ErrorMessage = _T("Error occured during DELE");
  254.     return FALSE;
  255.    }
  256.    break;
  257.   case RSET_CHECK:
  258.    if (strnicmp(buf,"-ERR", 4) == 0)
  259.    {
  260.     m_ErrorMessage = _T("Error occured during RSET");
  261.     return FALSE;
  262.    }
  263.    break;
  264.   case STAT_CHECK:
  265.    if (strnicmp(buf,"-ERR", 4) == 0)
  266.    {
  267.     m_ErrorMessage = _T("Error occured during STAT");
  268.     return FALSE;
  269.    }
  270.    else
  271.    {
  272.     BOOL EmailNumber = TRUE;
  273.     for (char *p = buf; *p != '\0'; p++)
  274.     {
  275.      if (*p == '\t' || *p == ' ')
  276.      {
  277.       if(EmailNumber == TRUE)
  278.       {
  279.        m_NumberMail = atoi(p);
  280.        EmailNumber = FALSE;
  281.       }
  282.       else
  283.       {
  284.        m_TotalSize = atoi(p);
  285.        return TRUE;
  286.       }
  287.      }
  288.     }

  289.    }
  290.    break;
  291.   case NOOP_CHECK:
  292.    if (strnicmp(buf,"-ERR", 4) == 0)
  293.    {
  294.     m_ErrorMessage = _T("Error occured during NOOP");
  295.     return FALSE;
  296.    }
  297.    break;

  298.   case LIST_CHECK:
  299.    if (strnicmp(buf,"-ERR", 4) == 0)
  300.    {
  301.     m_ErrorMessage = _T("Error occured during LIST");
  302.     return FALSE;
  303.    }
  304.    else
  305.    {
  306.     m_PopServer.Receive(buf, sizeof(buf));

  307.     for (char *p = buf; *p != '.'; p++)
  308.      if (*p == '\t' || *p == ' ')
  309.       m_SizeOfMsg.Add(atoi(p));
  310.    }
  311.    break;
  312.   case RETR_CHECK:
  313.    if (strnicmp(buf,"-ERR", 4) == 0)
  314.    {
  315.     m_ErrorMessage = _T("Error occured during RETR");
  316.     return FALSE;
  317.    }
  318.    else
  319.    {
  320.     char temp[9000];
  321.     m_PopServer.Receive(temp, sizeof(temp));
  322.     m_MsgContents = temp;
  323.    }
  324.    break;
  325. }
  326. return TRUE;
  327. }

  328. CString CPop::GetErrorMessage()
  329. {
  330. return m_ErrorMessage;
  331. }

  332. BOOL CPop::List()
  333. {
  334. char buf [512];

  335. wsprintf (buf, "LIST  \r\n");
  336. m_PopServer.Send(buf, strlen (buf));
  337. if (CheckResponse(LIST_CHECK)==FALSE)
  338.   return FALSE;
  339. else
  340.   return TRUE;
  341. }

复制代码
[MFC408]1[/MFC408]
[weixinlianxi]1[/weixinlianxi]







欢迎光临 工控编程吧 (https://www.gkbc8.com/) Powered by Discuz! X3.4