Pop3.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:8k
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net.Sockets;
- using System.IO;
- using System.Collections;
- using System.Net.Mail;
- namespace SoketEmail
- {
- public class POP3
- {
- string POPServer;
- string user;
- string pwd;
- NetworkStream ns;
- StreamReader sr;
- public POP3() { }
- public POP3(string server, string _user, string _pwd)
- {
- POPServer = server;
- user = _user;
- pwd = _pwd;
- }
- private bool Connect()
- {
- TcpClient sender = new TcpClient(POPServer, 110);
- byte[] outbytes;
- string input;
- System.Text.Encoding DefaultEncoding = System.Text.Encoding.GetEncoding("gbk");
- try
- {
- ns = sender.GetStream();
- sr = new StreamReader(ns, Encoding.GetEncoding("gbk"));
- sr.ReadLine();
- input = "user " + user + "rn";
- outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
- ns.Write(outbytes, 0, outbytes.Length);
- sr.ReadLine();
- input = "pass " + pwd + "rn";
- outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
- ns.Write(outbytes, 0, outbytes.Length);
- sr.ReadLine();
- return true;
- }
- catch
- {
- return false;
- }
- }
- private void Disconnect()
- {
- string input = "quit" + "rn";
- Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
- ns.Write(outbytes, 0, outbytes.Length);
- ns.Close();
- }
- public int GetNumberOfNewMessages()
- {
- //获得数量
- byte[] outbytes;
- string input;
- try
- {
- Connect();
- input = "stat" + "rn";
- outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
- ns.Write(outbytes, 0, outbytes.Length);
- string resp = sr.ReadLine();
- string[] tokens = resp.Split(new Char[] { ' ' });
- Disconnect();
- return Convert.ToInt32(tokens[1]);
- }
- catch
- {
- return -1;
- }
- }
- public ArrayList GetNewMessages(string subj)
- {
- int newcount;
- ArrayList newmsgs = new ArrayList();
- try
- {
- newcount = GetNumberOfNewMessages();
- Connect();
- for (int n = 1; n < newcount + 1; n++)
- // for (int n = 1; n < 2; n++)
- {
- ArrayList msglines = GetRawMessage(n);
- string msgsubj = GetMessageSubject(msglines);
- if (msgsubj.CompareTo(subj) == 0)
- {
- System.Net.Mail.MailMessage msg = new MailMessage();
- msg.Subject = msgsubj;
- msg.From = new MailAddress(GetMessageFrom(msglines));
- msg.Body = GetMessageBody(msglines);
- newmsgs.Add(msg);
- //DeleteMessage(n);
- }
- }
- Disconnect();
- return newmsgs;
- }
- catch
- {
- return newmsgs;
- }
- }
- public MailMessage GetNewMessages(int nIndex)
- {
- int newcount;
- System.Net.Mail.MailMessage msg = new MailMessage();
- try
- {
- newcount = GetNumberOfNewMessages();
- Connect();
- int n = nIndex + 1;
- if (n < newcount + 1)
- // if(n< 2)
- {
- ArrayList msglines = GetRawMessage(n);
- string msgsubj = GetMessageSubject(msglines);
- msg.Subject = msgsubj;
- msg.From = new MailAddress(GetMessageFrom(msglines));
- msg.Body = GetMessageBody(msglines);
- if (msg.Subject.IndexOf("gbk") != -1)//中文邮件
- {
- msg.Body = ConvertBM(msg.Body);
- }
- }
- Disconnect();
- return msg;
- }
- catch
- {
- return null;
- }
- }
- private string ConvertBM(string strS)
- {
- char[] ch = strS.ToCharArray();
-
- byte[] bt = Convert.FromBase64CharArray(ch, 0, ch.Length);
- System.Text.Encoding gbEncode = System.Text.Encoding.GetEncoding("gbk");
- System.Text.Encoding defaultEncode = System.Text.Encoding.UTF8;
- byte[] defaultBytes = Encoding.Convert(gbEncode, defaultEncode, bt);
- string strDes = defaultEncode.GetString(defaultBytes);
- return strDes;
- }
- private ArrayList GetRawMessage(int messagenumber)
- {
- Byte[] outbytes;
- string input;
- string sTemp = "";
- string line = "";
- input = "retr " + messagenumber.ToString() + "rn";
- outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
- ns.Write(outbytes, 0, outbytes.Length);
- ArrayList msglines = new ArrayList();
- do
- {
- line = sr.ReadLine();
- System.Text.Encoding gbEncode = System.Text.Encoding.GetEncoding("gbk");
- System.Text.Encoding defaultEncode = System.Text.Encoding.ASCII;
- byte[] defaultBytes = defaultEncode.GetBytes(line);
- byte[] gbBytes = Encoding.Convert(defaultEncode, gbEncode, defaultBytes);
- line = gbEncode.GetString(gbBytes);
- sTemp += line;
- msglines.Add(line);
- } while (line != ".");
- msglines.RemoveAt(msglines.Count - 1);
- return msglines;
- }
- private string GetMessageSubject(ArrayList msglines)
- {
- string[] tokens;
- IEnumerator msgenum = msglines.GetEnumerator();
- while (msgenum.MoveNext())
- {
- string line = ((string)msgenum.Current).ToLower();
- if (line.StartsWith("subject:"))
- {
- tokens = line.Split(new Char[] { ' ' });
- return tokens[1].Trim();
- }
- }
- return "None";
- }
- private string GetMessageFrom(ArrayList msglines)
- {
- string[] tokens;
- IEnumerator msgenum = msglines.GetEnumerator();
- while (msgenum.MoveNext())
- {
- string line = ((string)msgenum.Current).ToLower();
- if (line.StartsWith("from:"))
- {
- tokens = line.Split(new Char[] { ' ' });
- return tokens[2].Trim();
- }
- }
- return "None";
- }
- private string GetMessageBody(ArrayList msglines)
- {
- string body = "";
- string line = " ";
- IEnumerator msgenum = msglines.GetEnumerator();
- while (line.CompareTo("") != 0)
- {
- msgenum.MoveNext();
- line = (string)msgenum.Current;
- }
- while (msgenum.MoveNext())
- {
- body = body + (string)msgenum.Current + "rn";
- }
- return body;
- }
- private void DeleteMessage(int messagenumber)
- {
- Byte[] outbytes;
- string input;
- try
- {
- input = "dele " + messagenumber.ToString() + "rn";
- outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
- ns.Write(outbytes, 0, outbytes.Length);
- }
- catch
- {
- return;
- }
- }
- }
- }