SHARE
TWEET

Untitled

a guest May 31st, 2017 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net.Sockets;
  5. using System.Security.Cryptography.X509Certificates;
  6. using System.Net.Security;
  7. using System.Security.Authentication;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11.  
  12. namespace FilePOP.Source.POP3
  13. {
  14.     public sealed class POP3Mail
  15.     {
  16.         #region Properties/Variables
  17.  
  18.         private Stream currentStream;
  19.         private StreamReader streamReader;
  20.         private ASCIIEncoding streamEncoding = new ASCIIEncoding();
  21.         private String serverResponse;
  22.  
  23.         /// <summary>
  24.         /// The name of the POP3 server.
  25.         /// </summary>
  26.         private String server = String.Empty;
  27.         public String Server
  28.         {
  29.             get { return server; }
  30.             set { server = value; }
  31.         }
  32.  
  33.         /// <summary>
  34.         /// The port number of the POP3 server.
  35.         /// </summary>
  36.         private int port = 0;
  37.         public int Port
  38.         {
  39.             get { return port; }
  40.             set { port = value; }
  41.         }
  42.  
  43.         /// <summary>
  44.         /// Server requires a secure tunnel.
  45.         /// </summary>
  46.         private bool secureConnection = false;
  47.         public bool SecureConnection
  48.         {
  49.             get { return secureConnection; }
  50.             set { secureConnection = value; }
  51.         }
  52.  
  53.         /// <summary>
  54.         /// Server connection status.
  55.         /// </summary>
  56.         private bool connected = false;
  57.         public bool Connected
  58.         {
  59.             get { return connected; }
  60.             set { connected = value; }
  61.         }
  62.  
  63.         private String username = String.Empty;
  64.         public String Username
  65.         {
  66.             get { return username; }
  67.             set { username = value; }
  68.         }
  69.  
  70.         private String password = String.Empty;
  71.         public String Password
  72.         {
  73.             get { return password; }
  74.             set { password = value; }
  75.         }
  76.  
  77.         private int messageCount = 0;
  78.         public int MessageCount
  79.         {
  80.             get { return messageCount; }
  81.             private set { messageCount = value; }
  82.         }
  83.  
  84.         #endregion
  85.         /// <summary>
  86.         /// Creates a POP3 object with no parameters.
  87.         /// </summary>
  88.         public POP3Mail() { }
  89.  
  90.         /// <summary>
  91.         /// Creates a POP3 object ready to connect to the server with the specified port.  
  92.         /// Secure tunnel option is specified as well.
  93.         /// </summary>
  94.         /// <param name="server">POP3 Server to connect to</param>
  95.         /// <param name="port">Port to connect on.</param>
  96.         /// <param name="bSecureConnection">Server requires a secure tunnel.</param>
  97.         public POP3Mail(String server, int port, bool bSecureConnection)
  98.         {
  99.             Server = server;
  100.             Port = port;
  101.             SecureConnection = bSecureConnection;
  102.         }
  103.  
  104.         /// <summary>
  105.         /// Connects to the POP3 server.  
  106.         /// </summary>
  107.         public void Connect()
  108.         {
  109.             try
  110.             {
  111.                 TcpClient popClient = new TcpClient(Server, Port);
  112.  
  113.                 if (SecureConnection)
  114.                 {
  115.                     SslStream sslStream = new SslStream(popClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
  116.                     sslStream.AuthenticateAsClient(Server);
  117.                     currentStream = sslStream;
  118.  
  119.                     streamReader = new StreamReader(currentStream);
  120.                     serverResponse = streamReader.ReadLine();
  121. #if DEBUG
  122.                     Debug.Print(serverResponse);
  123. #endif
  124.  
  125.                     POP3SendUserData(Username);
  126.                     POP3SendPasswordData(Password);
  127.                     POP3GetMessageCount();
  128.                    
  129.                     Connected = true;
  130.  
  131.                 }
  132.                 else
  133.                 {
  134.                    
  135.                     currentStream = popClient.GetStream();
  136.  
  137.                     streamReader = new StreamReader(currentStream);
  138.                     serverResponse = streamReader.ReadLine();
  139. #if DEBUG
  140.                     Debug.Print(serverResponse);
  141. #endif
  142.  
  143.                     POP3SendUserData(Username);
  144.                     POP3SendPasswordData(Password);
  145.                     POP3GetMessageCount();
  146.  
  147.  
  148.                     Connected = true;
  149.                 }
  150.             }
  151.             catch (AuthenticationException e)
  152.             {
  153.                 currentStream.Close();
  154.                 Connected = false;
  155.                 throw new Exception("SSL Authentication error.  Please check your settings.");
  156.             }
  157.             catch (Exception e)
  158.             {
  159.                 Connected = false;
  160.                 throw new Exception(e.Message);
  161.             }
  162.  
  163.         }
  164.  
  165.         /// <summary>
  166.         /// Reads a message from the POP3 server.
  167.         /// </summary>
  168.         /// <param name="index">Index of the mail to read.</param>
  169.         /// <returns>Mail message</returns>
  170.         public String ReadMessage(int index)
  171.         {
  172.             POP3SendMessage("retr " + index);
  173.             serverResponse = streamReader.ReadLine();
  174. #if DEBUG
  175.             Debug.Print(serverResponse);
  176. #endif
  177.             if (serverResponse.Contains("-ERR"))
  178.             {
  179.                 throw new Exception("Unable to retrieve messages.  The POP3 server may be down.");
  180.             }
  181.  
  182.             streamReader.ReadLine();
  183.             while (streamReader.Peek() >= 0)
  184.             {
  185.                 serverResponse += streamReader.ReadLine() + Environment.NewLine ;
  186.                
  187.             }
  188.  
  189.             return serverResponse;
  190.         }
  191.         /// <summary>
  192.         /// Sends the username to the POP3 server.
  193.         /// </summary>MODE_Separator_Paint
  194.         /// <param name="user">Username</param>
  195.         private void POP3SendUserData(String user)
  196.         {
  197.             POP3SendMessage("user " + user);
  198.             serverResponse = streamReader.ReadLine();
  199. #if DEBUG
  200.             Debug.Print(serverResponse);
  201. #endif
  202.             if (serverResponse.Contains("-ERR"))
  203.             {
  204.                 throw new Exception("Unable to authenticate.  Please check your settings.");
  205.             }
  206.         }
  207.  
  208.         /// <summary>
  209.         /// Sends the password to the POP3 server.
  210.         /// </summary>
  211.         /// <param name="password">Password</param>
  212.         private void POP3SendPasswordData(String password)
  213.         {
  214.             POP3SendMessage("pass " + password);
  215.             serverResponse = streamReader.ReadLine();
  216. #if DEBUG
  217.             Debug.Print(serverResponse);
  218. #endif
  219.             if (serverResponse.Contains("-ERR"))
  220.             {
  221.                 throw new Exception("Unable to authenticate.  Please check your settings.");
  222.             }
  223.         }
  224.  
  225.         /// <summary>
  226.         /// Gets the total number of messages on the POP3 server.
  227.         /// </summary>
  228.         private void POP3GetMessageCount()
  229.         {
  230.             POP3SendMessage("stat");
  231.             serverResponse = streamReader.ReadLine();
  232. #if DEBUG
  233.             Debug.Print(serverResponse);
  234. #endif
  235.             if (serverResponse.Contains("-ERR"))
  236.             {
  237.                 throw new Exception("Unable to determine messeges. The POP3 server may be down.");
  238.             }
  239.  
  240.             int responsePosition = serverResponse.IndexOf(" ", 4);
  241.             String messageCount = serverResponse.Substring(4, responsePosition - 4);
  242.             MessageCount = Int32.Parse(messageCount);
  243. #if DEBUG
  244.             Debug.Print("Total Messages: {0}", messageCount);
  245. #endif
  246.         }
  247.  
  248.         /// <summary>
  249.         /// Sends data to the POP3 server.
  250.         /// </summary>
  251.         /// <param name="message">Message</param>
  252.         private void POP3SendData(String message)
  253.         {
  254.             byte[] byteData = streamEncoding.GetBytes(message);
  255.             currentStream.Write(byteData, 0, byteData.Length);
  256.         }
  257.  
  258.         /// <summary>
  259.         /// Sends message to the POP3 server.
  260.         /// </summary>
  261.         /// <param name="message">Message</param>
  262.         private void POP3SendMessage(String message)
  263.         {
  264.             POP3SendData(message + "\r\n");
  265.         }
  266.  
  267.         public void Disconnect()
  268.         {
  269.             POP3SendMessage("quit");
  270.  
  271. #if DEBUG
  272.             Debug.Print(serverResponse);
  273. #endif
  274.             if (serverResponse.Contains("-ERR"))
  275.             {
  276.                 throw new Exception("Unable to disconnect from server.");
  277.             }
  278.         }
  279.         /// <summary>
  280.         /// Deleted the message via the index.
  281.         /// </summary>
  282.         /// <param name="index">Index of the message to delete.</param>
  283.         public void DeleteMessage(int index)
  284.         {
  285.             // NOTE:  Not all POP3 servers send the +OK reply after marking for deletion!!
  286.             POP3SendMessage("dele " + index.ToString());
  287.             serverResponse = streamReader.ReadLine();
  288. #if DEBUG
  289.             Debug.Print(serverResponse);
  290. #endif
  291.             if (serverResponse.Contains("-ERR"))
  292.             {
  293.                 throw new Exception("Unable to delete messages.");
  294.             }
  295.         }
  296.         /// <summary>
  297.         ///
  298.         /// </summary>
  299.         /// <param name="sender"></param>
  300.         /// <param name="certificate"></param>
  301.         /// <param name="chain"></param>
  302.         /// <param name="sslPolicyErrors"></param>
  303.         /// <returns></returns>
  304.         private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  305.         {
  306.             if (sslPolicyErrors == SslPolicyErrors.None)
  307.             {
  308.                 return true;
  309.             }
  310.             else
  311.             {
  312.                 return false;
  313.             }
  314.         }
  315.     }
  316. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top