源文件:
#include <vcl.h>
#pragma hdrstop
#include "sendmail.h"
#include "mmsystem.h"
#pragma package(smart_init)
struct RecvEmailInfo
{
AnsiString strSubject, strFrom, strTo, strContent, strCmdDate, strCmdNum;
int nRecNum, nSendCount;
TList *AttachList;
};
struct AttachInfo
{
AnsiString FileName;
int Length;
BYTE *Content;
};
__fastcall TSendMailThread::TSendMailThread(bool CreateSuspended,int sleeptime )
: TThread(CreateSuspended)
{
Ftime = sleeptime;
FreeOnTerminate = true;
nSendFlag = -1;
nCommandKind = 0;
strBoundary = "----Mailor_of_qurqur.China----";
RecvEmailList= new TList;
/*TMemoryStream *ms = new TMemoryStream();
ms->LoadFromFile( "d:\\test.txt" );
RecvEmailInfo * pREI;
AttachInfo * pAI;
for( int i = 0; i < 5; i ++ )
{
pAI = new AttachInfo;
pREI = new RecvEmailInfo;
pAI->FileName = "test.txt";
pAI->Length = ms->Size;
pAI->Content = new BYTE[ ms->Size ];
ms->Position = 0;
ms->ReadBuffer( pAI->Content, ms->Size );
pREI->AttachList = new TList;
pREI->AttachList->Add( pAI );
pREI->strSubject = "humor";
pREI->strFrom = "[email protected]";
pREI->strTo = "[email protected];
pREI->strContent = "test";
pREI->nSendCount = 5;
RecvEmailList->Add( pREI );
}
delete ms;*/
}
void __fastcall TSendMailThread::Execute()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
// Tell the user that we could not find a usable WinSock DLL.
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
{
// Tell the user that we could not find a usable WinSock DLL.
WSACleanup( );
return;
}
while( !Terminated )
{
if( !bCanSendEmail )
{
Sleep( Ftime );
continue;
}
SendDeal();
Sleep( 10 );
}
}
int __fastcall TSendMailThread::InitSocket()
{
smtpclient = socket( AF_INET, SOCK_STREAM, 0 );
if( smtpclient == INVALID_SOCKET )
{
return -1;
}
HOSTENT* hostinfo = gethostbyname( strSMTPServer.c_str() );
if( hostinfo == NULL )
return -1;
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons( nSMTPPort );
memcpy( &server.sin_addr, *hostinfo->h_addr_list, sizeof( server.sin_addr ) );
int rval = connect( smtpclient, ( struct sockaddr* )&server, sizeof( server ) );
if( rval == SOCKET_ERROR )
{
// rval = WSAGetLastError();
return -1;
}
return 0;
}
void __fastcall TSendMailThread::SendDeal()
{
unsigned int new_time;
char aa[ 8200 ];
int err, len;
switch( nSendFlag )
{
case -1 :
if( HaveEmail() )
{
err = InitSocket();
if( err == -1 )
{
nSendFlag = 1;
strcpy( buf, "error" );
nCommandKind = 9;
}