// 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 );
}
发送EMail的小程序 VC源码-网络编程-.rar
需积分: 4 150 浏览量
2008-10-07
21:37:36
上传
评论
收藏 1.14MB RAR 举报
xiongwang2008
- 粉丝: 0
- 资源: 1
最新资源
- onenet_chongwukanhu_v06.apk
- 2022NOC软件创意编程赛项真题C++初中-决赛(有解析)
- 2022NOC软件创意编程赛项真题python初中-决赛(有解析)
- openLayer-本地数据加载 (day5)
- A题-正弦信号发生器.xdf
- 2022NOC软件创意编程赛项真题python小学高年级-决赛(有解析)
- mathml转换latex需要的xsl文件
- 2022NOC软件创意编程赛项真题图形化小学高年级-决赛(有解析)
- gbase驱动下载gbase-connector-java-8.3.81.53驱动下载
- 2022NOC软件创意编程赛项真题图形化小学低年级-决赛(有解析)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈