home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2002 September / PCPlus_193_Laplink2000.iso / Prog / C++ / Pop3Client.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-07  |  2.9 KB  |  107 lines

  1. #include "stdafx.h"
  2. #using <mscorlib.dll>
  3. #using <system.dll>
  4.  
  5. using namespace System;
  6. using namespace System::IO;
  7. using namespace System::Text;
  8. using System::Net::Sockets::NetworkStream;
  9. using System::Net::Sockets::TcpClient;
  10. using System::IO::StreamReader;
  11.  
  12. #include "Pop3Client.h"
  13.  
  14. void CPOP3Client::CPop3Client()
  15. {
  16.     m_Client = 0;
  17.     m_strPOP3HostName = String::Empty;
  18.     m_strUserName = String::Empty;
  19.     m_strPassword = String::Empty;
  20.     m_strStatus = new String ("OK");
  21. }
  22.  
  23. void CPOP3Client::Close()
  24. {
  25.     if (m_Client) m_Client->Close();
  26.     m_Client = 0;
  27. }
  28.  
  29. bool CPOP3Client::ConnectToServer()
  30. {
  31.     Close();                                                                                // Close existing connection.
  32.     m_Client = new TcpClient (m_strPOP3HostName, 110);                                        // Create connection to the server.
  33.     StreamReader * streamRead = new StreamReader (m_Client->GetStream(), Encoding::ASCII );    // Create stream to read.
  34.     String * resp = streamRead->ReadLine();                                                    // read server response
  35.     streamRead->DiscardBufferedData();                                                        // Ignore rest of the buffer.
  36.  
  37.     if (resp->StartsWith("+OK")) return true;                                                // Check response for +OK reply.
  38.     else
  39.     {
  40.         Close();
  41.         return false;
  42.     }
  43. }
  44.  
  45. void CPOP3Client::DisconnectFromServer()
  46. {
  47.     String *strResponse = String::Empty;
  48.     SendCommand("QUIT", &strResponse);
  49.     Close();
  50. }
  51.  
  52. bool CPOP3Client::SendCommand (String *strCommand, String** pstrOutput)
  53. {
  54.     if (m_Client == 0) return false;
  55.  
  56.     // Add crlf to the command.
  57.     String * strRequest = String::Concat(strCommand, "\r\n");
  58.  
  59.     // Create streams to read from socket and write to it.
  60.     NetworkStream * streamWrite = m_Client->GetStream();
  61.     StreamReader * streamRead = new StreamReader(streamWrite, Encoding::ASCII);
  62.  
  63.     // Convert from the string to the byte stream and write to socket.
  64.     Byte outbuffer __gc[] = Encoding::ASCII->GetBytes(strRequest);
  65.     streamWrite->Write(outbuffer, 0, outbuffer->Length);
  66.  
  67.     // read response from the socket.
  68.     *pstrOutput = streamRead->ReadLine();
  69.  
  70.     // Ignore rest of the buffer.
  71.     streamRead->DiscardBufferedData();
  72.  
  73.     return (*pstrOutput)->StartsWith(S"+OK") ? true : false;
  74. }
  75.  
  76. int CPOP3Client::MessageCount()
  77. {
  78.     String *strResponse = String::Empty;
  79.     String *strNrOfMessages;
  80.  
  81.     // Create array of separators to parse response from server.
  82.     Char separator __gc[] = new Char __gc[1];
  83.     separator[0] = ' ';
  84.  
  85.     try
  86.     {
  87.         if(!ConnectToServer ()) return -1;
  88.         if(!SendCommand (String::Concat("USER ", m_strUserName), &strResponse)) return -1;
  89.         if(!SendCommand (String::Concat("PASS ", m_strPassword), &strResponse)) return -1;
  90.         if(!SendCommand ("STAT", &strResponse)) return -1;
  91.         
  92.         // Extract number of messages from the string.
  93.         strNrOfMessages = strResponse->Split(separator)[1];
  94.  
  95.         return Convert::ToInt32(strNrOfMessages);
  96.     }
  97.     catch(Exception* e)
  98.     {
  99.         m_strStatus = String::Concat ("Error: ", e->Message);
  100.         return -1;
  101.     }
  102.     __finally
  103.     {
  104.         DisconnectFromServer();
  105.     }
  106. }
  107.