Pop3.cpp
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:6k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. // Pop3.cpp: implementation of the CPop3 class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Pop3.h"
  6. #include "stdio.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. #pragma comment( lib , "ws2_32.lib")
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. int CPop3::initNumber = 0;
  17. CPop3::CPop3()
  18. {
  19. if( ! CPop3::initNumber )
  20. {
  21. CPop3::initNumber ++;
  22. WSADATA wsaData;
  23. ::WSAStartup( MAKEWORD( 2 , 0 ) , &wsaData );
  24. }
  25. this->m_sock = INVALID_SOCKET;
  26. this->mailNumber = 0;
  27. this->mailString = "";
  28. this->curIndex = 0;
  29. this->from = "";
  30. this->to = "";
  31. this->subject = "";
  32. this->body = "";
  33. }
  34. CPop3::~CPop3()
  35. {
  36. this->Quit( );
  37. CPop3::initNumber --;
  38. if( ! CPop3::initNumber )
  39. ::WSACleanup( );
  40. }
  41. bool CPop3::Connect( const char * username , const char * password , const char * ip , const int port )
  42. {
  43. if( this->m_sock != INVALID_SOCKET )
  44. this->Quit( );
  45. if( ( this->m_sock = ::socket( AF_INET , SOCK_STREAM , IPPROTO_TCP ) ) == INVALID_SOCKET )
  46. return false;
  47. struct hostent * p = ::gethostbyname( ip );
  48. struct sockaddr_in addr;
  49. if( p )
  50. memcpy( &addr.sin_addr , p->h_addr , p->h_length );
  51. else
  52. addr.sin_addr.s_addr = ::inet_addr( ip );
  53. addr.sin_family = AF_INET;
  54. addr.sin_port = htons( port );
  55. if( connect( this->m_sock , ( struct sockaddr * )&addr , sizeof( addr ) ) == SOCKET_ERROR )
  56. {
  57. this->Quit( ); 
  58. return false;
  59. }
  60. char sendbuf[ 128 ];
  61. char recvbuf[ 128 ];
  62. // Recv POP3 server welcome message
  63. int rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
  64. if ( rs <= 0 || strncmp( recvbuf , "+OK", 3 ) != 0 ) 
  65. {
  66. this->Quit( ); return false;
  67. }
  68.     //Send USER command
  69. sprintf( sendbuf , "USER %srn" , username );
  70. ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
  71. rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
  72. if ( rs <= 0 || strncmp( recvbuf, "+OK" , 3 ) != 0 ) 
  73. {
  74. this->Quit( );
  75. return false;
  76. }//Send PASS command 
  77. sprintf( sendbuf, "PASS %srn", password );
  78. ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
  79. rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
  80. if ( rs <= 0 || strncmp( recvbuf, "+OK" , 3 ) != 0 ) 
  81. {
  82. this->Quit( );
  83. return false;
  84. }
  85. return true;
  86. }
  87. void CPop3::Quit( )
  88. {
  89. this->mailNumber = 0;
  90. if( this->m_sock != INVALID_SOCKET )
  91. {
  92. char sendbuf[ 128 ];
  93. char recvbuf[ 128 ];
  94.         // Send QUIT command 
  95. sprintf(sendbuf, "QUITrn");
  96. ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
  97. //接收回复信息
  98. int rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
  99. ::closesocket( this->m_sock );
  100. this->m_sock = INVALID_SOCKET;
  101. }
  102. }
  103. int CPop3::List( void )
  104. {
  105. this->mailNumber = 0;
  106. /* Send LIST command */
  107. char sendbuf[ 128 ];
  108. char recvbuf[ 256 ];
  109. sprintf(sendbuf, "LIST rn");
  110. ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
  111. //接收回复信息
  112. int rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
  113. if ( rs <= 0 || strncmp( recvbuf, "+OK" , 3 ) != 0 ) 
  114. return this->mailNumber;
  115. char * p = strstr(recvbuf , "rn" );
  116. if ( p == NULL )
  117. return this->mailNumber;
  118. p = strstr( p + 2 , "rn" );
  119. if ( p == NULL )
  120. return this->mailNumber;
  121. while ( ( p = strstr( p + 2 , "rn" ) ) != NULL ) 
  122. this->mailNumber ++;
  123. return this->mailNumber;
  124. }
  125. bool CPop3::Fetch( int index )
  126. {
  127. if( index <= 0 || index > this->mailNumber )
  128. return false;
  129. this->curIndex = index;
  130. char sendbuf[128];
  131. char recvbuf[10240];
  132. /* Send RETR command */
  133. sprintf( sendbuf , "RETR %drn" , index );
  134. ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
  135. int rs;
  136. this->mailString = "";
  137. this->from = "";
  138. this->to = "";
  139. this->subject = "";
  140. this->body = "";
  141. do 
  142. {
  143. if( ( rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 ) ) <= 0 )
  144. return false;
  145. recvbuf[ rs ] = '';
  146. this->mailString += recvbuf;
  147. } while( strstr( recvbuf , "rn.rn" ) == NULL );
  148. return true;
  149. }
  150. const char * CPop3::GetFrom( void )
  151. {
  152. if( this->mailString.IsEmpty( ) )
  153. return NULL;
  154. int index1 = this->mailString.Find( "From:" , 0 );
  155. if( index1 == -1 )
  156. return NULL;
  157. index1 += 5;
  158. int index2 = this->mailString.Find( "rn" , index1 + 1 );
  159. this->from = this->mailString.Mid( index1 , index2 - index1 );
  160. return this->from;
  161. }
  162. const char * CPop3::GetTo( void )
  163. {
  164. int index1 = this->mailString.Find( "To:" , 0 );
  165. if( index1 == -1 )
  166. return NULL;
  167. index1 += 3;
  168. int index2 = this->mailString.Find( "rn" , index1 + 1 );
  169. this->to = this->mailString.Mid( index1 , index2 - index1 );
  170. return this->to;
  171. }
  172. const char * CPop3::GetSubject( void )
  173. {
  174. int index1 = this->mailString.Find( "Subject:" , 0 );
  175. if( index1 == -1 )
  176. return NULL;
  177. index1 += 9;
  178. int index2 = this->mailString.Find( "rn" , index1 + 1 );
  179. this->subject = this->mailString.Mid( index1 , index2 - index1 );
  180. return this->subject;
  181. }
  182. const char * CPop3::GetBody( void )
  183. {
  184. int index = this->mailString.Find( "rnrn" , 0 );
  185. if( index == -1 )
  186. return NULL;
  187. this->body = this->mailString.Right( this->mailString.GetLength( ) - index );
  188. return this->body;
  189. }