писал это для отправки логов по почте правдо очень давненько думаю разберешся, на счет картинок прочитай про расширение SMTP MIME, там все подробно написано
http://www.sources.ru/protocols/smtp...ng.shtml#model
http://dimas.ncn.ru/libz/lib_os/unix...s/glava2_5.htm
Код:
#include <winsock2.h>
#include <stdio.h>
#include "shlwapi.h"
#include "Windows.h"
#define MAX_PACKET_SIZE 0x10000
#define INI "send.ini"
#define LOG "log.txt"
#define cool 0
#define bad 1
int i,n;
WSADATA wsadata;
SOCKET s;
HOSTENT* phe;
SOCKADDR_IN sa;
int x1=1024;
char Buffer[MAX_PACKET_SIZE];
CHAR filepath[MAX_PATH];
CHAR helo[] = {"HELO\xD\xA\x0"};
CHAR from[] = {"mail from: "};
CHAR rcpt[] = {"rcpt to: "};
CHAR dat[] = {"data\xD\xA\x0"};
CHAR end_dat[]= {"\xD\xA.\xD\xA"};
CHAR quit[] = {"quit\xD\xA\x0"};
CHAR simbols[] = {"\xD\xA\x0"};
char lpReturnedString[500];
char mess[500];
CHAR body[1024];
char integer[5];
char* FReadINI(char* section,char* key);
int FReadData(char* data);
void FErr(char* msg);
int FReadLog();
int FSetConn();
int FSend (char* msg, SOCKET s);
int FSendBody (SOCKET s);
int FRecv (SOCKET s);
void FErr (char* msg)
{
printf("error ",Buffer);
}
int FReadData(char* data)
{
if (StrStr(data,Buffer)==0)
{
printf(Buffer);
return cool;
}
else
{
ExitProcess(0);
return bad;
}
}
char* FReadINI(char* section,char* key)
{
LPTSTR part;
GetFullPathName(INI,MAX_PATH,filepath,&part);
GetPrivateProfileString(section,key,"No Data",lpReturnedString,499,filepath);
if (lstrcmp(lpReturnedString,"No Data")!=NULL)
{
if (sizeof(lpReturnedString)<499)
ExitProcess (0);
return(lpReturnedString);
}
ExitProcess(0);
return ("No data");
}
FSendBody (SOCKET s)
{
LPTSTR part;
FILE *file;
GetFullPathName(FReadINI("MAIN","path"),MAX_PATH,filepath,&part);
if ((file=fopen(filepath,"r"))!=NULL)
{
while (!feof(file))
{
fgets(body,x1,file);
FSend(body,s);
FSend(simbols,s);
ZeroMemory(&body,sizeof(body));
}
}
fclose(file);
return cool;
}
FSend (char* msg, SOCKET s)
{
if (send(s,msg,(int)lstrlen(msg),0)==SOCKET_ERROR)
{
ExitProcess(0);
return bad;
}
else
return cool;
}
int FRecv (SOCKET s)
{
memset(&Buffer,0, sizeof(Buffer));
if (recv(s,Buffer,x1,0)!=SOCKET_ERROR)
return cool;
else
{
ExitProcess(0);
return bad;
}
}
void main(void)
{
if (WSAStartup(0x0101, &wsadata)==SOCKET_ERROR)
ExitProcess(0);
s=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
phe=gethostbyname(FReadINI("MAIN","server"));
sa.sin_family = AF_INET;
sa.sin_port=htons(atoi(FReadINI("MAIN","port")));
sa.sin_addr.S_un.S_addr=((struct in_addr *)phe->h_addr_list[0])->s_addr;
i=StrToInt((LPCTSTR)FReadINI("MAIN","email"));
if (connect(s,(struct sockaddr*)&sa,sizeof(sa))!=SOCKET_ERROR)
{
if (FRecv(s)==cool)
{
if (FReadData("220")==cool)
{
memset(&Buffer,0, sizeof(Buffer));
}
while (1)
{
if (FSend(helo,s)==cool)
{
FRecv(s);
if (FReadData("250")==bad)
{
FErr(Buffer);
}
}
lstrcat(mess,from);
lstrcat(mess,FReadINI("MAIN","mailfrom"));
lstrcat(mess,simbols);
if (FSend(mess,s)==cool)
{
memset(&mess,0,sizeof(mess));
FRecv(s);
if (FReadData("250")==bad)
{
FErr(Buffer);
ExitProcess(0);
}
}
for (n=1;n<i+1;n++)
{
lstrcat(mess,rcpt);
ZeroMemory(&body,sizeof(body));
lstrcat(body,"email");
itoa(n,integer,10);
lstrcat(body,integer);
lstrcat(mess,FReadINI("EMAIL",body));
ZeroMemory(&body,sizeof(body));
lstrcat(mess,simbols);
if (FSend(mess,s)==cool)
{
memset(&mess,0,sizeof(mess));
FRecv(s);
if (FReadData("250")==bad)
{
FErr(Buffer);
ExitProcess(0);
}
}
}
if (FSend(dat,s)==cool)
{
FRecv(s);
if (FReadData("354")==bad)
{
FErr(Buffer);
ExitProcess(0);
}
}
ZeroMemory(&body,sizeof(body));
lstrcat(body,"From:");
lstrcat(body,FReadINI("MAIN","title"));
lstrcat(body,"<");
lstrcat(body,FReadINI("MAIN","mailfrom"));
lstrcat(body,">\xD\xA\x0");
lstrcat(body,"Subject:");
lstrcat(body,FReadINI("MAIN","title"));
lstrcat(body,"\xD\xA\x0");
lstrcat(body,"MIME-Version: 1.0\xD\xA\x0");
lstrcat(body,"Content-Type: text/plain; charset=windows-1251\xD\xA\x0Content-transfer-encoding: base64");
FSend(body,s);
ZeroMemory(&body,sizeof(body));
FSendBody(s);
if (FSend(end_dat,s)==cool)
{
FRecv(s);
if (FReadData("250")==bad)
{
FErr(Buffer);
ExitProcess(0);
}
}
if (FSend(quit,s)==cool)
{
FRecv(s);
if (FReadData("250")==bad)
{
FErr(Buffer);
ExitProcess(0);
}
}
break;
}
}
else
{
printf(Buffer);
memset(&Buffer,0, sizeof(Buffer));
ExitProcess(0);
}
}
shutdown(s,1);
closesocket( s );
WSACleanup();
}
|