- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
Pop3.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:6k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // Pop3.cpp: implementation of the CPop3 class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Pop3.h"
- #include "stdio.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- #pragma comment( lib , "ws2_32.lib")
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- int CPop3::initNumber = 0;
- CPop3::CPop3()
- {
- if( ! CPop3::initNumber )
- {
- CPop3::initNumber ++;
- WSADATA wsaData;
- ::WSAStartup( MAKEWORD( 2 , 0 ) , &wsaData );
- }
- this->m_sock = INVALID_SOCKET;
- this->mailNumber = 0;
- this->mailString = "";
- this->curIndex = 0;
- this->from = "";
- this->to = "";
- this->subject = "";
- this->body = "";
- }
- CPop3::~CPop3()
- {
- this->Quit( );
- CPop3::initNumber --;
- if( ! CPop3::initNumber )
- ::WSACleanup( );
- }
- bool CPop3::Connect( const char * username , const char * password , const char * ip , const int port )
- {
- if( this->m_sock != INVALID_SOCKET )
- this->Quit( );
- if( ( this->m_sock = ::socket( AF_INET , SOCK_STREAM , IPPROTO_TCP ) ) == INVALID_SOCKET )
- return false;
- struct hostent * p = ::gethostbyname( ip );
- struct sockaddr_in addr;
- if( p )
- memcpy( &addr.sin_addr , p->h_addr , p->h_length );
- else
- addr.sin_addr.s_addr = ::inet_addr( ip );
- addr.sin_family = AF_INET;
- addr.sin_port = htons( port );
- if( connect( this->m_sock , ( struct sockaddr * )&addr , sizeof( addr ) ) == SOCKET_ERROR )
- {
- this->Quit( );
- return false;
- }
- char sendbuf[ 128 ];
- char recvbuf[ 128 ];
- // Recv POP3 server welcome message
- int rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
- if ( rs <= 0 || strncmp( recvbuf , "+OK", 3 ) != 0 )
- {
- this->Quit( ); return false;
- }
- //Send USER command
- sprintf( sendbuf , "USER %srn" , username );
- ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
- rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
- if ( rs <= 0 || strncmp( recvbuf, "+OK" , 3 ) != 0 )
- {
- this->Quit( );
- return false;
- }//Send PASS command
- sprintf( sendbuf, "PASS %srn", password );
- ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
- rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
- if ( rs <= 0 || strncmp( recvbuf, "+OK" , 3 ) != 0 )
- {
- this->Quit( );
- return false;
- }
- return true;
- }
- void CPop3::Quit( )
- {
- this->mailNumber = 0;
- if( this->m_sock != INVALID_SOCKET )
- {
- char sendbuf[ 128 ];
- char recvbuf[ 128 ];
- // Send QUIT command
- sprintf(sendbuf, "QUITrn");
- ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
- //接收回复信息
- int rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
- ::closesocket( this->m_sock );
- this->m_sock = INVALID_SOCKET;
- }
- }
- int CPop3::List( void )
- {
- this->mailNumber = 0;
- /* Send LIST command */
- char sendbuf[ 128 ];
- char recvbuf[ 256 ];
- sprintf(sendbuf, "LIST rn");
- ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
- //接收回复信息
- int rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 );
- if ( rs <= 0 || strncmp( recvbuf, "+OK" , 3 ) != 0 )
- return this->mailNumber;
- char * p = strstr(recvbuf , "rn" );
- if ( p == NULL )
- return this->mailNumber;
- p = strstr( p + 2 , "rn" );
- if ( p == NULL )
- return this->mailNumber;
- while ( ( p = strstr( p + 2 , "rn" ) ) != NULL )
- this->mailNumber ++;
- return this->mailNumber;
- }
- bool CPop3::Fetch( int index )
- {
- if( index <= 0 || index > this->mailNumber )
- return false;
- this->curIndex = index;
- char sendbuf[128];
- char recvbuf[10240];
- /* Send RETR command */
- sprintf( sendbuf , "RETR %drn" , index );
- ::send( this->m_sock , sendbuf , strlen( sendbuf ) , 0 );
- int rs;
- this->mailString = "";
- this->from = "";
- this->to = "";
- this->subject = "";
- this->body = "";
- do
- {
- if( ( rs = ::recv( this->m_sock , recvbuf , sizeof( recvbuf ) , 0 ) ) <= 0 )
- return false;
- recvbuf[ rs ] = '';
- this->mailString += recvbuf;
- } while( strstr( recvbuf , "rn.rn" ) == NULL );
- return true;
- }
- const char * CPop3::GetFrom( void )
- {
- if( this->mailString.IsEmpty( ) )
- return NULL;
- int index1 = this->mailString.Find( "From:" , 0 );
- if( index1 == -1 )
- return NULL;
- index1 += 5;
- int index2 = this->mailString.Find( "rn" , index1 + 1 );
- this->from = this->mailString.Mid( index1 , index2 - index1 );
- return this->from;
- }
- const char * CPop3::GetTo( void )
- {
- int index1 = this->mailString.Find( "To:" , 0 );
- if( index1 == -1 )
- return NULL;
- index1 += 3;
- int index2 = this->mailString.Find( "rn" , index1 + 1 );
- this->to = this->mailString.Mid( index1 , index2 - index1 );
- return this->to;
- }
- const char * CPop3::GetSubject( void )
- {
- int index1 = this->mailString.Find( "Subject:" , 0 );
- if( index1 == -1 )
- return NULL;
- index1 += 9;
- int index2 = this->mailString.Find( "rn" , index1 + 1 );
- this->subject = this->mailString.Mid( index1 , index2 - index1 );
- return this->subject;
- }
- const char * CPop3::GetBody( void )
- {
- int index = this->mailString.Find( "rnrn" , 0 );
- if( index == -1 )
- return NULL;
- this->body = this->mailString.Right( this->mailString.GetLength( ) - index );
- return this->body;
- }