Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <fstream>
  4. #include <conio.h>
  5. #pragma comment(lib, "ws2_32.lib")
  6.  
  7. // Insist on at least Winsock v1.1
  8. const int VERSION_MAJOR = 1;
  9. const int VERSION_MINOR = 1;
  10.  
  11. #define CRLF "rn" // carriage-return/line feed pair
  12. using namespace std;
  13.  
  14. // Basic error checking for send() and recv() functions
  15. void Check(int iStatus, char *szFunction)
  16. {
  17. if((iStatus != SOCKET_ERROR) && (iStatus))
  18. return;
  19.  
  20. cerr<< "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;
  21. }
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25. int iProtocolPort = 25;
  26. char szSmtpServerName[64] = "";
  27. char szToAddr[64] = "";
  28. char szFromAddr[64] = "";
  29. char szBuffer[4096] = "";
  30. char szLine[255] = "";
  31. char szMsgLine[255] = "";
  32. SOCKET hServer;
  33. WSADATA WSData;
  34. LPHOSTENT lpHostEntry;
  35. LPSERVENT lpServEntry;
  36. SOCKADDR_IN SockAddr;
  37.  
  38. // Check for four command-line args
  39. //if(argc != 5)
  40. // ShowUsage();
  41.  
  42. // Load command-line args
  43. lstrcpy(szSmtpServerName, "smtp.gmail.com");
  44. lstrcpy(szToAddr, "xxxx@gmail.com");
  45. lstrcpy(szFromAddr, "xxx@gmail.com");
  46.  
  47. // Create input stream for reading email message file
  48. ifstream MsgFile("D:\d.txt");
  49.  
  50. // Attempt to intialize WinSock (1.1 or later)
  51. if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
  52. {
  53. cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
  54.  
  55. return 1;
  56. }
  57.  
  58. // Lookup email server's IP address.
  59. lpHostEntry = gethostbyname(szSmtpServerName);
  60. if(!lpHostEntry)
  61. {
  62. cout << "Cannot find SMTP mail server " << szSmtpServerName << endl;
  63.  
  64. return 1;
  65. }
  66.  
  67. // Create a TCP/IP socket, no specific protocol
  68. hServer = socket(PF_INET, SOCK_STREAM, 0);
  69. if(hServer == INVALID_SOCKET)
  70. {
  71. cout << "Cannot open mail server socket" << endl;
  72.  
  73. return 1;
  74. }
  75.  
  76. // Get the mail service port
  77. lpServEntry = getservbyname("mail", 0);
  78.  
  79. // Use the SMTP default port if no other port is specified
  80. if(!lpServEntry)
  81. iProtocolPort = htons(IPPORT_SMTP);
  82. else
  83. iProtocolPort = lpServEntry->s_port;
  84.  
  85. // Setup a Socket Address structure
  86. SockAddr.sin_family = AF_INET;
  87. SockAddr.sin_port = iProtocolPort;
  88. SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
  89.  
  90. // Connect the Socket
  91. if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
  92. {
  93. cout << "Error connecting to Server socket" << endl;
  94.  
  95. return 1;
  96. }
  97.  
  98. // Receive initial response from SMTP server
  99. Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
  100.  
  101. // Send HELO server.com
  102. sprintf(szMsgLine, "HELO %s%s", szSmtpServerName, CRLF);
  103. Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() HELO");
  104. Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() HELO");
  105.  
  106. // Send MAIL FROM: <sender@mydomain.com>
  107. sprintf(szMsgLine, "MAIL FROM:<%s>%s", szFromAddr, CRLF);
  108. Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() MAIL FROM");
  109. Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() MAIL FROM");
  110.  
  111. // Send RCPT TO: <receiver@domain.com>
  112. sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddr, CRLF);
  113. Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");
  114. Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");
  115.  
  116. // Send DATA
  117. sprintf(szMsgLine, "DATA%s", CRLF);
  118. Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA");
  119. Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA");
  120. //strat writing about the subject, end it with two CRLF chars and after that you can
  121. //write data to the body oif the message
  122. sprintf(szMsgLine, "Subject: My own subject %s%s", CRLF, CRLF);
  123. Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA");
  124.  
  125.  
  126.  
  127.  
  128. // Send all lines of message body (using supplied text file)
  129. MsgFile.getline(szLine, sizeof(szLine)); // Get first line
  130.  
  131. do // for each line of message text...
  132. {
  133. sprintf(szMsgLine, "%s%s", szLine, CRLF);
  134. Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line");
  135. MsgFile.getline(szLine, sizeof(szLine)); // get next line.
  136. } while(!MsgFile.eof());
  137.  
  138. // Send blank line and a period
  139. sprintf(szMsgLine, "%s.%s", CRLF, CRLF);
  140. Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() end-message");
  141. Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() end-message");
  142.  
  143. // Send QUIT
  144. sprintf(szMsgLine, "QUIT%s", CRLF);
  145. Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT");
  146. Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT");
  147.  
  148. // Report message has been sent
  149. cout<< "Sent " << argv[4] << " as email message to " << szToAddr << endl;
  150.  
  151. // Close server socket and prepare to exit.
  152. closesocket(hServer);
  153.  
  154. WSACleanup();
  155. _getch();
  156. return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement