CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #1
    Join Date
    Apr 2010
    Posts
    6

    Simple Email Sending

    So this is my code, Everything seems to work minus the fact that no email actually gets sent, what is the reason for this?
    Code:
    #define WIN32_LEAN_AND_MEAN
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <fstream>
    #include <iostream>
    #include <windows.h>
    #include <winsock2.h>
    using namespace std;
    #pragma comment(lib, "ws2_32.lib")
    
    #define CRLF "\r\n"   
      
    const int VERSION_MAJOR = 1;
    const int VERSION_MINOR = 1;
    
    using namespace std;       
    
    void ShowUsage(void)
    {
      cout << "Usage: SENDMAIL mailserv to_addr from_addr messagefile" << endl
           << "Example: SENDMAIL smtp.myisp.com [email protected] [email protected] message.txt" << endl;
      system("pause");
      exit(1);
    }
    
    
    void Check(int iStatus, char *szFunction)
    {
      if((iStatus != SOCKET_ERROR) && (iStatus))
        return;
      
      cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;
    }
    
    int main(int argc, char *argv[])
    {
      int         iProtocolPort ;
      char        szSmtpServerName[64] = "smtp.live.com";
      char        szToAddrA[64]        = "[email protected]";   // First account setup for this test
      char        szFromAddr[64]       = "[email protected]";  // Second account setup for this test
      char        szBuffer[4096]       = "";
      char        szLine[255]          = "";
      char        szMsgLine[255]       = "";
      SOCKET      hServer;
      WSADATA     WSData;
      LPHOSTENT   lpHostEntry;
      LPSERVENT   lpServEntry;
      SOCKADDR_IN SockAddr;
      
      const char *whatIsThisShit = szFromAddr;
    
      lstrcpy(szSmtpServerName, argv[1]);
      lstrcpy(szFromAddr, argv[3]);
      
      if(argc != 5)
        ShowUsage();
        
      argv[3] = szToAddrA;
    //ifstream MsgFile(whatIsThisShit);  // Use this if not using a file for the body text
      ifstream MsgFile("body.txt");
      if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
      {
        cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
        system("pause");
        return 1;
      }
    cout << "Found Winsock version " << VERSION_MAJOR << "." << VERSION_MINOR << "\n";
      lpHostEntry = gethostbyname(szSmtpServerName);
      if(!lpHostEntry)
      {
        cout << "Cannot find SMTP mail server " << szSmtpServerName << endl;
        system("pause");
        return 1;
      }
    cout << "Found SMTP server (" << lpHostEntry << ")\n";
      hServer = socket(PF_INET, SOCK_STREAM, 0);
      if(hServer == INVALID_SOCKET)
      {
        cout << "Cannot open mail server socket" << endl;
        system("pause");
        return 1;
      }
    cout << "Created TCP/IP mail server socket\n";
      lpServEntry = getservbyname("mail", 0);
      // Use the SMTP default port if no other port is specified
      if(!lpServEntry)
        iProtocolPort = htons(IPPORT_SMTP);
      else
        iProtocolPort = lpServEntry->s_port;
    cout << "Checked port\n";
      SockAddr.sin_family = AF_INET;
      SockAddr.sin_port   = iProtocolPort;
      SockAddr.sin_addr   = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
    cout << "Setup socket address structure\n";
      if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
      {
        cout << "Error connecting to Server socket" << endl;
        system("pause");
        return 1;
      }
    cout << "connected to server\n";
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
    
      sprintf(szMsgLine, "HELO &#37;s%s", szSmtpServerName, CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() HELO");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() HELO");
    cout << "Creating message data\n";
      sprintf(szMsgLine, "MAIL FROM:<%s>%s", szFromAddr, CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() MAIL FROM");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() MAIL FROM");
    cout << "  - added mail sender\n";
      sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddrA, CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");
    cout << "  - added mail reciever\n";
      // Send DATA
      sprintf(szMsgLine, "DATA%s", CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA");
    cout << "  - added mail data\n";
      MsgFile.getline(szLine, sizeof(szLine));            
    
      do
      {
        sprintf(szMsgLine, "%s%s", szLine, CRLF);
        Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line");
        MsgFile.getline(szLine, sizeof(szLine));
      } while(MsgFile.good());
    cout << "  - added mail body\n";
    cout << "Attempting to send\n\n";
      sprintf(szMsgLine, "QUIT%s", CRLF);
      Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT");
      Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT");
    cout << "sent message\n";
    //SENDMAIL("smtp.live.com", szToAddrA, szFromAddr, MsgFile);
      cout << "Sent " << whatIsThisShit << " as email message to " << szToAddrA << endl;
      closesocket(hServer);
    
      WSACleanup();
      system("pause");
      return 0;
    }
    Last edited by xedon; April 11th, 2010 at 12:41 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured