Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net.Sockets;
- using System.Security.Cryptography.X509Certificates;
- using System.Net.Security;
- using System.Security.Authentication;
- using System.IO;
- using System.Windows.Forms;
- using System.Diagnostics;
- namespace FilePOP.Source.POP3
- {
- public sealed class POP3Mail
- {
- #region Properties/Variables
- private Stream currentStream;
- private StreamReader streamReader;
- private ASCIIEncoding streamEncoding = new ASCIIEncoding();
- private String serverResponse;
- /// <summary>
- /// The name of the POP3 server.
- /// </summary>
- private String server = String.Empty;
- public String Server
- {
- get { return server; }
- set { server = value; }
- }
- /// <summary>
- /// The port number of the POP3 server.
- /// </summary>
- private int port = 0;
- public int Port
- {
- get { return port; }
- set { port = value; }
- }
- /// <summary>
- /// Server requires a secure tunnel.
- /// </summary>
- private bool secureConnection = false;
- public bool SecureConnection
- {
- get { return secureConnection; }
- set { secureConnection = value; }
- }
- /// <summary>
- /// Server connection status.
- /// </summary>
- private bool connected = false;
- public bool Connected
- {
- get { return connected; }
- set { connected = value; }
- }
- private String username = String.Empty;
- public String Username
- {
- get { return username; }
- set { username = value; }
- }
- private String password = String.Empty;
- public String Password
- {
- get { return password; }
- set { password = value; }
- }
- private int messageCount = 0;
- public int MessageCount
- {
- get { return messageCount; }
- private set { messageCount = value; }
- }
- #endregion
- /// <summary>
- /// Creates a POP3 object with no parameters.
- /// </summary>
- public POP3Mail() { }
- /// <summary>
- /// Creates a POP3 object ready to connect to the server with the specified port.
- /// Secure tunnel option is specified as well.
- /// </summary>
- /// <param name="server">POP3 Server to connect to</param>
- /// <param name="port">Port to connect on.</param>
- /// <param name="bSecureConnection">Server requires a secure tunnel.</param>
- public POP3Mail(String server, int port, bool bSecureConnection)
- {
- Server = server;
- Port = port;
- SecureConnection = bSecureConnection;
- }
- /// <summary>
- /// Connects to the POP3 server.
- /// </summary>
- public void Connect()
- {
- try
- {
- TcpClient popClient = new TcpClient(Server, Port);
- if (SecureConnection)
- {
- SslStream sslStream = new SslStream(popClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
- sslStream.AuthenticateAsClient(Server);
- currentStream = sslStream;
- streamReader = new StreamReader(currentStream);
- serverResponse = streamReader.ReadLine();
- #if DEBUG
- Debug.Print(serverResponse);
- #endif
- POP3SendUserData(Username);
- POP3SendPasswordData(Password);
- POP3GetMessageCount();
- Connected = true;
- }
- else
- {
- currentStream = popClient.GetStream();
- streamReader = new StreamReader(currentStream);
- serverResponse = streamReader.ReadLine();
- #if DEBUG
- Debug.Print(serverResponse);
- #endif
- POP3SendUserData(Username);
- POP3SendPasswordData(Password);
- POP3GetMessageCount();
- Connected = true;
- }
- }
- catch (AuthenticationException e)
- {
- currentStream.Close();
- Connected = false;
- throw new Exception("SSL Authentication error. Please check your settings.");
- }
- catch (Exception e)
- {
- Connected = false;
- throw new Exception(e.Message);
- }
- }
- /// <summary>
- /// Reads a message from the POP3 server.
- /// </summary>
- /// <param name="index">Index of the mail to read.</param>
- /// <returns>Mail message</returns>
- public String ReadMessage(int index)
- {
- POP3SendMessage("retr " + index);
- serverResponse = streamReader.ReadLine();
- #if DEBUG
- Debug.Print(serverResponse);
- #endif
- if (serverResponse.Contains("-ERR"))
- {
- throw new Exception("Unable to retrieve messages. The POP3 server may be down.");
- }
- streamReader.ReadLine();
- while (streamReader.Peek() >= 0)
- {
- serverResponse += streamReader.ReadLine() + Environment.NewLine ;
- }
- return serverResponse;
- }
- /// <summary>
- /// Sends the username to the POP3 server.
- /// </summary>MODE_Separator_Paint
- /// <param name="user">Username</param>
- private void POP3SendUserData(String user)
- {
- POP3SendMessage("user " + user);
- serverResponse = streamReader.ReadLine();
- #if DEBUG
- Debug.Print(serverResponse);
- #endif
- if (serverResponse.Contains("-ERR"))
- {
- throw new Exception("Unable to authenticate. Please check your settings.");
- }
- }
- /// <summary>
- /// Sends the password to the POP3 server.
- /// </summary>
- /// <param name="password">Password</param>
- private void POP3SendPasswordData(String password)
- {
- POP3SendMessage("pass " + password);
- serverResponse = streamReader.ReadLine();
- #if DEBUG
- Debug.Print(serverResponse);
- #endif
- if (serverResponse.Contains("-ERR"))
- {
- throw new Exception("Unable to authenticate. Please check your settings.");
- }
- }
- /// <summary>
- /// Gets the total number of messages on the POP3 server.
- /// </summary>
- private void POP3GetMessageCount()
- {
- POP3SendMessage("stat");
- serverResponse = streamReader.ReadLine();
- #if DEBUG
- Debug.Print(serverResponse);
- #endif
- if (serverResponse.Contains("-ERR"))
- {
- throw new Exception("Unable to determine messeges. The POP3 server may be down.");
- }
- int responsePosition = serverResponse.IndexOf(" ", 4);
- String messageCount = serverResponse.Substring(4, responsePosition - 4);
- MessageCount = Int32.Parse(messageCount);
- #if DEBUG
- Debug.Print("Total Messages: {0}", messageCount);
- #endif
- }
- /// <summary>
- /// Sends data to the POP3 server.
- /// </summary>
- /// <param name="message">Message</param>
- private void POP3SendData(String message)
- {
- byte[] byteData = streamEncoding.GetBytes(message);
- currentStream.Write(byteData, 0, byteData.Length);
- }
- /// <summary>
- /// Sends message to the POP3 server.
- /// </summary>
- /// <param name="message">Message</param>
- private void POP3SendMessage(String message)
- {
- POP3SendData(message + "\r\n");
- }
- public void Disconnect()
- {
- POP3SendMessage("quit");
- #if DEBUG
- Debug.Print(serverResponse);
- #endif
- if (serverResponse.Contains("-ERR"))
- {
- throw new Exception("Unable to disconnect from server.");
- }
- }
- /// <summary>
- /// Deleted the message via the index.
- /// </summary>
- /// <param name="index">Index of the message to delete.</param>
- public void DeleteMessage(int index)
- {
- // NOTE: Not all POP3 servers send the +OK reply after marking for deletion!!
- POP3SendMessage("dele " + index.ToString());
- serverResponse = streamReader.ReadLine();
- #if DEBUG
- Debug.Print(serverResponse);
- #endif
- if (serverResponse.Contains("-ERR"))
- {
- throw new Exception("Unable to delete messages.");
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="certificate"></param>
- /// <param name="chain"></param>
- /// <param name="sslPolicyErrors"></param>
- /// <returns></returns>
- private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
- {
- if (sslPolicyErrors == SslPolicyErrors.None)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement