Advertisement
Pi0trek

smtp

Apr 29th, 2017
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <WinSock2.h>
  4. #include <Windows.h>
  5. #include <conio.h>
  6. #include <iostream>
  7. #include <string>
  8. #include <ctime>
  9. #include <fstream>
  10.  
  11. #pragma comment(lib, "ws2_32.lib")
  12.  
  13. using namespace std;
  14.  
  15. #define MAX_MSG 128
  16. #define CRLF "\r\n"
  17. string senderMail = "testpop3test1@wp.pl", reciverMail = "psulzyc@wi.zut.edu.pl", serverSMTP = "smtp.wp.pl", emailwbase64 = "dGVzdHBvcDN0ZXN0MUB3cC5wbA==", haslowbase64 = "VGVzdHBvcDM=" ;
  18. string plain = "PLAIN";
  19. string login = "LOGIN";
  20. int port = 587;
  21.  
  22. int main()
  23. {
  24.     SOCKET sock_on;
  25.     WSADATA wsaData;
  26.     LPHOSTENT lpHostEntry;
  27.     LPSERVENT lpServEntry;
  28.     SOCKADDR SockAddr;
  29.     struct sockaddr_in sok = { 0 };
  30.     struct hostnet *hp;
  31.     string command, s, msg_num;
  32.     char buff[MAX_MSG];
  33.     int n_bytes = 0;
  34.     char szMsgLine[255] = "";
  35.     int         iProtocolPort = 0;
  36.  
  37.     if (WSAStartup(MAKEWORD(2, 2), &wsaData))
  38.     {
  39.         cout << "Cannot find Winsock v" << 2 << "." << 2 << " or later!" << endl;
  40.  
  41.         return 1;
  42.     }
  43.     // Lookup email server's IP address.
  44.     lpHostEntry = gethostbyname(serverSMTP.c_str());
  45.     if (!lpHostEntry)
  46.     {
  47.         cout << "canoot find SMTP mail server" << serverSMTP << endl;
  48.         return 1;
  49.     }
  50.     //sock_on = socket(AF_INET, SOCK_STREAM, 0); // init socket
  51.     sock_on = socket(PF_INET, SOCK_STREAM, 0);
  52.     sok.sin_family = AF_INET;                              
  53.     sok.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
  54.     sok.sin_port = (unsigned short int) htons(port);
  55.  
  56. //------------------------------------------------------------------------------------
  57.     if ((connect(sock_on, (sockaddr *)&sok, sizeof(sockaddr_in))))
  58.     {
  59.         cout << "nie udało się " << endl;
  60.     }
  61.     n_bytes = recv(sock_on, buff, MAX_MSG, 0);
  62.     //cout << buff<<", "<< n_bytes << endl;
  63. //------------------------------------------------------
  64.     sprintf(szMsgLine, "EHLO %s%s", serverSMTP.c_str(), CRLF);
  65.     n_bytes = send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  66.     cout << n_bytes << endl;
  67.     n_bytes = recv(sock_on, buff, sizeof(buff), 0);
  68.     cout << buff << ", " << n_bytes << endl;
  69. //---------------------------------------------------------------------------------------------
  70.     sprintf(szMsgLine, "AUTH %s%s", login.c_str(), CRLF);
  71.     n_bytes = send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  72.     cout << n_bytes << endl;
  73.     n_bytes = recv(sock_on, buff, sizeof(buff), 0);
  74.     cout << buff << ", " << n_bytes << endl;
  75. //-----------------------------------------------------------------------------------------------
  76.     string mailiN = "dGVzdHBvcDN0ZXN0MUB3cC5wbA==";
  77.     string hasloN = "VGVzdHBvcDM=";
  78. //-------------------------------------------------------------------------------------------------
  79.     sprintf(szMsgLine, "%s%s", mailiN.c_str(), CRLF);
  80.     n_bytes = send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  81.     cout << n_bytes << endl;
  82.     n_bytes = recv(sock_on, buff, sizeof(buff), 0);
  83.     cout << buff << endl;
  84. //-----------------------------------------------------------------------------------------------
  85.     sprintf(szMsgLine, "%s%s", hasloN.c_str(), CRLF);
  86.     n_bytes = send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  87.     cout << n_bytes << endl;
  88.     n_bytes = recv(sock_on, buff, sizeof(buff), 0);
  89.     cout << buff << endl;
  90. //----------------------------------------------------------------- //
  91.     sprintf(szMsgLine, "MAIL FROM:<%s>%s", senderMail.c_str(), CRLF);
  92.     n_bytes = send(sock_on, szMsgLine, strlen(szMsgLine),0);
  93.     cout << n_bytes << endl;
  94.     n_bytes = recv(sock_on, buff, sizeof(buff), 0);
  95.    
  96.     cout << buff << ", " << n_bytes << endl;
  97. //------------------------------------------------------
  98.     sprintf(szMsgLine, "RCPT TO:<%s>%s", reciverMail.c_str(), CRLF);
  99.     n_bytes = send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  100.     cout << n_bytes << endl;
  101.     n_bytes = recv(sock_on, buff, sizeof(buff), 0);
  102.     cout << buff << endl << n_bytes << endl;
  103.    
  104. //-------------------------------------------------------
  105.     sprintf(szMsgLine, "DATA%s", CRLF);
  106.     n_bytes = send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  107.     cout << n_bytes << endl;
  108.  
  109.     n_bytes = recv(sock_on, buff, sizeof(buff), 0);
  110.     cout << buff << endl;
  111.  
  112.     string testwiadomosc = "testpop3test1@wp.pl";
  113.     sprintf(szMsgLine, "FROM: %s%s", testwiadomosc.c_str(), CRLF);
  114.     send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  115.  
  116.  
  117.     testwiadomosc = "data";
  118.     sprintf(szMsgLine, "Date: %s%s", testwiadomosc.c_str(), CRLF);
  119.     send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  120.  
  121.     testwiadomosc = "Temat";
  122.     sprintf(szMsgLine, "Subject: %s%s", testwiadomosc.c_str(), CRLF);
  123.     send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  124.  
  125.     testwiadomosc = "test smtptest";
  126.     sprintf(szMsgLine, "%s%s", testwiadomosc.c_str(), CRLF);
  127.     send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  128.  
  129.     testwiadomosc = "test smtptest";
  130.     sprintf(szMsgLine, "%s%s", testwiadomosc.c_str(), CRLF);
  131.     send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  132.  
  133.     sprintf(szMsgLine, "%s%s", testwiadomosc.c_str(), CRLF);
  134.     send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  135. //-----------------------------------------------------
  136.     sprintf(szMsgLine, "%s.%s", CRLF, CRLF);
  137.     n_bytes = send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  138.     cout << n_bytes << endl;
  139.  
  140.     n_bytes = recv(sock_on, buff, sizeof(buff), 0);
  141.     cout << buff << endl;
  142. //-----------------------------------------------------
  143. // Send QUIT
  144.     sprintf(szMsgLine, "QUIT%s", CRLF);
  145.     send(sock_on, szMsgLine, strlen(szMsgLine), 0);
  146.     recv(sock_on, buff, sizeof(buff), 0);
  147.     cout << buff << endl;
  148.     cout << "Sent as email message to " << reciverMail << endl;
  149.    
  150.     closesocket(sock_on);
  151.     getchar();
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement