Pop3.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:8k
源码类别:

Email服务器

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net.Sockets;
  5. using System.IO;
  6. using System.Collections;
  7. using System.Net.Mail;
  8. namespace SoketEmail
  9. {
  10.     public class POP3
  11.     {
  12.         string POPServer;
  13.         string user;
  14.         string pwd;
  15.         NetworkStream ns;
  16.         StreamReader sr;   
  17.         public POP3() { }
  18.         public POP3(string server, string _user, string _pwd)
  19.         {
  20.             POPServer = server;
  21.             user = _user;
  22.             pwd = _pwd;
  23.         }
  24.         private bool Connect()
  25.         {
  26.             TcpClient sender = new TcpClient(POPServer, 110);
  27.             byte[] outbytes;
  28.             string input;
  29.             System.Text.Encoding DefaultEncoding = System.Text.Encoding.GetEncoding("gbk");
  30.             try
  31.             {
  32.                 ns = sender.GetStream();
  33.                 sr = new StreamReader(ns, Encoding.GetEncoding("gbk"));
  34.                 sr.ReadLine();
  35.                 input = "user " + user + "rn";
  36.                 outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
  37.                 ns.Write(outbytes, 0, outbytes.Length);
  38.                 sr.ReadLine();
  39.                 input = "pass " + pwd + "rn";
  40.                 outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
  41.                 ns.Write(outbytes, 0, outbytes.Length);
  42.                 sr.ReadLine();
  43.                 return true;
  44.             }
  45.             catch
  46.             {
  47.                 return false;
  48.             }
  49.         }
  50.         private void Disconnect()
  51.         {
  52.             string input = "quit" + "rn";
  53.             Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
  54.             ns.Write(outbytes, 0, outbytes.Length);
  55.             ns.Close();
  56.         }
  57.         public int GetNumberOfNewMessages()
  58.         {
  59.             //获得数量
  60.             byte[] outbytes;
  61.             string input;
  62.             try
  63.             {
  64.                 Connect();
  65.                 input = "stat" + "rn";
  66.                 outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
  67.                 ns.Write(outbytes, 0, outbytes.Length);
  68.                 string resp = sr.ReadLine();
  69.                 string[] tokens = resp.Split(new Char[] { ' ' });
  70.                 Disconnect();
  71.                 return Convert.ToInt32(tokens[1]);
  72.             }
  73.             catch
  74.             {
  75.                 return -1;
  76.             }
  77.         }
  78.         public ArrayList GetNewMessages(string subj)
  79.         {
  80.             int newcount;
  81.             ArrayList newmsgs = new ArrayList();
  82.             try
  83.             {
  84.                 newcount = GetNumberOfNewMessages();
  85.                 Connect();
  86.                for (int n = 1; n < newcount + 1; n++)
  87.                // for (int n = 1; n < 2; n++)
  88.                 {
  89.                     ArrayList msglines = GetRawMessage(n);
  90.                     string msgsubj = GetMessageSubject(msglines);
  91.                     if (msgsubj.CompareTo(subj) == 0)
  92.                     {
  93.                         System.Net.Mail.MailMessage msg = new MailMessage();
  94.                         msg.Subject = msgsubj;
  95.                         msg.From = new MailAddress(GetMessageFrom(msglines));
  96.                         msg.Body = GetMessageBody(msglines);
  97.                         newmsgs.Add(msg);
  98.                       //DeleteMessage(n);
  99.                     }
  100.                 }
  101.                 Disconnect();
  102.                 return newmsgs;
  103.             }
  104.             catch
  105.             {
  106.                 return newmsgs;
  107.             }
  108.         }
  109.         public MailMessage GetNewMessages(int nIndex)
  110.         {
  111.             int newcount;
  112.             System.Net.Mail.MailMessage msg = new MailMessage();
  113.             try
  114.             {
  115.                 newcount = GetNumberOfNewMessages();
  116.                 Connect();
  117.                 int n = nIndex + 1;
  118.                if (n < newcount + 1)
  119.                // if(n< 2)
  120.                 {
  121.                     ArrayList msglines = GetRawMessage(n);
  122.                     string msgsubj = GetMessageSubject(msglines);
  123.                     msg.Subject = msgsubj;
  124.                     msg.From = new MailAddress(GetMessageFrom(msglines));
  125.                     msg.Body = GetMessageBody(msglines);
  126.                     if (msg.Subject.IndexOf("gbk") != -1)//中文邮件
  127.                     {
  128.                         msg.Body = ConvertBM(msg.Body);
  129.                     }
  130.                 }
  131.                 Disconnect();
  132.                 return msg;
  133.             }
  134.             catch
  135.             {
  136.                 return null;
  137.             }
  138.         }
  139.         private string ConvertBM(string strS)
  140.         {
  141.             char[] ch = strS.ToCharArray();
  142.             
  143.             byte[] bt = Convert.FromBase64CharArray(ch, 0, ch.Length);
  144.             System.Text.Encoding gbEncode = System.Text.Encoding.GetEncoding("gbk");
  145.             System.Text.Encoding defaultEncode = System.Text.Encoding.UTF8;
  146.             byte[] defaultBytes = Encoding.Convert(gbEncode, defaultEncode, bt);
  147.             string strDes = defaultEncode.GetString(defaultBytes);
  148.             return strDes;
  149.         }
  150.         private ArrayList GetRawMessage(int messagenumber)
  151.         {
  152.             Byte[] outbytes;
  153.             string input;
  154.             string sTemp = "";
  155.             string line = "";
  156.             input = "retr " + messagenumber.ToString() + "rn";
  157.             outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
  158.             ns.Write(outbytes, 0, outbytes.Length);
  159.             ArrayList msglines = new ArrayList();
  160.             do
  161.             {
  162.                 line = sr.ReadLine();
  163.                 System.Text.Encoding gbEncode = System.Text.Encoding.GetEncoding("gbk");
  164.                 System.Text.Encoding defaultEncode = System.Text.Encoding.ASCII;
  165.                 byte[] defaultBytes = defaultEncode.GetBytes(line);
  166.                 byte[] gbBytes = Encoding.Convert(defaultEncode, gbEncode, defaultBytes);
  167.                 line = gbEncode.GetString(gbBytes);
  168.                 sTemp += line;
  169.                 msglines.Add(line);
  170.             } while (line != ".");
  171.             msglines.RemoveAt(msglines.Count - 1);
  172.             return msglines;
  173.         }
  174.         private string GetMessageSubject(ArrayList msglines)
  175.         {
  176.             string[] tokens;
  177.             IEnumerator msgenum = msglines.GetEnumerator();
  178.             while (msgenum.MoveNext())
  179.             {
  180.                 string line = ((string)msgenum.Current).ToLower();
  181.                 if (line.StartsWith("subject:"))
  182.                 {
  183.                     tokens = line.Split(new Char[] { ' ' });
  184.                     return tokens[1].Trim();
  185.                 }
  186.             }
  187.             return "None";
  188.         }
  189.         private string GetMessageFrom(ArrayList msglines)
  190.         {
  191.             string[] tokens;
  192.             IEnumerator msgenum = msglines.GetEnumerator();
  193.             while (msgenum.MoveNext())
  194.             {
  195.                 string line = ((string)msgenum.Current).ToLower();
  196.                 if (line.StartsWith("from:"))
  197.                 {
  198.                     tokens = line.Split(new Char[] { ' ' });
  199.                     return tokens[2].Trim();
  200.                 }
  201.             }
  202.             return "None";
  203.         }
  204.         private string GetMessageBody(ArrayList msglines)
  205.         {
  206.             string body = "";
  207.             string line = " ";
  208.             IEnumerator msgenum = msglines.GetEnumerator();
  209.             while (line.CompareTo("") != 0)
  210.             {
  211.                 msgenum.MoveNext();
  212.                 line = (string)msgenum.Current;
  213.             }
  214.             while (msgenum.MoveNext())
  215.             {
  216.                 body = body + (string)msgenum.Current + "rn";
  217.             }
  218.             return body;
  219.         }
  220.         private void DeleteMessage(int messagenumber)
  221.         {
  222.             Byte[] outbytes;
  223.             string input;
  224.             try
  225.             {
  226.                 input = "dele " + messagenumber.ToString() + "rn";
  227.                 outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
  228.                 ns.Write(outbytes, 0, outbytes.Length);
  229.             }
  230.             catch
  231.             {
  232.                 return;
  233.             }
  234.         }
  235.     }
  236. }