Advertisement
Guest User

EMail sending - Sockets

a guest
Jul 30th, 2010
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2. #define New_Line "\r\n"
  3.  
  4. #include <iostream>
  5. #include<WinSock2.h>
  6. using namespace std;
  7. #include<string>
  8. #pragma comment (lib, "Ws2_32.lib")
  9. #include<process.h>
  10. #include<Windows.h>
  11. #include<conio.h>
  12. #include<stdio.h>
  13. #include "auto_ptr.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <fstream>
  17. #include <windows.h>
  18. #include <winsock2.h>
  19.  
  20.  
  21.  
  22.  
  23. void AppEnd();
  24. int eMailSend(const char const *Title, const char const *Message, const char const *from, const char const *To);
  25.  
  26. bool Check(int iStatus)
  27. {
  28.     if(iStatus == SOCKET_ERROR  || !iStatus)
  29.         cerr<< "error had Occured" << "eMailSend failed." << endl;
  30.     return iStatus == SOCKET_ERROR  || !iStatus;
  31. }
  32.  
  33. int main()
  34. {
  35.     SetConsoleTitle("Sockets training");
  36.  
  37.     if(eMailSend("Check", "is it Working?", MyMail, MyMail))
  38.         cerr<< "fatal error while sending the email";
  39.    
  40.     AppEnd();
  41.     return 0;
  42. }
  43.  
  44.  
  45. int eMailSend(const char const *Title, const char const *Message, const char const *from, const char const *To)
  46. {
  47.     int         iProtocolPort       = 0;
  48.     const char  SmtpServerName[64]  = "smtp.internetlibero.it";
  49.     char        Buffer[64]          = "";
  50.     char        MsgLine[500];
  51.     SOCKET      hServer;
  52.     LPHOSTENT   lpHostEntry;
  53.     LPSERVENT   lpServEntry;
  54.     SOCKADDR_IN SockAddr;
  55.  
  56.     // Attempt to intialize WinSock (1.1 or later)
  57.     WSADATA wsaData;
  58.     if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
  59.     {
  60.         cerr << "WSADATA failed";
  61.         AppEnd();
  62.     }
  63.  
  64.     // Lookup email server's IP address.
  65.     lpHostEntry = gethostbyname(SmtpServerName);
  66.     if(!lpHostEntry)
  67.     {
  68.         cout << "Cannot find SMTP mail server " << SmtpServerName << endl;
  69.         return -1;
  70.     }
  71.  
  72.     // Create a TCP/IP socket, no specific protocol
  73.     hServer = socket(PF_INET, SOCK_STREAM, 0);
  74.     if(hServer == INVALID_SOCKET)
  75.     {
  76.         cout << "Cannot open mail server socket" << endl;
  77.         return -1;
  78.     }
  79.  
  80.     // Get the mail service port
  81.     lpServEntry = getservbyname("mail", 0);
  82.  
  83.     // Use the SMTP default port if no other port is specified
  84.     if(!lpServEntry)
  85.         iProtocolPort = htons(25);
  86.     else
  87.         iProtocolPort = lpServEntry->s_port;
  88.  
  89.     // Setup a Socket Address structure
  90.     SockAddr.sin_family = AF_INET;
  91.     SockAddr.sin_port   = iProtocolPort;
  92.     SockAddr.sin_addr   = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
  93.  
  94.     // Connect the Socket
  95.     if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
  96.     {
  97.         cout << "Error connecting to Server socket" << endl;
  98.         return -1;
  99.     }
  100.  
  101.     // Receive initial response from SMTP server
  102.     if(Check(recv(hServer, Buffer, sizeof(Buffer), 0))) return -1;
  103.  
  104.     // Send HELO server.com
  105.     sprintf(MsgLine, "HELO %s%s", SmtpServerName, New_Line);
  106.     if(Check(send(hServer, MsgLine, strlen(MsgLine), 0))) return -1;
  107.     Check(recv(hServer, Buffer, sizeof(Buffer), 0));
  108.  
  109.     // Send MAIL FROM: <sender@mydomain.com>
  110.     sprintf(MsgLine, "MAIL FROM:<%s>%s", from, New_Line);
  111.     if(Check(send(hServer, MsgLine, strlen(MsgLine), 0))) return -1;
  112.     if(Check(recv(hServer, Buffer, sizeof(Buffer), 0))) return -1;
  113.  
  114.     // Send RCPT TO: <receiver@domain.com>
  115.     sprintf(MsgLine, "RCPT TO:<%s>%s", To, New_Line);
  116.     if(Check(send(hServer, MsgLine, strlen(MsgLine), 0))) return -1;
  117.     if(Check(recv(hServer, Buffer, sizeof(Buffer), 0))) return -1;
  118.  
  119.     // Send DATA
  120.     sprintf(MsgLine, "DATA%s", New_Line);
  121.     if(Check(send(hServer, MsgLine, strlen(MsgLine), 0))) return -1;
  122.     if(Check(recv(hServer, Buffer, sizeof(Buffer), 0))) return -1;
  123.  
  124.     // Send blank line and a period
  125.     sprintf(MsgLine, "%s.%s", New_Line, New_Line);
  126.     if(Check(send(hServer, MsgLine, strlen(MsgLine), 0))) return -1;
  127.     if(Check(recv(hServer, Buffer, sizeof(Buffer), 0))) return -1;
  128.  
  129.     // Send QUIT
  130.     sprintf(MsgLine, "QUIT%s", New_Line);
  131.     if(Check(send(hServer, MsgLine, strlen(MsgLine), 0))) return -1;
  132.     if(Check(recv(hServer, Buffer, sizeof(Buffer), 0))) return -1;
  133.  
  134.     // Close server socket and prepare to exit.
  135.     closesocket(hServer);
  136.  
  137.     return 0;
  138. }  
  139.  
  140. void AppEnd()
  141. {
  142.     cout << "\nEnd of program.";
  143.     if (WSACleanup() == SOCKET_ERROR)
  144.     {
  145.         cout<<"\nWSACleanup failed with error " << WSAGetLastError() << '\n';
  146.     }
  147.     _flushall();
  148.     _getch();
  149.     exit(0);
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement