gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:16:35 27 4
gpt4 key购买 nike

我正在尝试连接到 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;
}

这是怎么回事?

最佳答案

很可能您正在使用的机器仍在使用 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 运行请求。 .

关于C - 套接字无法连接到 SMTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50939365/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com