Exemplo n.º 1
0
BOOL CSMTP::Connect()
{
	CString sHello;
	TCHAR local_host[ 80 ];	// Warning: arbitrary size
	if( m_bConnected )
		return TRUE;

	if( !m_wsSMTPServer.Create() )
	{
		m_sError = _T( "Unable to create the socket." );
		return FALSE;
	}
	if( !m_wsSMTPServer.Connect( GetServerHostName(), GetPort() ) )
	{
		m_sError = _T( "Unable to connect to server" );
		m_wsSMTPServer.Close();
		return FALSE;
	}
	if( !get_response( CONNECT_SUCCESS ) )
	{
		m_sError = _T( "Server didn't respond." );
		m_wsSMTPServer.Close();
		return FALSE;
	}
	gethostname( local_host, 80 );
	sHello.Format( "HELO %s\r\n", local_host );
	m_wsSMTPServer.Send( (LPCTSTR)sHello, sHello.GetLength() );
	if( !get_response( GENERIC_SUCCESS ) )
	{
		m_wsSMTPServer.Close();
		return FALSE;
	}
	m_bConnected = TRUE;
	return TRUE;
}
Exemplo n.º 2
0
BOOL CSMTP::Connect()
{
	CString sHello;
	TCHAR local_host[ 80 ];	// Warning: arbitrary size
	if( m_bConnected )
		return TRUE;
	
	try
	{
		// This will be deleted in Disconnect();
		response_buf = new TCHAR[ RESPONSE_BUFFER_SIZE ];

		// I can't count on all class users' applications
		// to have exception-throwing operator-new implementations,
		// so I'll soul-kiss the ones that don't.
		if( response_buf == NULL )
		{
			m_sError = _T( "Not enough memory" );
			return FALSE;
		}
	}
	catch( CException *e )
	{
		response_buf = NULL;
		m_sError = _T( "Not enough memory" );
		delete e;
		return FALSE;
	}

	if( !m_wsSMTPServer.Create() )
	{
		m_sError = _T( "Unable to create the socket." );
		delete response_buf;
		response_buf = NULL;
		return FALSE;
	}
	if( !m_wsSMTPServer.Connect( GetServerHostName(), GetPort() ) )
	{
		m_sError = _T( "Unable to connect to server" );
		m_wsSMTPServer.Close();
		delete response_buf;
		response_buf = NULL;
		return FALSE;
	}
	if( !get_response( CONNECT_SUCCESS ) )
	{
		m_sError = _T( "Server didn't respond." );
		m_wsSMTPServer.Close();
		delete response_buf;
		response_buf = NULL;
		return FALSE;
	}
	gethostname( local_host, 80 );
	sHello.Format( _T( "HELO %s\r\n" ), local_host );
	m_wsSMTPServer.Send( (LPCTSTR)sHello, sHello.GetLength() );
	if( !get_response( GENERIC_SUCCESS ) )
	{
		m_wsSMTPServer.Close();
		delete response_buf;
		response_buf = NULL;
		return FALSE;
	}
	m_bConnected = TRUE;
	return TRUE;
}
Exemplo n.º 3
0
BOOL CSMTP::Connect(int nNeedAuth, CString vUid, CString vPwd)
{
	CString sHello;
	TCHAR local_host[ 80 ];	// Warning: arbitrary size
	if( m_bConnected )
		return TRUE;
	
	try
	{
		// This will be deleted in Disconnect();
		response_buf = new TCHAR[ RESPONSE_BUFFER_SIZE ];

		// I can't count on all class users' applications
		// to have exception-throwing operator-new implementations,
		// so I'll soul-kiss the ones that don't.
		if( response_buf == NULL )
		{
			//m_sError = _T( "内存溢出" );//<%IDS_SMTP_2%>
			m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_2%>");
			return FALSE;
		}
	}
	catch( CException *e )
	{
		response_buf = NULL;
		//m_sError = _T( "内存溢出" );//<%IDS_SMTP_3%>
		m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_3%>");
		delete e;
		return FALSE;
	}

	m_wsSMTPServer = socket(AF_INET, SOCK_STREAM, 0);
	if(m_wsSMTPServer == INVALID_SOCKET)
	{
		//m_sError = _T( "通信建立失败" );//<%IDS_SMTP_4%>
		m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_4%>");
		delete response_buf;
		response_buf = NULL;
		return FALSE;
	}

	struct hostent *hp;
	struct sockaddr_in serv;
	hp = gethostbyname(GetServerHostName());
	if(hp == NULL)
	{
		//m_sError = _T( "域名解析失败" );//<%IDS_SMTP_5%>
		m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_5%>");
		shutdown(m_wsSMTPServer, 0x02);
		closesocket(m_wsSMTPServer);
		delete response_buf;
		response_buf = NULL;
		return FALSE;
	}

	int i = 0;
	while(1)
	{
		if(hp->h_addr_list[i])
			memcpy(&serv.sin_addr,hp->h_addr_list[i],hp->h_length);
		else
		{
			//m_sError = _T( "主机连接失败" );//<%IDS_SMTP_6%>
			m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_6%>");
			shutdown(m_wsSMTPServer, 0x02);
			closesocket(m_wsSMTPServer);
			delete response_buf;
			response_buf = NULL;
			return FALSE;
		}
		serv.sin_family = AF_INET;
		serv.sin_port=htons(GetPort());
//		if (connect(m_wsSMTPServer, (struct sockaddr*)&serv, sizeof(serv)) != SOCKET_ERROR)
		if (connect_timeo(m_wsSMTPServer, (struct sockaddr*)&serv, sizeof(serv), CONNECT_TIMEOUT) == 0)
		{
			break;
		}
		i ++;
	}

	if( !get_response( CONNECT_SUCCESS ) )
	{
		//m_sError = _T( "主机无响应" );//<%IDS_SMTP_7%>
		m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_7%>");
		shutdown(m_wsSMTPServer, 0x02);
		closesocket(m_wsSMTPServer);
		delete response_buf;
		response_buf = NULL;
		return FALSE;
	}

	gethostname( local_host, 80 );
	if(nNeedAuth)
		sHello.Format( _T( "EHLO %s\r\n" ), local_host );
	else
		sHello.Format( _T( "HELO %s\r\n" ), local_host );
	send(m_wsSMTPServer, (LPCTSTR)sHello, sHello.GetLength(), 0);
	if( !get_response( GENERIC_SUCCESS ) )
	{
		//m_sError = _T("发送HELO命令失败");//<%IDS_SMTP_8%>
		m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_8%>");
		shutdown(m_wsSMTPServer, 0x02);
		closesocket(m_wsSMTPServer);
		delete response_buf;
		response_buf = NULL;
		return FALSE;
	}
	//get_response( GENERIC_SUCCESS ) ;
/*
	if( !get_response( GENERIC_SUCCESS ) )
	{
		//m_sError = _T("发送HELO命令失败");//<%IDS_SMTP_8%>
		m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_8%>");
		shutdown(m_wsSMTPServer, 0x02);
		closesocket(m_wsSMTPServer);
		delete response_buf;
		response_buf = NULL;
		return FALSE;
	}
	*/
	if(nNeedAuth)
	{
		CString sAuth = _T("");
		CBase64 *pBase64 = new CBase64();

		sAuth = _T("AUTH LOGIN\r\n");
		send(m_wsSMTPServer, (LPCTSTR)sAuth, sAuth.GetLength(), 0);
		if( !get_response( AUTH_RESPONSE ) )
		{
			//m_sError = _T("身份验证失败");//<%IDS_SMTP_9%>
			m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_9%>");
			shutdown(m_wsSMTPServer, 0x02);
			closesocket(m_wsSMTPServer);
			delete pBase64;
			pBase64 = NULL;
			delete response_buf;
			response_buf = NULL;
			return FALSE;
		}
		//get_response( AUTH_RESPONSE );

		sAuth.Format(_T("%s\r\n"), pBase64->Encode((LPCTSTR)vUid, vUid.GetLength()));
		send(m_wsSMTPServer, (LPCTSTR)sAuth, sAuth.GetLength(), 0);
		if( !get_response( AUTH_RESPONSE ) )
		{
			//m_sError = _T("发送身份验证用户名失败");//<%IDS_SMTP_10%>
			m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_10%>");
			shutdown(m_wsSMTPServer, 0x02);
			closesocket(m_wsSMTPServer);
			delete pBase64;
			pBase64 = NULL;
			delete response_buf;
			response_buf = NULL;
			return FALSE;
		}

		sAuth.Format(_T("%s\r\n"), pBase64->Encode((LPCTSTR)vPwd, vPwd.GetLength()));
		send(m_wsSMTPServer, (LPCTSTR)sAuth, sAuth.GetLength(), 0);
		if( !get_response( AUTH_SUCCESS ) )
		{
			//m_sError = _T("发送身份验证密码失败");//<%IDS_SMTP_11%>
			m_sError = ::FuncGetStringFromIDS("<%IDS_SMTP_11%>");
			shutdown(m_wsSMTPServer, 0x02);
			closesocket(m_wsSMTPServer);
			delete pBase64;
			pBase64 = NULL;
			delete response_buf;
			response_buf = NULL;
			return FALSE;
		}

		delete pBase64;
		pBase64 = NULL;
	}

	m_bConnected = TRUE;
	return TRUE;
}