Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net.Sockets;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Net.Security;
- using System.Security.Cryptography;
- using System.Text;
- using System.Linq;
- using System.Collections.Generic;
- namespace System.Net.Mail{
- public struct MailboxStatus{
- public int Messages{ get; set; }
- public int Size{ get; set; }
- public override string ToString(){
- return string.Format("[MailboxStatus: Messages={0}, Size={1}]", Messages, Size);
- }
- }
- public struct MessageInfo{
- public int MessageNumber{ get; set; }
- public int Size{ get; set; }
- public override string ToString(){
- return string.Format("[MessageInfo: MessageNumber={0}, Size={1}]", MessageNumber, Size);
- }
- }
- public class POP3Error:Exception{
- public override string Message{ get{return _Message;} }
- private string _Message;
- public POP3Error(string Message){
- _Message = Message;
- }
- public override string ToString(){
- return string.Format("[POP3Error: Message={0}]", Message);
- }
- }
- public class POP3Client{
- private TcpClient tcpClient;
- private TextWriter sender;
- private TextReader receiver;
- private string APOPChallange;
- public POP3Client(){
- APOPChallange = "";
- }
- public void Connect(string Server, int Port = 110, bool UseSSL = false){
- tcpClient = new TcpClient();
- tcpClient.Connect(Server,Port);
- Stream stream = tcpClient.GetStream();
- if(UseSSL){
- stream = new SslStream(stream);
- ((SslStream)stream).AuthenticateAsClient(Server);
- }
- sender = new StreamWriter(stream);
- receiver = new StreamReader(stream);
- string Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- APOPChallange = Regex.Match(Line, "^\\+OK (<[^>]+>)").Groups[1].Value;
- }
- public bool AuthenticateUser(string Username, string Password){
- string Line;
- if(!String.IsNullOrEmpty(APOPChallange)){
- string APOPResponse = string.Join("", MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(APOPChallange + Password)).Select(b => string.Format("{0:x2}", b)));
- sender.WriteLine("APOP {0} {1}", Username, APOPResponse);
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- return false;
- }
- return true;
- }
- else{
- sender.WriteLine("USER {0}", Username);
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- return false;
- }
- sender.WriteLine("PASS {0}", Password);
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new Exception(Line);
- }
- return true;
- }
- }
- public MailboxStatus Status(){
- string Line;
- sender.WriteLine("STAT");
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- Match match = Regex.Match(Line, "^\\+OK[ \t]+([0-9]+)[ \t]+([0-9]+)");
- if(match.Groups.Count == 3){
- int Messages, Size;
- if(int.TryParse(match.Groups[1].Value, out Messages) && int.TryParse(match.Groups[2].Value, out Size)){
- return new MailboxStatus{Messages = Messages, Size = Size};
- }
- throw new FormatException("Invalid status response");
- }
- throw new FormatException("Invalid status response");
- }
- public List<MessageInfo> List(int MessageNumber = 0){
- if(MessageNumber <= 0){
- string Line;
- sender.WriteLine("LIST");
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- List<MessageInfo> Messages = new List<MessageInfo>();
- while((Line = receiver.ReadLine()) != null){
- if(Line == "."){
- break;
- }
- Match match = Regex.Match(Line, "^\\+OK[ \t]+([0-9]+)[ \t]+([0-9]+)");
- if(match.Groups.Count == 3){
- int Size;
- if(int.TryParse(match.Groups[1].Value, out MessageNumber) && int.TryParse(match.Groups[2].Value, out Size)){
- Messages.Add(new MessageInfo{MessageNumber = MessageNumber, Size = Size});
- }
- else{
- throw new FormatException("Invalid list response");
- }
- }
- else{
- throw new FormatException("Invalid list response");
- }
- }
- return Messages;
- }
- else{
- string Line;
- sender.WriteLine("LIST {0}",MessageNumber);
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- Match match = Regex.Match(Line, "^\\+OK[ \t]+([0-9]+)[ \t]+([0-9]+)");
- if(match.Groups.Count == 3){
- int Size;
- if(int.TryParse(match.Groups[1].Value, out MessageNumber) && int.TryParse(match.Groups[2].Value, out Size)){
- return new List<MessageInfo>(new MessageInfo[]{new MessageInfo{MessageNumber = MessageNumber, Size = Size}});
- }
- throw new FormatException("Invalid list response");
- }
- throw new FormatException("Invalid list response");
- }
- }
- public string Retrieve(int MessageNumber){
- string Line;
- sender.WriteLine("RETR {0}",MessageNumber);
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- string MailContent = "";
- while((Line = receiver.ReadLine())!=null){
- if(Line == "."){
- break;
- }
- MailContent += Line + Environment.NewLine;
- }
- return MailContent;
- }
- public string Retrieve(int MessageNumber, int Lines){
- string Line;
- sender.WriteLine("TOP {0} {1}",MessageNumber,Lines);
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- string MailContent = "";
- while((Line = receiver.ReadLine())!=null){
- if(Line == "."){
- break;
- }
- MailContent += Line + Environment.NewLine;
- }
- return MailContent;
- }
- public List<string> Capabilities(){
- string Line;
- sender.WriteLine("CAPA");
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- List<string> capas = new List<string>();
- while((Line = receiver.ReadLine())!=null){
- if(Line == "."){
- break;
- }
- capas.Add(Line);
- }
- return capas;
- }
- public string UniqueID(int MessageNumber){
- string Line;
- sender.WriteLine("UIDL {0}",MessageNumber);
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- string uidl = Regex.Match(Line, "^\\+OK[ \t][0-9]+[ \t](.+)").Groups[1].Value;
- if(string.IsNullOrEmpty(uidl)){
- throw new POP3Error("Invalid uidl response");
- }
- return uidl;
- }
- public void Reset(){
- string Line;
- sender.WriteLine("RSET");
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- }
- public void Delete(int MessageNumber){
- string Line;
- sender.WriteLine("DELE {0}", MessageNumber);
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- }
- public void Quit(){
- string Line;
- sender.WriteLine("QUIT");
- sender.Flush();
- Line = receiver.ReadLine();
- if(!Line.ToUpper().StartsWith("+OK")){
- throw new POP3Error(Line);
- }
- receiver.Close();
- sender.Close();
- tcpClient.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement