Reputation: 379
I am trying to connect to a Gmail SMTP server to send an e-mail, but when i try to connect I get an error, the socket can't be connected. I tried to get the IP from Gmail SMTP but I just get 2607:f8b0:400c:c05::6c.
#define IP "2607:f8b0:400c:c05::6c"// Gmail smtp ip
#define DOOR 25
#define FROM "[email protected]"
#define TO "[email protected]"
#define SUBJECT "Data"
#define _MAX_CHAR 20
#define _OPEN "HELO default\r\n"\
"MAIL FROM: <"
#define _RCPT ">\r\nRCPT TO: <"
#define _DATA ">\r\nDATA\r\n"\
"From: <"
#define _SUB ">\r\nSubject: "
#define _NEWLINE "\r\n\r\n"
#define _CLOSE "\r\n.\r\n"\
"QUIT\r\n"
int send_mail ( char * subject, char * message, char * to, char * from, const
char ip [ 17 ], int door )
{
int sock;
struct sockaddr_in saddr;
WSADATA wsadata;
if ( WSAStartup ( MAKEWORD ( 2, 2 ), & wsadata ) != 0 ) {
printf("Hubo un error MAKE WORD");
return -1;
}
if ( ( sock = socket ( AF_INET, SOCK_STREAM, 0 ) ) < 0) {
printf("Hubo un error CREANDO EL SOCKET");
return -1;
}
saddr . sin_family = AF_INET;
saddr . sin_addr . s_addr = inet_addr ( ip );
saddr . sin_port = htons ( door );
if ( connect ( sock, ( struct sockaddr * ) & saddr, sizeof ( saddr ) ) < 0 ) {
printf("Hubo un error CONECTANDO EL SOCKET");
return -1;
}
size_t check_size = get_len ( _OPEN );
if ( check_size != send ( sock, _OPEN, check_size, 0 ) ) {
printf("Hubo un error EVIANDO OPEN");
return -1;
}
check_size = get_len ( from );
if ( check_size != send ( sock, from, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO FROM");
return -1;
}
check_size = get_len ( _RCPT );
if ( check_size != send ( sock, _RCPT, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO RCTP");
return -1;
}
check_size = get_len ( to );
if ( check_size != send ( sock, to, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO TO");
return -1;
}
check_size = get_len ( _DATA );
if ( check_size != send ( sock, _DATA, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO DATA");
return -1;
}
check_size = get_len ( from );
if ( check_size != send ( sock, from, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO from");
return -1;
}
check_size = get_len ( _SUB );
if ( check_size != send ( sock, _SUB, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO sub");
return -1;
}
check_size = get_len ( subject );
if ( check_size != send ( sock, subject, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO subjet");
return -1;
}
check_size = get_len ( _NEWLINE );
if ( check_size != send ( sock, _NEWLINE, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO newline");
return -1;
}
check_size = get_len ( message );
if ( check_size != send ( sock, message, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO message");
return -1;
}
check_size = get_len ( _CLOSE );
if ( check_size != send ( sock, _CLOSE, check_size, 0 ) ) {
printf("Hubo un error ENVIANDO close");
return -1;
}
closesocket ( sock );
WSACleanup ( );
return 0;
}
What is wrong with this?
Upvotes: 1
Views: 558
Reputation: 4619
It's very likely that the machine you are using is still on IPv4. Since the address you have right now is an IPv6 format 2607:f8b0:400c:c05::6c
To obtain the IPv4 address you can run a dig
command as so:
dig A smtp.gmail.com
Here is a sample output
C:\Users\PC-James>dig A smtp.gmail.com
; <<>> DiG 9.10.3-P4 <<>> A smtp.gmail.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61819
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;smtp.gmail.com. IN A
;; ANSWER SECTION:
smtp.gmail.com. 299 IN CNAME gmail-smtp-msa.l.google.com.
gmail-smtp-msa.l.google.com. 299 IN A 108.177.125.108
gmail-smtp-msa.l.google.com. 299 IN A 108.177.125.109
In my region, the IPv4 address of Google's SMTP servers would be 108.177.125.108
If you don't have dig
on your computer, you can also run the request via the web interface from Google's Toolbox.
Upvotes: 1