Email.cpp
上传用户:jpdph_love
上传日期:2013-04-11
资源大小:116k
文件大小:6k
- // 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;
- //zfl
- lpszEmailAddress="zfl-123@tom.com";
- lpszMessage="dhsfsdflkklflsdkklfdklfk";
- lpszFrom ="zoufenglin@21cn.com";
- lpszHost ="smtp.21cn.com";
- lpszSubject ="jkjkfjdfjk";
- 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 );
- MailSocket.Create();
- // 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 %srn", 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>rn",
- 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>rn",
- lpInfo->szEmailAddress );
- if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
- NULL ) )
- goto EndSendThread;
- // Format a DATA string and send it to the host.
- strcpy( cbBuffer, "DATArn" );
- if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
- NULL ) )
- goto EndSendThread;
- // Format the email message string and send it
- // to the host.
- wsprintf( cbBuffer,
- "Subject: %srnTo: %srn%srnrn.rn",
- 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 );
- }