Advertisement
isendrak

System.Net.Mail.POP3Client

Jul 29th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.38 KB | None | 0 0
  1. using System;
  2. using System.Net.Sockets;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Net.Security;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10.  
  11. namespace System.Net.Mail{
  12.     public struct MailboxStatus{
  13.         public int Messages{ get; set; }
  14.         public int Size{ get; set; }
  15.         public override string ToString(){
  16.             return string.Format("[MailboxStatus: Messages={0}, Size={1}]", Messages, Size);
  17.         }
  18.     }
  19.     public struct MessageInfo{
  20.         public int MessageNumber{ get; set; }
  21.         public int Size{ get; set; }
  22.         public override string ToString(){
  23.             return string.Format("[MessageInfo: MessageNumber={0}, Size={1}]", MessageNumber, Size);
  24.         }
  25.     }
  26.     public class POP3Error:Exception{
  27.         public override string Message{ get{return _Message;} }
  28.         private string _Message;
  29.         public POP3Error(string Message){
  30.             _Message = Message;
  31.         }
  32.         public override string ToString(){
  33.             return string.Format("[POP3Error: Message={0}]", Message);
  34.         }
  35.     }
  36.     public class POP3Client{
  37.         private TcpClient tcpClient;
  38.         private TextWriter sender;
  39.         private TextReader receiver;
  40.         private string APOPChallange;
  41.         public POP3Client(){
  42.             APOPChallange = "";
  43.         }
  44.         public void Connect(string Server, int Port = 110, bool UseSSL = false){
  45.             tcpClient = new TcpClient();
  46.             tcpClient.Connect(Server,Port);
  47.             Stream stream = tcpClient.GetStream();
  48.             if(UseSSL){
  49.                 stream = new SslStream(stream);
  50.                 ((SslStream)stream).AuthenticateAsClient(Server);
  51.             }
  52.             sender = new StreamWriter(stream);
  53.             receiver = new StreamReader(stream);
  54.             string Line = receiver.ReadLine();
  55.             if(!Line.ToUpper().StartsWith("+OK")){
  56.                 throw new POP3Error(Line);
  57.             }
  58.             APOPChallange = Regex.Match(Line, "^\\+OK (<[^>]+>)").Groups[1].Value;
  59.         }
  60.         public bool AuthenticateUser(string Username, string Password){
  61.             string Line;
  62.             if(!String.IsNullOrEmpty(APOPChallange)){
  63.                 string APOPResponse = string.Join("", MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(APOPChallange + Password)).Select(b => string.Format("{0:x2}", b)));
  64.                 sender.WriteLine("APOP {0} {1}", Username, APOPResponse);
  65.                 sender.Flush();
  66.                 Line = receiver.ReadLine();
  67.                 if(!Line.ToUpper().StartsWith("+OK")){
  68.                     return false;
  69.                 }
  70.                 return true;
  71.             }
  72.             else{
  73.                 sender.WriteLine("USER {0}", Username);
  74.                 sender.Flush();
  75.                 Line = receiver.ReadLine();
  76.                 if(!Line.ToUpper().StartsWith("+OK")){
  77.                     return false;
  78.                 }
  79.                 sender.WriteLine("PASS {0}", Password);
  80.                 sender.Flush();
  81.                 Line = receiver.ReadLine();
  82.                 if(!Line.ToUpper().StartsWith("+OK")){
  83.                     throw new Exception(Line);
  84.                 }
  85.                 return true;
  86.             }
  87.         }
  88.         public MailboxStatus Status(){
  89.             string Line;
  90.             sender.WriteLine("STAT");
  91.             sender.Flush();
  92.             Line = receiver.ReadLine();
  93.             if(!Line.ToUpper().StartsWith("+OK")){
  94.                 throw new POP3Error(Line);
  95.             }
  96.             Match match = Regex.Match(Line, "^\\+OK[ \t]+([0-9]+)[ \t]+([0-9]+)");
  97.             if(match.Groups.Count == 3){
  98.                 int Messages, Size;
  99.                 if(int.TryParse(match.Groups[1].Value, out Messages) && int.TryParse(match.Groups[2].Value, out Size)){
  100.                     return new MailboxStatus{Messages = Messages, Size = Size};
  101.                 }
  102.                 throw new FormatException("Invalid status response");
  103.             }
  104.             throw new FormatException("Invalid status response");
  105.         }
  106.         public List<MessageInfo> List(int MessageNumber = 0){
  107.             if(MessageNumber <= 0){
  108.                 string Line;
  109.                 sender.WriteLine("LIST");
  110.                 sender.Flush();
  111.                 Line = receiver.ReadLine();
  112.                 if(!Line.ToUpper().StartsWith("+OK")){
  113.                     throw new POP3Error(Line);
  114.                 }
  115.                 List<MessageInfo> Messages = new List<MessageInfo>();
  116.                 while((Line = receiver.ReadLine()) != null){
  117.                     if(Line == "."){
  118.                         break;
  119.                     }
  120.                     Match match = Regex.Match(Line, "^\\+OK[ \t]+([0-9]+)[ \t]+([0-9]+)");
  121.                     if(match.Groups.Count == 3){
  122.                         int Size;
  123.                         if(int.TryParse(match.Groups[1].Value, out MessageNumber) && int.TryParse(match.Groups[2].Value, out Size)){
  124.                             Messages.Add(new MessageInfo{MessageNumber = MessageNumber, Size = Size});
  125.                         }
  126.                         else{
  127.                             throw new FormatException("Invalid list response");
  128.                         }
  129.                     }
  130.                     else{
  131.                         throw new FormatException("Invalid list response");
  132.                     }
  133.                 }
  134.                 return Messages;
  135.             }
  136.             else{
  137.                 string Line;
  138.                 sender.WriteLine("LIST {0}",MessageNumber);
  139.                 sender.Flush();
  140.                 Line = receiver.ReadLine();
  141.                 if(!Line.ToUpper().StartsWith("+OK")){
  142.                     throw new POP3Error(Line);
  143.                 }
  144.                 Match match = Regex.Match(Line, "^\\+OK[ \t]+([0-9]+)[ \t]+([0-9]+)");
  145.                 if(match.Groups.Count == 3){
  146.                     int Size;
  147.                     if(int.TryParse(match.Groups[1].Value, out MessageNumber) && int.TryParse(match.Groups[2].Value, out Size)){
  148.                         return new List<MessageInfo>(new MessageInfo[]{new MessageInfo{MessageNumber = MessageNumber, Size = Size}});
  149.                     }
  150.                     throw new FormatException("Invalid list response");
  151.                 }
  152.                 throw new FormatException("Invalid list response");
  153.             }
  154.         }
  155.         public string Retrieve(int MessageNumber){
  156.             string Line;
  157.             sender.WriteLine("RETR {0}",MessageNumber);
  158.             sender.Flush();
  159.             Line = receiver.ReadLine();
  160.             if(!Line.ToUpper().StartsWith("+OK")){
  161.                 throw new POP3Error(Line);
  162.             }
  163.             string MailContent = "";
  164.             while((Line = receiver.ReadLine())!=null){
  165.                 if(Line == "."){
  166.                     break;
  167.                 }
  168.                 MailContent += Line + Environment.NewLine;
  169.             }
  170.             return MailContent;
  171.         }
  172.         public string Retrieve(int MessageNumber, int Lines){
  173.             string Line;
  174.             sender.WriteLine("TOP {0} {1}",MessageNumber,Lines);
  175.             sender.Flush();
  176.             Line = receiver.ReadLine();
  177.             if(!Line.ToUpper().StartsWith("+OK")){
  178.                 throw new POP3Error(Line);
  179.             }
  180.             string MailContent = "";
  181.             while((Line = receiver.ReadLine())!=null){
  182.                 if(Line == "."){
  183.                     break;
  184.                 }
  185.                 MailContent += Line + Environment.NewLine;
  186.             }
  187.             return MailContent;
  188.         }
  189.         public List<string> Capabilities(){
  190.             string Line;
  191.             sender.WriteLine("CAPA");
  192.             sender.Flush();
  193.             Line = receiver.ReadLine();
  194.             if(!Line.ToUpper().StartsWith("+OK")){
  195.                 throw new POP3Error(Line);
  196.             }
  197.             List<string> capas = new List<string>();
  198.             while((Line = receiver.ReadLine())!=null){
  199.                 if(Line == "."){
  200.                     break;
  201.                 }
  202.                 capas.Add(Line);
  203.             }
  204.             return capas;
  205.         }
  206.         public string UniqueID(int MessageNumber){
  207.             string Line;
  208.             sender.WriteLine("UIDL {0}",MessageNumber);
  209.             sender.Flush();
  210.             Line = receiver.ReadLine();
  211.             if(!Line.ToUpper().StartsWith("+OK")){
  212.                 throw new POP3Error(Line);
  213.             }
  214.             string uidl = Regex.Match(Line, "^\\+OK[ \t][0-9]+[ \t](.+)").Groups[1].Value;
  215.             if(string.IsNullOrEmpty(uidl)){
  216.                 throw new POP3Error("Invalid uidl response");
  217.             }
  218.             return uidl;
  219.         }
  220.         public void Reset(){
  221.             string Line;
  222.             sender.WriteLine("RSET");
  223.             sender.Flush();
  224.             Line = receiver.ReadLine();
  225.             if(!Line.ToUpper().StartsWith("+OK")){
  226.                 throw new POP3Error(Line);
  227.             }
  228.         }
  229.         public void Delete(int MessageNumber){
  230.             string Line;
  231.             sender.WriteLine("DELE {0}", MessageNumber);
  232.             sender.Flush();
  233.             Line = receiver.ReadLine();
  234.             if(!Line.ToUpper().StartsWith("+OK")){
  235.                 throw new POP3Error(Line);
  236.             }
  237.         }
  238.         public void Quit(){
  239.             string Line;
  240.             sender.WriteLine("QUIT");
  241.             sender.Flush();
  242.             Line = receiver.ReadLine();
  243.             if(!Line.ToUpper().StartsWith("+OK")){
  244.                 throw new POP3Error(Line);
  245.             }
  246.             receiver.Close();
  247.             sender.Close();
  248.             tcpClient.Close();
  249.         }
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement