⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pop3.cpp

📁 一个简单的pop3接收邮件程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		return false;
	}
	nSubStart = strMailHeader.Find("Subject:");
	if( -1 == nSubStart )
	{
		SetLastError("No Subject!");
		return false;
	}
	nSubEnd = strMailHeader.Find("\r\n" , nSubStart);
	*strSubject = strMailHeader.Mid(nSubStart + 9 , nSubEnd - (nSubStart + 9) );
	return true;
}

BOOL CPop3::GetMailSender(int nMailIndex, CString *strSender)
{
	CString strMailHeader;
	int nSubStart , nSubEnd;
	if( !GetMailHeader(nMailIndex , &strMailHeader) )
	{
		//SetLastError("Get Sender error!");
		return false;
	}
	nSubStart = strMailHeader.Find("From:");
	if( -1 == nSubStart )
	{
		SetLastError("No Sender!");
		return false;
	}
	nSubEnd = strMailHeader.Find("\r\n" , nSubStart);
	*strSender = strMailHeader.Mid(nSubStart + 6 , nSubEnd - (nSubStart + 6) );
	return true;
}

BOOL CPop3::GetMailReceiver(int nMailIndex, CString *strReceiver)
{
	CString strMailHeader;
	int nSubStart , nSubEnd;
	if( !GetMailHeader(nMailIndex , &strMailHeader) )
	{
		//SetLastError("Get Receiver error!");
		return false;
	}
	nSubStart = strMailHeader.Find("\nTo:");
	if( -1 == nSubStart )
	{
		SetLastError("No Receiver!");
		return false;
	}
	nSubEnd = strMailHeader.Find("\r\n" , nSubStart);
	*strReceiver = strMailHeader.Mid(nSubStart + 5 , nSubEnd - (nSubStart + 5) );
	return true;
}

BOOL CPop3::GetMailDate(int nMailIndex, CString *strDate)
{
	CString strMailHeader;
	int nSubStart , nSubEnd;
	if( !GetMailHeader(nMailIndex , &strMailHeader) )
	{
		//SetLastError("Get Date error!");
		return false;
	}
	nSubStart = strMailHeader.Find("Date:");
	if( -1 == nSubStart )
	{
		SetLastError("No Date!");
		return false;
	}
	nSubEnd = strMailHeader.Find("\r\n" , nSubStart);
	*strDate = strMailHeader.Mid(nSubStart + 6 , nSubEnd - (nSubStart + 6) );
	return true;
}

BOOL CPop3::DeleteMail(int nMailIndex)
{
	CString strResult;
	char msg[255];
	int ret;

	if( false == m_bSocketOK )
	{
		SetLastError("Socket not Create!");
		return false;
	}
	if( false == m_bConnected )
	{
		SetLastError("Not Connected!");
		return false;
	}

	sprintf(msg , "dele %d\r\n" , nMailIndex);
	ret = send(m_sPop3Socket , msg , strlen(msg) , NULL);
	if(ret == SOCKET_ERROR)
	{
		SetLastError("Socket error!");
		m_bSocketOK = false;
		m_bConnected = false;
		return false;
	}

	if( !GetSocketResult(&strResult , COMMAND_END_FLAG) )
		return false;
	if( 0 == strResult.Find('-' , 0) )
	{
		SetLastError("Delete Mail error!");
		return false;
	}

	return true;

}

BOOL CPop3::ResetMail()
{
	CString strResult;
	char msg[255];
	int ret;

	if( false == m_bSocketOK )
	{
		SetLastError("Socket not Create!");
		return false;
	}
	if( false == m_bConnected )
	{
		SetLastError("Not Connected!");
		return false;
	}

	sprintf(msg , "rset\r\n");
	ret = send(m_sPop3Socket , msg , strlen(msg) , NULL);
	if(ret == SOCKET_ERROR)
	{
		SetLastError("Socket error!");
		m_bSocketOK = false;
		m_bConnected = false;
		return false;
	}

	if( !GetSocketResult(&strResult , COMMAND_END_FLAG) )
		return false;
	if( 0 == strResult.Find('-' , 0) )
	{
		SetLastError("Reset Mail error!");
		return false;
	}

	return true;

}


BOOL CPop3::IsConnected()
{
	if( m_bConnected )
		return true;
	else
		return false;
}


/*********************************************************************************************
/* Function for MD5 start
**********************************************************************************************/
CString CPop3::MD5_GetMD5(BYTE* pBuf, UINT nLength)
{
	//entry invariants
	AfxIsValidAddress(pBuf,nLength,FALSE);

	//calculate and return the checksum
	//CMD5Checksum MD5Checksum;
	MD5_Init();
	MD5_Update( pBuf, nLength );
	return MD5_Final();
}


DWORD CPop3::MD5_RotateLeft(DWORD x, int n)
{
	//check that DWORD is 4 bytes long - true in Visual C++ 6 and 32 bit Windows
	ASSERT( sizeof(x) == 4 );

	//rotate and return x
	return (x << n) | (x >> (32-n));
}

void CPop3::MD5_FF( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD F = (B & C) | (~B & D);
	A += F + X + T;
	A = MD5_RotateLeft(A, S);
	A += B;
}

void CPop3::MD5_GG( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD G = (B & D) | (C & ~D);
	A += G + X + T;
	A = MD5_RotateLeft(A, S);
	A += B;
}

void CPop3::MD5_HH( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD H = (B ^ C ^ D);
	A += H + X + T;
	A = MD5_RotateLeft(A, S);
	A += B;
}

void CPop3::MD5_II( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)
{
	DWORD I = (C ^ (B | ~D));
	A += I + X + T;
	A = MD5_RotateLeft(A, S);
	A += B;
}

void CPop3::MD5_ByteToDWord(DWORD* Output, BYTE* Input, UINT nLength)
{
	//entry invariants
	ASSERT( nLength % 4 == 0 );
	ASSERT( AfxIsValidAddress(Output, nLength/4, TRUE) );
 	ASSERT( AfxIsValidAddress(Input, nLength, FALSE) );

	//initialisations
	UINT i=0;	//index to Output array
	UINT j=0;	//index to Input array

	//transfer the data by shifting and copying
	for ( ; j < nLength; i++, j += 4)
	{
		Output[i] = (ULONG)Input[j]			| 
					(ULONG)Input[j+1] << 8	| 
					(ULONG)Input[j+2] << 16 | 
					(ULONG)Input[j+3] << 24;
	}
}

void CPop3::MD5_Transform(BYTE Block[64])
{
	//initialise local data with current checksum
	ULONG a = m_lMD5[0];
	ULONG b = m_lMD5[1];
	ULONG c = m_lMD5[2];
	ULONG d = m_lMD5[3];

	//copy BYTES from input 'Block' to an array of ULONGS 'X'
	ULONG X[16];
	MD5_ByteToDWord( X, Block, 64 );

	//Perform Round 1 of the transformation
	MD5_FF (a, b, c, d, X[ 0], MD5_S11, MD5_T01); 
	MD5_FF (d, a, b, c, X[ 1], MD5_S12, MD5_T02); 
	MD5_FF (c, d, a, b, X[ 2], MD5_S13, MD5_T03); 
	MD5_FF (b, c, d, a, X[ 3], MD5_S14, MD5_T04); 
	MD5_FF (a, b, c, d, X[ 4], MD5_S11, MD5_T05); 
	MD5_FF (d, a, b, c, X[ 5], MD5_S12, MD5_T06); 
	MD5_FF (c, d, a, b, X[ 6], MD5_S13, MD5_T07); 
	MD5_FF (b, c, d, a, X[ 7], MD5_S14, MD5_T08); 
	MD5_FF (a, b, c, d, X[ 8], MD5_S11, MD5_T09); 
	MD5_FF (d, a, b, c, X[ 9], MD5_S12, MD5_T10); 
	MD5_FF (c, d, a, b, X[10], MD5_S13, MD5_T11); 
	MD5_FF (b, c, d, a, X[11], MD5_S14, MD5_T12); 
	MD5_FF (a, b, c, d, X[12], MD5_S11, MD5_T13); 
	MD5_FF (d, a, b, c, X[13], MD5_S12, MD5_T14); 
	MD5_FF (c, d, a, b, X[14], MD5_S13, MD5_T15); 
	MD5_FF (b, c, d, a, X[15], MD5_S14, MD5_T16); 

	//Perform Round 2 of the transformation
	MD5_GG (a, b, c, d, X[ 1], MD5_S21, MD5_T17); 
	MD5_GG (d, a, b, c, X[ 6], MD5_S22, MD5_T18); 
	MD5_GG (c, d, a, b, X[11], MD5_S23, MD5_T19); 
	MD5_GG (b, c, d, a, X[ 0], MD5_S24, MD5_T20); 
	MD5_GG (a, b, c, d, X[ 5], MD5_S21, MD5_T21); 
	MD5_GG (d, a, b, c, X[10], MD5_S22, MD5_T22); 
	MD5_GG (c, d, a, b, X[15], MD5_S23, MD5_T23); 
	MD5_GG (b, c, d, a, X[ 4], MD5_S24, MD5_T24); 
	MD5_GG (a, b, c, d, X[ 9], MD5_S21, MD5_T25); 
	MD5_GG (d, a, b, c, X[14], MD5_S22, MD5_T26); 
	MD5_GG (c, d, a, b, X[ 3], MD5_S23, MD5_T27); 
	MD5_GG (b, c, d, a, X[ 8], MD5_S24, MD5_T28); 
	MD5_GG (a, b, c, d, X[13], MD5_S21, MD5_T29); 
	MD5_GG (d, a, b, c, X[ 2], MD5_S22, MD5_T30); 
	MD5_GG (c, d, a, b, X[ 7], MD5_S23, MD5_T31); 
	MD5_GG (b, c, d, a, X[12], MD5_S24, MD5_T32); 

	//Perform Round 3 of the transformation
	MD5_HH (a, b, c, d, X[ 5], MD5_S31, MD5_T33); 
	MD5_HH (d, a, b, c, X[ 8], MD5_S32, MD5_T34); 
	MD5_HH (c, d, a, b, X[11], MD5_S33, MD5_T35); 
	MD5_HH (b, c, d, a, X[14], MD5_S34, MD5_T36); 
	MD5_HH (a, b, c, d, X[ 1], MD5_S31, MD5_T37); 
	MD5_HH (d, a, b, c, X[ 4], MD5_S32, MD5_T38); 
	MD5_HH (c, d, a, b, X[ 7], MD5_S33, MD5_T39); 
	MD5_HH (b, c, d, a, X[10], MD5_S34, MD5_T40); 
	MD5_HH (a, b, c, d, X[13], MD5_S31, MD5_T41); 
	MD5_HH (d, a, b, c, X[ 0], MD5_S32, MD5_T42); 
	MD5_HH (c, d, a, b, X[ 3], MD5_S33, MD5_T43); 
	MD5_HH (b, c, d, a, X[ 6], MD5_S34, MD5_T44); 
	MD5_HH (a, b, c, d, X[ 9], MD5_S31, MD5_T45); 
	MD5_HH (d, a, b, c, X[12], MD5_S32, MD5_T46); 
	MD5_HH (c, d, a, b, X[15], MD5_S33, MD5_T47); 
	MD5_HH (b, c, d, a, X[ 2], MD5_S34, MD5_T48); 

	//Perform Round 4 of the transformation
	MD5_II (a, b, c, d, X[ 0], MD5_S41, MD5_T49); 
	MD5_II (d, a, b, c, X[ 7], MD5_S42, MD5_T50); 
	MD5_II (c, d, a, b, X[14], MD5_S43, MD5_T51); 
	MD5_II (b, c, d, a, X[ 5], MD5_S44, MD5_T52); 
	MD5_II (a, b, c, d, X[12], MD5_S41, MD5_T53); 
	MD5_II (d, a, b, c, X[ 3], MD5_S42, MD5_T54); 
	MD5_II (c, d, a, b, X[10], MD5_S43, MD5_T55); 
	MD5_II (b, c, d, a, X[ 1], MD5_S44, MD5_T56); 
	MD5_II (a, b, c, d, X[ 8], MD5_S41, MD5_T57); 
	MD5_II (d, a, b, c, X[15], MD5_S42, MD5_T58); 
	MD5_II (c, d, a, b, X[ 6], MD5_S43, MD5_T59); 
	MD5_II (b, c, d, a, X[13], MD5_S44, MD5_T60); 
	MD5_II (a, b, c, d, X[ 4], MD5_S41, MD5_T61); 
	MD5_II (d, a, b, c, X[11], MD5_S42, MD5_T62); 
	MD5_II (c, d, a, b, X[ 2], MD5_S43, MD5_T63); 
	MD5_II (b, c, d, a, X[ 9], MD5_S44, MD5_T64); 

	//add the transformed values to the current checksum
	m_lMD5[0] += a;
	m_lMD5[1] += b;
	m_lMD5[2] += c;
	m_lMD5[3] += d;
}

void CPop3::MD5_Init()
{
	// zero members
	memset( m_lpszBuffer, 0, 64 );
	m_nCount[0] = m_nCount[1] = 0;

	// Load magic state initialization constants
	m_lMD5[0] = MD5_INIT_STATE_0;
	m_lMD5[1] = MD5_INIT_STATE_1;
	m_lMD5[2] = MD5_INIT_STATE_2;
	m_lMD5[3] = MD5_INIT_STATE_3;
}

void CPop3::MD5_DWordToByte(BYTE* Output, DWORD* Input, UINT nLength )
{
	//entry invariants
	ASSERT( nLength % 4 == 0 );
	ASSERT( AfxIsValidAddress(Output, nLength, TRUE) );
	ASSERT( AfxIsValidAddress(Input, nLength/4, FALSE) );

	//transfer the data by shifting and copying
	UINT i = 0;
	UINT j = 0;
	for ( ; j < nLength; i++, j += 4) 
	{
		Output[j] =   (UCHAR)(Input[i] & 0xff);
		Output[j+1] = (UCHAR)((Input[i] >> 8) & 0xff);
		Output[j+2] = (UCHAR)((Input[i] >> 16) & 0xff);
		Output[j+3] = (UCHAR)((Input[i] >> 24) & 0xff);
	}
}

CString CPop3::MD5_Final()
{
	//Save number of bits
	BYTE Bits[8];
	MD5_DWordToByte( Bits, m_nCount, 8 );

	//Pad out to 56 mod 64.
	UINT nIndex = (UINT)((m_nCount[0] >> 3) & 0x3f);
	UINT nPadLen = (nIndex < 56) ? (56 - nIndex) : (120 - nIndex);
	MD5_Update( PADDING, nPadLen );

	//Append length (before padding)
	MD5_Update( Bits, 8 );

	//Store final state in 'lpszMD5'
	const int nMD5Size = 16;
	unsigned char lpszMD5[ nMD5Size ];
	MD5_DWordToByte( lpszMD5, m_lMD5, nMD5Size );

	//Convert the hexadecimal checksum to a CString
	CString strMD5;
	for ( int i=0; i < nMD5Size; i++) 
	{
		CString Str;
		if (lpszMD5[i] == 0) {
			Str = CString("00");
		}
		else if (lpszMD5[i] <= 15) 	{
			Str.Format("0%x",lpszMD5[i]);
		}
		else {
			Str.Format("%x",lpszMD5[i]);
		}

		ASSERT( Str.GetLength() == 2 );
		strMD5 += Str;
	}
	ASSERT( strMD5.GetLength() == 32 );
	return strMD5;
}

void CPop3::MD5_Update( BYTE* Input,	ULONG nInputLen )
{
	//Compute number of bytes mod 64
	UINT nIndex = (UINT)((m_nCount[0] >> 3) & 0x3F);

	//Update number of bits
	if ( ( m_nCount[0] += nInputLen << 3 )  <  ( nInputLen << 3) )
	{
		m_nCount[1]++;
	}
	m_nCount[1] += (nInputLen >> 29);

	//Transform as many times as possible.
	UINT i=0;		
	UINT nPartLen = 64 - nIndex;
	if (nInputLen >= nPartLen) 	
	{
		memcpy( &m_lpszBuffer[nIndex], Input, nPartLen );
		MD5_Transform( m_lpszBuffer );
		for (i = nPartLen; i + 63 < nInputLen; i += 64) 
		{
			MD5_Transform( &Input[i] );
		}
		nIndex = 0;
	} 
	else 
	{
		i = 0;
	}

	// Buffer remaining input
	memcpy( &m_lpszBuffer[nIndex], &Input[i], nInputLen-i);
}
/*********************************************************************************************
/* Function for MD5 end
**********************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -