c#接收邮件类
来源:互联网 发布:gif动画制作软件 编辑:程序博客网 时间:2024/05/21 19:48
- using System;
- using System.Net.Sockets;
- using System.Net;
- using System.Security.Cryptography;
- using System.IO;
- namespace ZTSX.Email
- {
- public class Pop3
- {
- private string mstrHost = null;
- private int mintPort = 110;
- private TcpClient mtcpClient = null;
- private NetworkStream mnetStream = null;
- private StreamReader m_stmReader = null;
- private string mstrStatMessage = null;
- public Pop3()
- {
- }
- public Pop3(string host)
- {
- mstrHost = host;
- }
- public Pop3(string host,int port)
- {
- mstrHost = host;
- mintPort = port;
- }
- #region 属性
- public string HostName
- {
- get{return mstrHost;}
- set{mstrHost = value;}
- }
- public int Port
- {
- get{return mintPort;}
- set{mintPort = value;}
- }
- #endregion
- #region 私有方法
- private void WriteToNetStream(ref NetworkStream netStream,String command)
- {
- string strToSend = command + "/r/n";
- byte[] arrayToSend = System.Text.Encoding.ASCII.GetBytes(strToSend.ToCharArray());
- netStream.Write(arrayToSend,0,arrayToSend.Length);
- }
- private bool CheckCorrect(string message,string check)
- {
- if(message.IndexOf(check) == -1)
- return false;
- else
- return true;
- }
- private int GetMailNumber(string message)
- {
- string[] strMessage = message.Split(' ');
- return Int32.Parse(strMessage[1]);
- }
- private string GetDecodeMailContent(string encodingContent)
- {
- string strContent = encodingContent.Trim();
- string strEncode = null;
- int iStart = strContent.IndexOf("Base64");
- if(iStart == -1)
- throw new Pop3Exception("邮件内容不是Base64编码,请检查");
- else
- {
- strEncode = strContent.Substring(iStart + 6,strContent.Length - iStart - 6);
- try
- {
- return SX.Encode.TransformToString(strEncode);
- }
- catch(SX.EncodeException exc)
- {
- throw new Pop3Exception(exc.Message);
- }
- }
- }
- #endregion
- public bool Connect()
- {
- if(mstrHost == null)
- throw new Exception("请提供SMTP主机名称或IP地址!");
- if(mintPort == 0)
- throw new Exception("请提供SMTP主机的端口号");
- try
- {
- mtcpClient = new TcpClient(mstrHost,mintPort);
- mnetStream = mtcpClient.GetStream();
- m_stmReader = new StreamReader(mtcpClient.GetStream());
- string strMessage = m_stmReader.ReadLine();
- if(CheckCorrect(strMessage,"+OK") == true)
- return true;
- else
- return false;
- }
- catch(SocketException exc)
- {
- throw new Pop3Exception(exc.ToString());
- }
- catch(NullReferenceException exc)
- {
- throw new Pop3Exception(exc.ToString());
- }
- }
- #region Pop3命令
- private string ExecuteCommand(string command)
- {
- string strMessage = null;
- try
- {
-
- WriteToNetStream(ref mnetStream,command);
-
- if(command.Substring(0,4).Equals("LIST") || command.Substring(0,4).Equals("RETR") || command.Substring(0,4).Equals("UIDL"))
- {
- strMessage = ReadMultiLine();
- if(command.Equals("LIST"))
- mstrStatMessage = strMessage;
- }
-
- else
- strMessage = m_stmReader.ReadLine();
-
- if(CheckCorrect(strMessage,"+OK"))
- return strMessage;
- else
- return "Error";
- }
- catch(IOException exc)
- {
- throw new Pop3Exception(exc.ToString());
- }
- }
- private string ReadMultiLine()
- {
- string strMessage = m_stmReader.ReadLine();
- string strTemp = null;
- while(strMessage != ".")
- {
- strTemp = strTemp + strMessage;
- strMessage = m_stmReader.ReadLine();
- }
- return strTemp;
- }
- private string USER(string user)
- {
- return ExecuteCommand("USER " + user) + "/r/n";
- }
- private string PASS(string password)
- {
- return ExecuteCommand("PASS " + password) + "/r/n";
- }
- private string LIST()
- {
- return ExecuteCommand("LIST") + "/r/n";
- }
- private string UIDL()
- {
- return ExecuteCommand("UIDL") + "/r/n";
- }
- private string NOOP()
- {
- return ExecuteCommand("NOOP") + "/r/n";
- }
- private string STAT()
- {
- return ExecuteCommand("STAT") + "/r/n";
- }
- private string RETR(int number)
- {
- return ExecuteCommand("RETR " + number.ToString()) + "/r/n";
- }
- private string DELE(int number)
- {
- return ExecuteCommand("DELE " + number.ToString()) + "/r/n";
- }
- private void Quit()
- {
- WriteToNetStream(ref mnetStream,"QUIT");
- }
- private string[] ReceiveMail(string user,string password)
- {
- int iMailNumber = 0;
- if(USER(user).Equals("Error"))
- throw new Pop3Exception("用户名不正确!");
- if(PASS(password).Equals("Error"))
- throw new Pop3Exception("用户口令不正确!");
- if(STAT().Equals("Error"))
- throw new Pop3Exception("准备接收邮件时发生错误!");
- if(LIST().Equals("Error"))
- throw new Pop3Exception("得到邮件列表时发生错误!");
- try
- {
- iMailNumber = GetMailNumber(mstrStatMessage);
-
- if(iMailNumber == 0)
- return null;
- else
- {
- string[] strMailContent = new string[iMailNumber];
- for(int i = 1 ; i <= iMailNumber ; i++)
- {
-
- strMailContent[i - 1] = GetDecodeMailContent(RETR(i));
- }
- return strMailContent;
- }
- }
- catch(Pop3Exception exc)
- {
- throw new Pop3Exception(exc.ToString());
- }
- }
- #endregion
- public string[] Receive(string user,string password)
- {
- try
- {
- return ReceiveMail(user,password);
- }
- catch(Pop3Exception exc)
- {
- throw new Pop3Exception(exc.ToString());
- }
- }
- public void DisConnect()
- {
- try
- {
- Quit();
- if(m_stmReader != null)
- m_stmReader.Close();
- if(mnetStream != null)
- mnetStream.Close();
- if(mtcpClient != null)
- mtcpClient.Close();
- }
- catch(SocketException exc)
- {
- throw new Pop3Exception(exc.ToString());
- }
- }
- public void DeleteMail(int number)
- {
-
- int iMailNumber = number + 1;
- if(DELE(iMailNumber).Equals("Error"))
- throw new Pop3Exception("删除第" + iMailNumber.ToString() + "时出现错误!");
- }
- }
- }