C-套接字无法连接到SMTP服务器

时间:2018-06-20 02:31:14

标签: c windows sockets tcp ip

我正在尝试连接到Gmail SMTP服务器以发送电子邮件,但是当我尝试连接时出现错误,无法连接套接字。我试图从Gmail SMTP获取IP,但我只得到2607:f8b0:400c:c05 :: 6c。

#define IP "2607:f8b0:400c:c05::6c"// Gmail smtp ip 
#define DOOR 25           
#define FROM "someemail@gmail.com"
#define TO "dskato0603@gmail.com" 
#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;
 }

这有什么问题?

1 个答案:

答案 0 :(得分:1)

您使用的计算机很可能仍在IPv4上。由于您现在拥有的地址是IPv6格式2607:f8b0:400c:c05::6c

要获取IPv4地址,您可以这样运行dig命令:

dig A smtp.gmail.com

这是示例输出

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

在我的地区,Google SMTP服务器的IPv4地址为108.177.125.108

如果您的计算机上没有dig,也可以通过Google's Toolbox通过网络界面运行请求。