#include <winsock2.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"wsock32.lib")
#define EOL "\r\n"
DWORD smailid;
HANDLE hthr;
const WORD VERSION_MAJOR = 1;
const WORD VERSION_MINOR = 1;
int error;
char pszBody[1500];
typedef struct threadd
{
char name[200];
char fromid[200];
char toid[200];
char serv[200];
char sub[300];
char mes[1000];
} TDATA;
void send(char *smtpservr, char *fromname, char *fromid, char *toid,
char *subject, char *message);
void Check( int iStatus, char *szFunction );
DWORD WINAPI is_ok( LPVOID l);
int sendmail( LPVOID l);
int main()
{
send("mail.web.de","andreas","1Avatar@sms.at","andreas.hoess@gmx.at",
"dssd","asdsddads");
getchar();
return 0;
}
void send(char *smtpservr, char *fromname, char *fromid, char *toid,
char *subject, char *message)
{
TDATA td;
strcpy(td.fromid,fromid);
strcpy(td.name ,fromname);
strcpy(td.serv ,smtpservr);
strcpy(td.toid ,toid);
strcpy(td.sub ,subject);
strcpy(td.mes ,message);
cout<<"Email an "<<toid<<" wird versendet . . ."<<endl;
hthr=CreateThread(NULL,0,is_ok,(LPVOID)&td,CREATE_SUSPENDED,&smailid);
SetThreadPriority( hthr, THREAD_PRIORITY_TIME_CRITICAL);
ResumeThread(hthr);
WaitForSingleObject(hthr, INFINITE);
}
DWORD WINAPI is_ok( LPVOID l)
{
TDATA *d=(TDATA *)l;
if(sendmail(l)==0)
cout<<"Versenden abgeschlossen"<<endl;
else
cout<<"Versenden gescheitert"<<endl;
return 0;
}
void Check( int iStatus, char *szFunction )
{
if (iStatus != SOCKET_ERROR && iStatus != 0) return;
else
error=1;
}
int sendmail( LPVOID l)
{
WSADATA WSData;
LPHOSTENT lpHostEntry;
LPSERVENT lpServEntry;
SOCKADDR_IN SockAddr;
SOCKET hServer;
int iProtocolPort;
char szSmtpServerName[100], szToAddr[100], szFromAddr[100];
char szBuffer[4096], szMsgLine[255];
TDATA *d=(TDATA *)l;
error=0;
lstrcpy( szSmtpServerName,d->serv );
lstrcpy( szToAddr, d->toid );
lstrcpy( szFromAddr,d->fromid );
if ( WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData) )
{
cout<<"Error: Kann Winsock nicht finden"<<endl;
return(1);
}
lpHostEntry = gethostbyname( szSmtpServerName );
if (lpHostEntry == NULL)
{
cout<<"Error: Kann den SMTP Server("<<szSmtpServerName<<")"
<<"nicht finden "<<endl;
return(1);
}
hServer = socket( PF_INET, SOCK_STREAM, 0);
if (hServer == INVALID_SOCKET)
{
cout<<"Error: Cannot open mail server socket"<<endl;
return(1);
}
lpServEntry = getservbyname( "mail", 0);
if (lpServEntry == NULL)
iProtocolPort = htons(IPPORT_SMTP);
else
iProtocolPort = lpServEntry->s_port;
SockAddr.sin_family = AF_INET;
SockAddr.sin_port = iProtocolPort;
SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
if (connect( hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
{
cout<<"Error: Connecting to Server socket failed"<<endl;
return (1);
}
Check( recv( hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
wsprintf(szMsgLine,"HELO %s%s","microsoft [111.122.1.12]", EOL);
Check(send(hServer,szMsgLine,strlen(szMsgLine), 0),"send() HELO");
Check(recv(hServer,szBuffer,sizeof(szBuffer), 0),"recv() HELO");
wsprintf( szMsgLine,"MAIL FROM:<%s>%s", szFromAddr,EOL);
Check(send(hServer,szMsgLine,strlen(szMsgLine), 0),"send() MAIL FROM");
Check(recv(hServer,szBuffer,sizeof(szBuffer), 0),"recv() MAIL FROM");
wsprintf( szMsgLine,"RCPT TO:<%s>%s", szToAddr, EOL);
Check(send(hServer,szMsgLine,strlen(szMsgLine),0),"send() RCPT TO");
Check(recv(hServer,szBuffer,sizeof(szBuffer),0),"recv() RCPT TO");
wsprintf( szMsgLine,"DATA%s", EOL);
Check(send(hServer,szMsgLine,strlen(szMsgLine),0),"send() DATA");
Check(recv(hServer,szBuffer,sizeof(szBuffer),0),"recv() DATA");
char sdate[70];
lstrcpy(sdate,"Date: ");
char s1s[70];
GetDateFormat(0x409,0,0,"ddd,dd MMM yyyy",s1s,200);
lstrcat(sdate,s1s);
lstrcat(sdate," ");
GetTimeFormat(0x409,0,0,"HH:mm:ss",s1s,200);
lstrcat(sdate,s1s);
lstrcat(sdate," PM");
char header[350];
lstrcpy(header,"From: ");
lstrcat(header,d->name );
lstrcat(header,"<");
lstrcat(header,d->fromid );
lstrcat(header,">");
lstrcat(header,"\r\n");
lstrcat(header,"To: ");
lstrcat(header,d->toid );
lstrcat(header,"\r\n");
lstrcat(header,"Subject: ");
lstrcat(header,d->sub );
lstrcat(header,"\r\n");
lstrcat(header,sdate);
lstrcat(header,"\r\n");
lstrcat(header,"X-Mailer: Outlook Express 1.00\r\nMIME-Version:
1.0\r\nContent-Type:text/plain;\r\n\tcharset=\"iso-8859-1\"
\r\nContent-Transfer-Encoding: 7bit\r\n\r\n");
wsprintf( szMsgLine,header);
Check(send(hServer,szMsgLine,strlen(szMsgLine), 0),"send() header");
if (error)
return error;
lstrcpy(pszBody,d->mes );
lstrcat(pszBody,"\r\n\r\n");
Check(send( hServer,pszBody, strlen(pszBody), 0), "send() message");
wsprintf(szMsgLine,"%s.%s", EOL, EOL);
Check(send(hServer,szMsgLine, strlen(szMsgLine),0),"send() end-message");
Check(recv(hServer,szBuffer, sizeof(szBuffer),0),"recv() end-message");
wsprintf(szMsgLine,"QUIT%s",EOL);
Check(send(hServer,szMsgLine,strlen(szMsgLine),0),"send() QUIT");
Check(recv(hServer,szBuffer,sizeof(szBuffer),0),"recv() QUIT");
closesocket(hServer);
WSACleanup();
return error;
}