How can i send a mail with smtp in socket programming?

This is my client code:
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
#include <windows.h>
#include <winsock2.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
  WSADATA wsa;
  WORD Version=MAKEWORD(2,1);
  WSAStartup(Version,&wsa);
  char my_message[]="Hi.This is a test message.";
  SOCKET _connect=socket(AF_INET,SOCK_STREAM,0);
  char sender[]="MAIL FROM:<sender.gmail.com>";
  char 	recip[]="RCPT TO:<recip.gmail.com>";
   char from[]="From:sender.gmail.com";
   char to[]="To:recip.gmail.com";
   char Subject[]="Subject:Test Message";
  struct sockaddr_in Sock;
  struct hostent* server;
  server=gethostbyname("192.168.1.1");
  Sock.sin_addr.s_addr=inet_addr("smtp.gmail.com");
  Sock.sin_family=AF_INET;
  memcpy(&Sock.sin_addr,server->h_addr,server->h_length);
  Sock.sin_port=htons(25);
  int _length=sizeof(struct sockaddr_in);
  printf("Connect not founded.\n");
  if(!connect(_connect,(SOCKADDR*)&Sock,&_length)){
  printf("\n");
  for(;;){
    if(send(_connect,"HELO",4,0))
    printf("Protcole is starting\n");
    else
    printf("Protocole is not starting\n");     
    if(send(_connect,sender,strlen(sender),0))  
    printf("Sender name is sending \n"); 
    else
   printf("Sender name is not sending\n");
    if(send(_connect,recip,strlen(recip),0))
    printf("Recip is access\n");
    else
    printf("Recip is not access\n");
    if(send(_connect,"DATA",4,0))
    printf("Datas are sending\n");
    else
    printf("Datas are not sending\n");
    if(send(_connect,from,strlen(from),0))
    printf("Sender is setting\n");
    else
    printf("Sender is not setting\n");
    if(send(_connect,to,strlen(to),0))
    printf("Recip is sending\n");
    else
    printf("Recip is not sending\n");
     if(send(_connect,Subject,strlen(Subject),0))
    printf("Subject is sending\n");
    else
    printf("Subject is not sending\n");
    if(send(_connect,my_message,strlen(my_message),0))
    printf("This message is sending\n");
    else
    printf("This message is not sending");
     if(send(_connect,".",1,0))
    printf("Datas are carrying\n");
    else
    printf("Datas are not carrying\n");
    if(send(_connect,"NOOP",4,0))
    printf("NOOP is carrying\n");
    else
    printf("NOOP is not carrying\n");
     if(send(_connect,"QUIT",4,0))
    printf("Exit in this protocole\n");
    else
    printf("Not exit in this protocole\n");
    } 
             }
             else{
                  printf("Cut this connect\n");
                  }
             WSACleanup();
  system("PAUSE");	
  return 0;
}


But i can not send an e-mail in my gmail account.
Last edited on
Gmail server does not use port 25 and you must use a SSL connection, read gmail documentation.
And you don't need SMTP to send mails, it's native inside Win32 api (COM)
Last edited on
I think a solution was posted here eventually.
http://www.cplusplus.com/forum/general/47150/

This is a topic that comes up from time to time on the forum. It may be worth your while to search the site.
As I said Win32 COM contains mail support
Topic archived. No new replies allowed.