if (!socket.Create(0, SOCK_STREAM))
{
AfxMessageBox("Could not create socket!");
res=socket.GetLastError();
return;
}
if (!socket.Connect(m_SmtpServer, 25))
{
TRACE(sendbuf);
AfxMessageBox("Could not connect to SMTP Service on server!");
res=socket.GetLastError();
socket.Close ();
return;
}
if (socket.Receive(returnbuf, 1024, 0)==SOCKET_ERROR)
{
res=socket.GetLastError();
socket.Close ();
return;
}
// Generate and send lp check request to socket
strcpy(command,"EHLO MY_APPLICATION");
len=strlen(command);
command[len]=0x0D;
command[len+1]=0x0A;
len++;
if (socket.Send (command,len+1)==SOCKET_ERROR )
{
res=socket.GetLastError();
socket.Close ();
return;
}
if (socket.Receive(returnbuf, 1024, 0)==SOCKET_ERROR)
{
res=socket.GetLastError();
socket.Close ();
return;
}
if ((returnbuf[0]=='2')&&(returnbuf[1]=='5')&&(returnbuf[2]=='0'))
{}
else
{
AfxMessageBox("EHLO command not understood by server!");
socket.Close ();
return;
}
char size[10];
sprintf(size,"%d",strlen(sendbuf));
strcpy(command,"MAIL From:<");
strcat(command,m_SmtpFrom);
strcat(command,"> ");
strcat(command,"SIZE=");
strcat(command,size);
len=strlen(command);
command[len]=0x0D;
command[len+1]=0x0A;
len++;
if (socket.Send (command,len+1)==SOCKET_ERROR )
{
res=socket.GetLastError();
socket.Close ();
return;
}
memset(returnbuf,0,1024);
if (socket.Receive(returnbuf, 1024, 0)==SOCKET_ERROR)
{
res=socket.GetLastError();
socket.Close ();
return;
}
if ((returnbuf[0]=='2')&&(returnbuf[1]=='5')&&(returnbuf[2]=='0'))
{}
else
{
CString Message;
Message="From Field is invalid! Error Received: \r\n\r\n";
Message+=returnbuf;
AfxMessageBox(Message);
socket.Close ();
return;
}
strcpy(command,"RCPT To:<");
strcat(command,info->mailTo);
strcat(command,"> \r\n");
len=strlen(command);
if (socket.Send (command,len)==SOCKET_ERROR )
{
res=socket.GetLastError();
socket.Close ();
return;
}
if (socket.Receive(returnbuf, 1024, 0)==SOCKET_ERROR)
{
res=socket.GetLastError();
socket.Close ();
return;
}
if ((returnbuf[0]=='2')&&(returnbuf[1]=='5')&&(returnbuf[2]=='0'))
{}
else
{
AfxMessageBox("Recipient not valid!");
socket.Close ();
return;
}
strcpy(command,"DATA");
len=strlen(command);
command[len]=0x0D;
command[len+1]=0x0A;
len++;
if (socket.Send (command,len+1)==SOCKET_ERROR )
{
res=socket.GetLastError();
socket.Close ();
return;
}
if (socket.Receive(returnbuf, 1024, 0)==SOCKET_ERROR)
{
res=socket.GetLastError();
socket.Close ();
return;
}
if ((returnbuf[0]=='3')&&(returnbuf[1]=='5')&&(returnbuf[2]=='4'))
{}
else
{
AfxMessageBox("DATA command not understood by server!");
socket.Close ();
return;
}
strcpy(command,sendbuf);
len=strlen(command);
command[len]=0x0D;
command[len+1]=0x0A;
len++;
if (socket.Send (command,len+1)==SOCKET_ERROR )
{
res=socket.GetLastError();
socket.Close ();
return;
}
strcpy(command,".");
len=strlen(command);
command[len]=0x0D;
command[len+1]=0x0A;
len++;
if (socket.Send (command,len+1)==SOCKET_ERROR )
{
res=socket.GetLastError();
socket.Close ();
return;
}
if (socket.Receive(returnbuf, 1024, 0)==SOCKET_ERROR)
{
res=socket.GetLastError();
socket.Close ();
return;
}
if ((returnbuf[0]=='2')&&(returnbuf[1]=='5')&&(returnbuf[2]=='0'))
{}
else
{
AfxMessageBox("E-Mail not processed by server!");
socket.Close ();
return;
}
strcpy(command,"QUIT");
len=strlen(command);
command[len]=0x0D;
command[len+1]=0x0A;
len++;
if (socket.Send (command,len+1)==SOCKET_ERROR )
{
res=socket.GetLastError();
socket.Close ();
return;
}
if (socket.Receive(returnbuf, 1024, 0)==SOCKET_ERROR)
{
res=socket.GetLastError();
socket.Close ();
return;
}
socket.Close ();
[code]