0
votes

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.

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#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 < 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 < 0) 
    error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);
}

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

n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n < 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 < 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n < 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 < 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n < 0) 
     error("ERROR reading from socket");
printf("%s\n",buffer);

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

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

strcpy(buffer,"[email protected]");

n = write(sockfd,buffer,strlen(buffer));
if (n < 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n < 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 < 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n < 0) 
     error("ERROR reading from socket");
printf("%s\n",buffer);

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

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

printf("MAIL FROM");

strcpy(buffer,"MAIL FROM: [email protected]");

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

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

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

 printf("MAIL TO");


strcpy(buffer,"RCPT TO: [email protected]");

n = write(sockfd,buffer,strlen(buffer));
if (n < 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n < 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 < 0) 
     error("ERROR writing to socket");
bzero(buffer,BUFFEr_SIZE);
n = read(sockfd,buffer,BUFFEr_SIZE-1);
if (n < 0) 
     error("ERROR reading from socket");
puts(buffer);

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

return 0;
}

OUTPUT is here:

220 smtp.gmail.com ESMTP bh6sm124854736wjb.0 - gsmtp

DONE EHLO250-smtp.gmail.com at your service, [85.98.184.204]

250-SIZE 35882577

250-8BITMIME

250-STARTTLS

250-ENHANCEDSTATUSCODES

250-PIPELINING

250-CHUNKING

250 SMTPUTF8

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

DONE AUTH

AUTH UID

DONE UID

AUTH PWD

MAIL FROM

MAIL TO

DONE MAILTO

DATASON DONE

3
If your code "executed successful", why do you ask?too honest for this site
What does your program output when you run it? Also, you should use send and recv for sockets, not write and read.dbush
And learn proper indentation. That code is quite unreadable.too honest for this site
Can you show the output of your program? You suggest that it works (or doesn't detect an error), which means there's no obvious problem with your C code, and it's the SMTP which needs debugging.Useless
@dbush I tried these but it still doesn't work.Utku Gökcan

3 Answers

1
votes

After you send the AUTH LOGIN command, the SMTP server returns with 451 4.5.0 SMTP protocol violation, see RFC 2821. Looking at RFC 2181, this command does not exist.

This command is an extension to SMTP, which this mail server does not support. If you remove the authentication commands, it should work.

Also, when continuing to test, be sure to look at the output to your program to ensure that the server doesn't return any more errors. If it does, check the standards as to why you're getting those errors.

0
votes

You need to send addresses as:

  MAIL FROM:<[email protected]>
  RCPT TO:<[email protected]>
0
votes

The protocol violation is simply because of the +1 in the second line:

strcpy(buffer,"AUTH LOGIN\n");
n = write(sockfd,buffer,strlen(buffer)+1);

You are sending google "AUTH LOGIN\n\0" - \0 should not be there.

You would still find that google won't allow you to send your password over a plain unencrypted SMTP connection (i.e. without STARTTLS and actually connecting over TLS), but that's a different story.