smtp send mail with C in linux

hi
I wanna send an email to another body.but smtp server sends me some errors that i can't understand why it sends these errors!
plaese help me

here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>
#include <iostream>
using namespace std;
int main(void)
{

// set family and socket type
struct addrinfo hints, *result;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

// resolve hostname to IP address: specify host and port number (in char not int)
if(getaddrinfo("smtp.gmail.com", "25", &hints, &result) != 0)
{
freeaddrinfo(result);
puts("Could not resolve hostname.");
exit(1);
}
// create socket and free addrinfo memory
int newsocket = socket(result->ai_family, result->ai_socktype, 0);
if(newsocket == -1)
{
puts("Could not create socket.");
freeaddrinfo(result); // free addrinfo memory
close(newsocket);
exit(1);
}

// set socket timeouts
struct timeval timeout;
memset(&timeout, 0, sizeof(timeout)); // zero timeout struct before use
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(newsocket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); // send timeout
setsockopt(newsocket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); // receive timeout

// connect to website
if(connect(newsocket, result->ai_addr, result->ai_addrlen) == -1)
{
puts("Could not connect.");
freeaddrinfo(result); // free addrinfo memory
close(newsocket);
exit(1);
}

char t[500];
int y=recv(newsocket,t,100,0);
t[y]='\0';
cout<<t<<"\n";

strcpy(t,"HELO gmail.com\r");
send(newsocket,t,sizeof(t),0);
memset(t,'\0',100);
y=recv(newsocket,t,100,0);
t[y]='\0';
cout<<"message\n"<<t;
memset(t,'\0',100);
strcpy(t,"MAIL FROM: robo.amin71@gmail.com\r");
send(newsocket,t,sizeof(t),0);
memset(t,'\0',100);
y=recv(newsocket,t,100,0);
t[y]='\0';
cout<<"message:\n"<<t<<"\n";
strcpy(t,"RCPT TO: robo.amin71@gmail.com>\r");
send(newsocket,t,sizeof(t),0);
memset(t,'\0',100);
y=recv(newsocket,t,100,0);
t[y]='\0';
cout<<t<<"\n ok\n";
return 0;
}


thanks
What error?
after sendig MAIL FROM to smtp,every command which you try to send will get back error!

smtp sends me 555 error :O
and sometimes 502 error

my problem is after sendig MAIL FROM to stmp.
gmail DOES NOT use port 25 and the connection MUST BE ssl encrypted, Also stmp service must be activated first from gmail account settings.

I suggest you to use libcurl instead of direcrly using sockets.
Last edited on
Topic archived. No new replies allowed.