I was looking for an implementation for sending letters via smtp, I did not find any documentation, I had to wander around the forums, but nowhere was there anything practical. I found a topic on the forum http://www.cyberforum.ru/cpp-beginners/thread1020524 ... from 2012, I did it by analogy, but I did not achieve anything.
In short, here's the code:
#define _CRT_SECURE_NO_WARNINGS #pragma comment (lib,"Ws2_32.lib") #include <windows.h> #include <winsock.h> #include <stdio.h> WSADATA ws; SOCKET s; struct sockaddr_in addr; hostent* d_addr; char text[1024]; int main() { // ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΠ΅ΠΌ ΡΠΎΠΊΠ΅ΡΡ if (FAILED(WSAStartup(MAKEWORD(1, 1), &ws))) { printf("Error in WSAStartup(...)\n"); return 1; } // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΡΠΎΠΊΠ΅Ρ s = socket(AF_INET, SOCK_STREAM, 0); if (s == INVALID_SOCKET) { printf("Error in socket(...)\n"); return 1; } // ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ Π°Π΄ΡΠ΅Ρ ΡΠ΅ΡΠ²Π΅ΡΠ° d_addr = gethostbyname("smtp.gmail.com"); if (d_addr == NULL) { printf("Error in gethostbyname(...)\n"); return 1; }; // Π·Π°ΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΡ Π°Π΄ΡΠ΅ΡΠ° addr.sin_family = AF_INET; addr.sin_addr.s_addr = *((unsigned long*)d_addr->h_addr); addr.sin_port = htons(465); // ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌ ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΠ΅ if (SOCKET_ERROR == (connect(s, (sockaddr*)& addr, sizeof(addr)))) { printf("Error in connect(...)\n"); return 1; } // ΠΆΠ΄Π΅ΠΌ ΠΎΡΠ²Π΅Ρ ΠΎΡ ΡΠ΅ΡΠ²Π΅ΡΠ° recv(s, text, sizeof(text), 0); printf("recv - %s", text); // ΠΏΡΠΈΠ²Π΅ΡΡΡΠ²ΡΠ΅ΠΌ ΡΠ΅ΡΠ²Π΅Ρ strcpy(text, "EHLO SSL\r\n"); send(s, text, strlen(text), 0); printf("send - %s", text); // ΠΆΠ΄Π΅ΠΌ ΠΏΠΎΠ΄ΡΠ²Π΅ΡΠΆΠ΄Π΅Π½ΠΈΠ΅ ΠΎΡ ΡΠ΅ΡΠ²Π΅ΡΠ° recv(s, text, sizeof(text), 0); printf("recv - %s", text); // Π½Π°ΡΠΈΠ½Π°Π΅ΠΌ ΠΎΡΠΏΡΠ°Π²Π»ΡΡΡ ΠΊΠΎΠ½Π²Π΅ΡΡ ΡΠΎΡΡΠΎΡΡΠΈΠΉ ΠΈΠ· ΠΏΠΎΠ»Π΅ΠΉ // MAIL FROM: ΠΈ RCPT TO: ΠΠΎΡΠ»Π΅ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΠΎΠ»Ρ ΠΆΠ΄Π΅ΠΌ // ΠΏΠΎΠ΄ΡΠ²Π΅ΡΠΆΠ΄Π΅Π½ΠΈΠ΅ strcpy(text, "AUTH LOGIN\r\n"); send(s, text, strlen(text), 0); printf("send - %s", text); recv(s, text, sizeof(text), 0); printf("recv - %s", text); // ΡΠΎΠΎΠ±ΡΠ°Π΅ΠΌ ΠΎΡΠΏΡΠ°Π²ΠΈΡΠ΅Π»Ρ strcpy(text, "MAIL FROM: [email]sender@mail.ru[/email] "); send(s, text, strlen(text), 0); printf("send - %s", text); // ΠΆΠ΄Π΅ΠΌ ΠΏΠΎΠ΄ΡΠ²Π΅ΡΠΆΠ΄Π΅Π½ΠΈΠ΅ ΠΎΡ ΡΠ΅ΡΠ²Π΅ΡΠ° recv(s, text, sizeof(text), 0); printf("recv - %s", text); // ΡΠΎΠΎΠ±ΡΠ°Π΅ΠΌ ΠΏΠΎΠ»ΡΡΠ°ΡΠ΅Π»Ρ strcpy(text, "RCPT TO: [email]receiver@mtu-net.ru[/email] "); send(s, text, strlen(text), 0); printf("send - %s", text); // ΠΆΠ΄Π΅ΠΌ ΠΏΠΎΠ΄ΡΠ²Π΅ΡΠΆΠ΄Π΅Π½ΠΈΠ΅ ΠΎΡ ΡΠ΅ΡΠ²Π΅ΡΠ° recv(s, text, sizeof(text), 0); printf("recv - %s", text); // ΠΏΠΎΠ΄Π°Π΅ΠΌ ΠΊΠΎΠΌΠ°Π½Π΄Ρ, ΡΡΠΎ Π³ΠΎΡΠΎΠ²Ρ Π½Π°ΡΠ°ΡΡ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ ΠΏΠΈΡΡΠΌΠ° strcpy(text, "DATA\r\n"); send(s, text, strlen(text), 0); printf("send - %s", text); // ΠΆΠ΄Π΅ΠΌ ΠΏΠΎΠ΄ΡΠ²Π΅ΡΠΆΠ΄Π΅Π½ΠΈΠ΅ ΠΎΡ ΡΠ΅ΡΠ²Π΅ΡΠ° recv(s, text, sizeof(text), 0); printf("recv - %s", text); // ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΠΌ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ // ΠΎΡ ΠΊΠΎΠ³ΠΎ ΠΏΠΈΡΡΠΌΠΎ strcpy(text, "FROM: [email]sender@mail.ru[/email] "); send(s, text, strlen(text), 0); printf("send - %s", text); // ΠΊΠΎΠΌΡ ΠΏΠΈΡΡΠΌΠΎ strcpy(text, "TO: [email]receiver@mtu-net.ru[/email] "); send(s, text, strlen(text), 0); printf("send - %s", text); // ΡΠ΅ΠΌΠ° ΠΏΠΈΡΡΠΌΠ° strcpy(text, "SUBJECT: test\r\n"); send(s, text, strlen(text), 0); printf("send - %s", text); // ΡΠ΅ΠΊΡΡ ΠΏΠΈΡΡΠΌΠ° strcpy(text, "Hi!\nIt is a message for you\n"); send(s, text, strlen(text), 0); printf("send - %s", text); // Π³ΠΎΠ²ΠΎΡΠΈΠΌ, ΡΡΠΎ Π·Π°ΠΊΠΎΠ½ΡΠΈΠ»ΠΈ strcpy(text, "\r\n.\r\n"); send(s, text, strlen(text), 0); printf("send - %s", text); recv(s, text, sizeof(text), 0); printf("recv - %s", text); // ΠΏΡΠΎΡΠ°Π΅ΠΌΡΡ Ρ ΡΠ΅ΡΠ²Π΅ΡΠΎΠΌ strcpy(text, "QUIT"); send(s, text, strlen(text), 0); printf("send - %s", text); // Π·Π°ΠΊΡΡΠ²Π°Π΅ΠΌ ΡΠΎΠΊΠ΅Ρ closesocket(s); return 0; }
He seems to be working, through the 25th port and 587 the connection passes. But at the time of authorization request, the server generates an error that does not use encryption. As I understand it, this is due to the fact that I use a port that does not support encryption. According to the documentation, such a port is 465, but when I set it up, the server connects for a long time and as a result does not respond, that is, with empty messages, although there seems to be a connection.
Here is a picture at port 465:
recv - send - EHLO SSL recv - EHLO SSL send - AUTH LOGIN recv - AUTH LOGIN send
- MAIL FROM: sender@mail.ru recv - MAIL FROM: sender@mail.ru send - RCPT TO: receiver@mtu-net.ru recv - RCPT TO: receiver@mtu-net.ru send
- DATA recv - DATA send - FROM: sender@mail.ru send - TO: receiver@mtu-net.ru send - SUBJECT: test send - Hi! It is a message for you send. recv -. send - QUIT
Here is such for others:
recv - 220 smtp2o.mail.yandex.net ESMTP (Want to use Yandex.Mail for your domain? Visit http://pdd.yandex.ru ) send - EHLO SSL recv - 250-smtp2o.mail.yandex.net 250- 8BITMIME 250-PIPELINING 250-SIZE 42991616 250-STARTTLS 250-AUTH LOGIN PLAIN XOAUTH2 250-DSN 250 ENHANCEDSTATUSCODES send - AUTH LOGIN recv-530 Please see: http://help.yandex.ru/mail/mail-clients/ssl.xml
250 ENHANCEDSTATUSCODES send - MAIL FROM: sender@mail.ru recv - MAIL FROM: sender@mail.ru send - RCPT TO: receiver@mtu-net.ru recv - RCPT TO: receiver@mtu-net.ru send - DATA recv - DATA send - FROM: sender@mail.ru send - TO: receiver@mtu-net.ru send - SUBJECT: test send - Hi! It is a message for you send. recv -. send - QUIT
I do not know what to do, I will be grateful if you help.