Using SMTP for sending e-mail with C in Linux
<p>I was trying to send an email in c language but after handshaking, ack message and printing message sent, it does not send mail to my inbox. My whole code executed succesfully. Could you help, thanks.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt; 
#define BUFFEr_SIZE 4096

void error(char *msg)
{
  perror(msg);
  exit(0);
}

int main(int argc, char *argv[])
{
 int sockfd, portno, n;
 struct sockaddr_in serv_addr;
 struct hostent *server;

 char buffer[BUFFEr_SIZE];
 if (argc &lt; 3) {
    fprintf(stderr,"usage %s hostname port\n", argv[0]);
    exit(0);
 }
 portno = atoi(argv[2]);
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd &lt; 0) 
    error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);
}

bzero((char *) &amp;serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server-&gt;h_addr, 
     (char *)&amp;serv_addr.sin_addr.s_addr,
     server-&gt;h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&amp;serv_addr,sizeof(serv_addr)) &lt; 0) 
    error("ERROR connecting");

n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n &lt; 0) 
     error("ERROR reading from socket");
printf("%s\n",buffer);

bzero(buffer,BUFFEr_SIZE);

/*------------------------------*/
printf("\nDONE\n");
printf("EHLO");

strcpy(buffer,"ehlo smtp.gmail.com\n");

n = write(sockfd,buffer,strlen(buffer));
if (n &lt; 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n &lt; 0) 
     error("ERROR reading from socket");
printf("%s\n",buffer);

/*------------------------------*/    

/*------------------------------*/
printf("\nDONE EHLO\n");
printf("AUTH");

strcpy(buffer,"AUTH LOGIN\n");

n = write(sockfd,buffer,strlen(buffer)+1);
if (n &lt; 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n &lt; 0) 
     error("ERROR reading from socket");
printf("%s\n",buffer);

/*------------------------------*/ 

/*------------------------------*/
printf("\nDONE AUTH\n");
printf("AUTH UID");

strcpy(buffer,"xxxx@gmail.com");

n = write(sockfd,buffer,strlen(buffer));
if (n &lt; 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n &lt; 0) 
     error("ERROR reading from socket");
printf("%s\n",buffer);

/*------------------------------*/ 

/*------------------------------*/
printf("\nDONE UID\n");
printf("AUTH PWD");

strcpy(buffer,"xxxxxx");

n = write(sockfd,buffer,strlen(buffer)+1);
if (n &lt; 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n &lt; 0) 
     error("ERROR reading from socket");
printf("%s\n",buffer);

/*------------------------------*/ 

/*------------------------------*/

printf("MAIL FROM");

strcpy(buffer,"MAIL FROM: xxxxx@gmail.com");

n = write(sockfd,buffer,strlen(buffer));
if (n &lt; 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n &lt; 0) 
     error("ERROR reading from socket");
printf("%s\n",buffer);

/*------------------------------*/ 

/*------------------------------*/

 printf("MAIL TO");


strcpy(buffer,"RCPT TO: aaaaa@gmail.com");

n = write(sockfd,buffer,strlen(buffer));
if (n &lt; 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n &lt; 0) 
     error("ERROR reading from socket");
printf("%s\n",buffer);

/*------------------------------*/       

/*------------------------------*/
printf("DONE MAILTO\n");

printf("DATA");

strcpy(buffer,"DATA\r\n");

n = write(sockfd,buffer,strlen(buffer));

strcpy(buffer,"Subject: test\r\n");

n = write(sockfd,buffer,strlen(buffer));

strcpy(buffer,"SMTP MAIL TOOL TEST WORKS!!!\r\n");

n = write(sockfd,buffer,strlen(buffer));

strcpy(buffer,"\n\n");

n = write(sockfd,buffer,strlen(buffer));

strcpy(buffer,".\n");

n = write(sockfd,buffer,strlen(buffer));


/*------------------------------*/ 

/*------------------------------*/ 
printf("SON DONE");
strcpy(buffer,"quit\n");

n = write(sockfd,buffer,strlen(buffer));
if (n &lt; 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n &lt; 0) 
     error("ERROR reading from socket");
puts(buffer);

/*------------------------------*/

return 0;
}
</code></pre>

<p>OUTPUT is here:</p>

<p>220 smtp.gmail.com ESMTP bh6sm124854736wjb.0 - gsmtp</p>

<p>DONE EHLO250-smtp.gmail.com at your service, [85.98.184.204]</p>

<p>250-SIZE 35882577</p>

<p>250-8BITMIME</p>

<p>250-STARTTLS</p>

<p>250-ENHANCEDSTATUSCODES</p>

<p>250-PIPELINING</p>

<p>250-CHUNKING</p>

<p>250 SMTPUTF8</p>

<p>DONE EHLO AUTH451 4.5.0 SMTP protocol violation, see RFC 2821 bh6sm124854736wjb.0 - gsmtp</p>

<p>DONE AUTH</p>

<p>AUTH UID</p>

<p>DONE UID</p>

<p>AUTH PWD</p>

<p>MAIL FROM</p>

<p>MAIL TO</p>

<p>DONE MAILTO</p>

<p>DATASON DONE</p>