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

📄 email.cpp

📁 发送邮件的小程序
💻 CPP
字号:
// Email.cpp

#include "stdafx.h"
#include "Email.h"

CEmail::CEmail()
{

	// Set the thread pointer and the
	// thread to NULL.
	m_pSendEmailThread = NULL;
	m_hThread = NULL;

}

CEmail::~CEmail()
{

	// Set the abort flag to TRUE.
	m_Info.bAbort = TRUE;

	// See if the thread pointer and the
	// thread handle are NULL.
	if( m_pSendEmailThread && m_hThread != NULL )

		// Wait for the thread to end.
		::WaitForSingleObject( m_hThread, 15000 );

}

void CEmail::Send( const char *lpszEmailAddress,
	const char *lpszMessage, const char *lpszFrom,
	const char *lpszHost, const char *lpszSubject )
{

	// If the thread pointer is not NULL,
	// a thread has already been created.
	if( m_pSendEmailThread != NULL )
		return;

	// Populate the EMAIL_INFO structure with
	// information such as the email address and
	// the message data.
	m_Info.pSendEmailThread = &m_pSendEmailThread;
	strcpy( m_Info.szEmailAddress, lpszEmailAddress );
	strcpy( m_Info.szMessage, lpszMessage );
	strcpy( m_Info.szFrom, lpszFrom );
	strcpy( m_Info.szHost, lpszHost );
	strcpy( m_Info.szSubject, lpszSubject );

	// Set the abort and completed flags to
	// FALSE, set the socket to INVALID_SOCKET
	// so we know there's no socket created yet.
	m_Info.bAbort = FALSE;
	m_Info.bCompleted = FALSE;
	m_Info.hSocket = INVALID_SOCKET;

	// Kick off the thread.
	m_pSendEmailThread =
		AfxBeginThread( CEmail::SendThread,(LPVOID)&m_Info );

	// Store the thread handle for later.
	m_hThread = m_pSendEmailThread->m_hThread;

}

BOOL CEmail::SendSocketData( EMAIL_INFO *lpInfo,
	char *cbBuffer, CSocket& MailSocket,
	const char *lpszToken )
{

	// Sleep so that the buffer will clear.
	Sleep( 100 );
	// Send the data.
	MailSocket.Send( cbBuffer, strlen( cbBuffer ) );

	// Check the last error and store it. If there's
	// an error other than WSAEWOULDBLOCK, bail out.
	lpInfo->dwLastError = GetLastError();
	if( lpInfo->dwLastError != 0 &&
		lpInfo->dwLastError != WSAEWOULDBLOCK )
		goto SendSocketDataError;
	lpInfo->dwLastError = 0;

	// Sleep so the buffer has time to empty.
	Sleep( 100 );
	// Clear the incoming buffer.
	memset( cbBuffer, 0, BUFFERSIZE );

	// Look for incoming data.
	MailSocket.Receive( cbBuffer, BUFFERSIZE );

	// Check the last error and store it. If there's
	// an error other than WSAEWOULDBLOCK, bail out.
	lpInfo->dwLastError = GetLastError();
	if( lpInfo->dwLastError != 0 &&
		lpInfo->dwLastError != WSAEWOULDBLOCK )
		goto SendSocketDataError;
	lpInfo->dwLastError = 0;

	// There are times (when lpszToken != NULL) that we
	// want to compare lpszToken to the incoming buffer.
	if( lpszToken != NULL && strnicmp( cbBuffer, lpszToken,
		strlen( lpszToken ) ) ){
		strcpy( lpInfo->szErrorMessage, cbBuffer );
		lpInfo->dwLastError = -10000;
		goto SendSocketDataError;
		}

	return( TRUE );

SendSocketDataError:
	return( FALSE );

}

UINT CEmail::SendThread( LPVOID lpInf )
{
	EMAIL_INFO *lpInfo = (EMAIL_INFO *) lpInf;

	// Declare a buffer for data transfer.
	char cbBuffer[BUFFERSIZE];

	CSocket MailSocket;
	int nBytesRead;
	BOOL bMessageTypeOK;

	// Create a socket for the transfer.
	MailSocket.Create( 25 );
	// Connect to the host.
	MailSocket.Connect( lpInfo->szHost, 25 );

	// Check for an error. If there's any other
	// error besides WSAEWOULDBLOCK, we don't do
	// the following code.
	lpInfo->dwLastError = GetLastError();
	if( lpInfo->dwLastError == 0 ||
		lpInfo->dwLastError == WSAEWOULDBLOCK ){

		// Set last error to 0 and store the
		// socket in the structure.
		lpInfo->dwLastError = 0;
		lpInfo->hSocket = MailSocket.m_hSocket;

		// Let the socket stabilize before we look
		// for data/
		Sleep( 100 );

		// Look for incoming data.
		nBytesRead =
			MailSocket.Receive( cbBuffer, sizeof( cbBuffer ) );

		// See if we got a '220 ' at the beginning of
		// the data we got back.
		bMessageTypeOK =
			!( strnicmp( cbBuffer, "220 ", 4 ) );

		// If we didn't get a '220 ' take the
		// appropriate action.
		if( !bMessageTypeOK ){
			strcpy( lpInfo->szErrorMessage, cbBuffer );
			lpInfo->dwLastError = -10000;
			goto EndSendThread;
			}

		// Look for an error. If we got anything
		// other than WSAAEWOULDBLOCK, bail out.
		lpInfo->dwLastError = GetLastError();
		if( lpInfo->dwLastError != 0 &&
			lpInfo->dwLastError != WSAEWOULDBLOCK )
			goto EndSendThread;

		// If we got data...
		if( nBytesRead > 0 ){

			// Format a HELO string to send to the host.
			wsprintf( cbBuffer, "HELO %s\r\n", lpInfo->szHost);
			if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
				"250 " ) )
				goto EndSendThread;

			// Format a FROM string and send it to the host.
			wsprintf( cbBuffer, "MAIL FROM: <%s>\r\n",
				lpInfo->szFrom );
			if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
				NULL ) )
				goto EndSendThread;

			// Format a RCPT string and send it to the host.
			wsprintf( cbBuffer, "RCPT to: <%s>\r\n",
				lpInfo->szEmailAddress );
			if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
				NULL ) )
				goto EndSendThread;

			// Format a DATA string and send it to the host.
			strcpy( cbBuffer, "DATA\r\n" );
			if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
				NULL ) )
				goto EndSendThread;

			// Format the email message string and send it
			// to the host.
			wsprintf( cbBuffer,
				"Subject: %s\r\nTo: %s\r\n%s\r\n\r\n.\r\n",
				lpInfo->szSubject, lpInfo->szEmailAddress,
				lpInfo->szMessage );
			if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
				NULL ) )
				goto EndSendThread;

			}
		}

EndSendThread:
	// Clean up.
	lpInfo->hSocket = INVALID_SOCKET;
	lpInfo->pSendEmailThread[0] = NULL;
	lpInfo->bCompleted = TRUE;

	return( 0 );

}

⌨️ 快捷键说明

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