1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
static void init(void);
static void end(void);
char dest[120] = "EHLO client\r\nMAIL FROM:<erreur410@hotmail.fr>\r\n";
char sender[120] = "RCPT TO:<gerar@gmail.com>\r\n";
char sujet[] = "monemail";
char data[1024] ="voila mon message ^^";
char serveur[] = "smtp.live.com";
char test[] = "";
int main()
{
init();//initialisation du wsa pour windows
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);//creation du socket sockstream =tcp AFinet = ipv4
SOCKADDR_IN sin = { 0 };
struct hostent *hostinfo;
hostinfo = gethostbyname(serveur);
if(sock == -1)
{
perror("socket()"); //verification si il ny a pas derreur
exit(errno);
}
printf("Appel de gethost by name %s\n", serveur);
sin.sin_addr.s_addr = hostinfo->h_addr;
sin.sin_port = htons(25);//recuperation du port
sin.sin_family = AF_INET;//comment l'envoiyer
printf("configuration ok\n");
connect(sock,(SOCKADDR *)&sin,sizeof(sin));
char buffer[1024] = {0};
char reponce[1024] = {0};
send(sock,sender,strlen(sender),0);
recv(sock, reponce, 1023, 0);
printf("recu:%s\n",reponce);
send(sock,dest,strlen(dest),0);
recv(sock, reponce, 1023,0);
printf("recu:%s\n",reponce);
sprintf(buffer,"DATA\r\n");
send(sock,buffer,strlen(buffer),0);
recv(sock, reponce,1023,0);
printf("recu:%s\n",reponce);
sprintf(buffer,"From: %s\r\nTo: %s\r\nSubject: %s\r\n%s\r\n.\r\n\r\n",sender,dest,sujet,data);
send(sock,buffer,strlen(buffer),0);
recv(sock, reponce,1023,0);
printf("recu:%s\n",reponce);
sprintf(buffer,"QUIT\r\n");
send(sock,buffer,strlen(buffer),0);
recv(sock,reponce,1023,0);
printf("recu:%s\n",reponce);
perror("envoi()");
printf("envoie terminer\n");
end();
printf("connection fermer\n");
return 0;
}
static void init(void)
{
#ifdef WIN32
WSADATA wsa;
int err = WSAStartup(MAKEWORD(2, 2), &wsa);
if(err < 0)
{
puts("Échec du lancement WSA !");
exit(EXIT_FAILURE);
}
#endif
}
static void end(void)
{
#ifdef WIN32
WSACleanup();
#endif
} |
Partager