.NET利用自带的命名空间接收邮件,也就是利用System.Net.Sockets;System.Net;System.IO;等命名空间实现Pop3邮件接收功能,有知道的请附上详细源码。谢谢!
------解决方案--------------------------------------------------------
- C# code
c#接收电子邮件类using System;using System.Net.Sockets;using System.Net;using System.Security.Cryptography;using System.IO; ///////////////////////////////////////////// // 类名:Pop3 // // 功能:接收电子邮件 // /////////////////////////////////////////////namespace ZTSX.Email{/// <summary>/// Pop3 的摘要说明。/// </summary>public class Pop3{ private string mstrHost = null; //主机名称或IP地址 private int mintPort = 110; //主机的端口号(默认为110) private TcpClient mtcpClient = null; //客户端 private NetworkStream mnetStream = null; //网络基础数据流 private StreamReader m_stmReader = null; //读取字节流 private string mstrStatMessage = null; //执行STAT命令后得到的消息(从中得到邮件数) /// <summary> /// 构造函数 /// </summary> /// <remarks>一个邮件接收对象</remarks> public Pop3() { } /// <summary> /// 构造函数 /// </summary> /// <param name="host">主机名称或IP地址</param> public Pop3(string host) { mstrHost = host; } /// <summary> /// 构造函数 /// </summary> /// <param name="host">主机名称或IP地址</param> /// <param name="port">主机的端口号</param> /// <remarks>一个邮件接收对象</remarks> public Pop3(string host,int port) { mstrHost = host; mintPort = port; } #region 属性 /// <summary> /// 主机名称或IP地址 /// </summary> /// <remarks>主机名称或IP地址</remarks> public string HostName { get{return mstrHost;} set{mstrHost = value;} } /// <summary> /// 主机的端口号 /// </summary> /// <remarks>主机的端口号</remarks> public int Port { get{return mintPort;} set{mintPort = value;} } #endregion #region 私有方法 /// <summary> /// 向网络访问的基础数据流中写数据(发送命令码) /// </summary> /// <param name="netStream">可以用于网络访问的基础数据流</param> /// <param name="command">命令行</param> /// <remarks>向网络访问的基础数据流中写数据(发送命令码)</remarks> 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); } /// <summary> /// 检查命令行结果是否正确 /// </summary> /// <param name="message">命令行的执行结果</param> /// <param name="check">正确标志</param> /// <returns> /// 类型:布尔 /// 内容:true表示没有错误,false为有错误 /// </returns> /// <remarks>检查命令行结果是否有错误</remarks> private bool CheckCorrect(string message,string check) { if(message.IndexOf(check) == -1) return false; else return true; } /// <summary> /// 邮箱中的未读邮件数 /// </summary> /// <param name="message">执行完LIST命令后的结果</param> /// <returns> /// 类型:整型 /// 内容:邮箱中的未读邮件数 /// </returns> /// <remarks>邮箱中的未读邮件数</remarks> private int GetMailNumber(string message) { string[] strMessage = message.Split(' '); return Int32.Parse(strMessage[1]); } /// <summary> /// 得到经过解码后的邮件的内容 /// </summary> /// <param name="encodingContent">解码前的邮件的内容</param> /// <returns> /// 类型:字符串 /// 内容:解码后的邮件的内容 /// </returns> /// <remarks>得到解码后的邮件的内容</remarks> 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 /// <summary> /// 与主机建立连接 /// </summary> /// <returns> /// 类型:布尔 /// 内容:连接结果(true为连接成功,false为连接失败) /// </returns> /// <remarks>与主机建立连接</remarks> 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()); } }