代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
使用TcpClient 获取邮件列表
作者:
zgke
/ 发布于
2014/7/29
/
462
使用方法 POP3 _Popt = new POP3("192.168.0.1", 110); DataTable _MailTable = _Popt.GetMailTable("zk", "zk");
//使用方法 POP3 _Popt = new POP3("192.168.0.1", 110); DataTable _MailTable = _Popt.GetMailTable("zk", "zk"); using System; using System.Net.Sockets; using System.Net; using System.Security.Cryptography; using System.IO; using System.Data; namespace Zgke.Net { /// <summary> /// 获取邮件的类 /// </summary> public class POP3 { private string m_Address = "127.0.0.1"; private int m_Port = 110; public POP3(string p_Address, int p_Port) { m_Address = p_Address; m_Port = p_Port; } public DataTable GetMailTable(string p_Name,string p_PassWord) { POP3Client _Client = new POP3Client(); _Client.UserName = p_Name; _Client.PassWord = p_PassWord; _Client.Client = new TcpClient(); _Client.Client.BeginConnect(m_Address, m_Port, new AsyncCallback(OnConnectRequest), _Client); while (!_Client.ReturnEnd) { System.Windows.Forms.Application.DoEvents(); } if (_Client.Error.Length != 0) throw new Exception("错误信息!" + _Client.Error); return _Client.MailDataTable; } private class POP3Client { public TcpClient Client; public string UserName = ""; public string PassWord = ""; public bool ReturnEnd = false; public DataTable MailDataTable = new DataTable(); public string Error = ""; public POP3Client() { MailDataTable.Columns.Add("NUM"); MailDataTable.Columns.Add("Size"); MailDataTable.Columns.Add("Form"); MailDataTable.Columns.Add("To"); MailDataTable.Columns.Add("Subject"); MailDataTable.Columns.Add("Date"); } private int m_SendMessage = 0; private int m_TOPIndex = 1; public bool ReadEnd = false; /// <summary> /// 获取下一个登陆到获取列表需要的命令 /// </summary> /// <param name="p_Value"></param> /// <returns></returns> public byte[] GetSendBytes(byte[] p_Value) { ReadEnd = false; string _Value = System.Text.Encoding.ASCII.GetString(p_Value).Replace("/0", ""); if (_Value.IndexOf("+OK") == 0) { m_SendMessage++; switch (m_SendMessage) { case 1: return System.Text.Encoding.ASCII.GetBytes("USER " + UserName + "/r/n"); case 2: return System.Text.Encoding.ASCII.GetBytes("PASS " + PassWord + "/r/n"); case 3: ReadEnd = true; return System.Text.Encoding.ASCII.GetBytes("LIST/r/n"); case 4: string[] _List = _Value.Split(new char[] { '/r', '/n', '.' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 1; i != _List.Length; i++) { string[] _MaliSize = _List[i].Split(' '); MailDataTable.Rows.Add(new object[] { _MaliSize[0], _MaliSize[1] }); } if (MailDataTable.Rows.Count == 0) { ReturnEnd = true; return new byte[0]; } else { ReadEnd = true; m_TOPIndex = 1; return System.Text.Encoding.ASCII.GetBytes("TOP 1/r/n"); } case 5: System.Text.RegularExpressions.Regex _Regex = new System.Text.RegularExpressions.Regex(@"(?<=Date: ).*?(/r/n)+"); System.Text.RegularExpressions.MatchCollection _Collection = _Regex.Matches(_Value); if (_Collection.Count != 0) MailDataTable.Rows[m_TOPIndex - 1]["Date"] = GetReadText(_Collection[0].Value); System.Text.RegularExpressions.Regex _RegexFrom = new System.Text.RegularExpressions.Regex(@"(?<=From: ).*?(/r/n)+"); System.Text.RegularExpressions.MatchCollection _CollectionForm = _RegexFrom.Matches(_Value); if (_CollectionForm.Count != 0) MailDataTable.Rows[m_TOPIndex - 1]["Form"] = GetReadText(_CollectionForm[0].Value); System.Text.RegularExpressions.Regex _RegexTo = new System.Text.RegularExpressions.Regex(@"(?<=To: ).*?(/r/n)+"); System.Text.RegularExpressions.MatchCollection _CollectionTo = _RegexTo.Matches(_Value); if (_CollectionTo.Count != 0) MailDataTable.Rows[m_TOPIndex - 1]["To"] = GetReadText(_CollectionTo[0].Value); System.Text.RegularExpressions.Regex _RegexSubject = new System.Text.RegularExpressions.Regex(@"(?<=Subject: ).*?(/r/n)+"); System.Text.RegularExpressions.MatchCollection _CollectionSubject = _RegexSubject.Matches(_Value); if (_CollectionSubject.Count != 0) MailDataTable.Rows[m_TOPIndex - 1]["Subject"] = GetReadText(_CollectionSubject[0].Value); m_TOPIndex++; m_SendMessage--; ReadEnd = true; if (m_TOPIndex > MailDataTable.Rows.Count) { ReturnEnd = true; return System.Text.Encoding.ASCII.GetBytes("QUIT"); } else { return System.Text.Encoding.ASCII.GetBytes("TOP " + m_TOPIndex.ToString() + "/r/n"); } case 6: break; } } Error = _Value; ReturnEnd = true; return new byte[0]; } /// <summary> /// 转换文字里的字符集 /// </summary> /// <param name="p_Text"></param> /// <returns></returns> public string GetReadText(string p_Text) { System.Text.RegularExpressions.Regex _Regex = new System.Text.RegularExpressions.Regex(@"(?<=/=/?).*?(?=/?/=)+"); System.Text.RegularExpressions.MatchCollection _Collection = _Regex.Matches(p_Text); string _Text = p_Text; foreach (System.Text.RegularExpressions.Match _Match in _Collection) { string _Value = "=?" + _Match.Value + "?="; if (_Value[0] == '=') { string[] _BaseData = _Value.Split('?'); if (_BaseData.Length == 5) { System.Text.Encoding _Coding = System.Text.Encoding.GetEncoding(_BaseData[1]); _Text = _Text.Replace(_Value, _Coding.GetString(Convert.FromBase64String(_BaseData[3]))); } } else { } } return _Text; } } /// <summary> /// 连接事件 /// </summary> /// <param name="ar"></param> private void OnConnectRequest(IAsyncResult ar) { POP3Client _Client = (POP3Client)ar.AsyncState; byte[] _ReadBytes =new byte[0]; _Client.Client.Client.BeginReceive(_ReadBytes, 0, 0, SocketFlags.None, new AsyncCallback(OnWrite), _Client); } /// <summary> /// 连接事件 /// </summary> /// <param name="ar"></param> private void OnSend(IAsyncResult ar) { POP3Client _Client = (POP3Client)ar.AsyncState; byte[] _ReadBytes = new byte[0]; _Client.Client.Client.BeginReceive(_ReadBytes, 0, 0, SocketFlags.None, new AsyncCallback(OnWrite), _Client); } /// <summary> /// 连接事件 /// </summary> /// <param name="ar"></param> private void OnWrite(IAsyncResult ar) { POP3Client _Client = (POP3Client)ar.AsyncState; byte[] _WriteBytes = new byte[_Client.Client.Client.ReceiveBufferSize]; _Client.Client.Client.Receive(_WriteBytes); if (_Client.ReadEnd) _WriteBytes = ReadEnd(_WriteBytes, _Client); byte[] _SendBytes = _Client.GetSendBytes(_WriteBytes); if (_SendBytes.Length == 0) return; _Client.Client.Client.BeginSend(_SendBytes, 0, _SendBytes.Length, SocketFlags.None, new AsyncCallback(OnSend), _Client); } /// <summary> /// 获取知道获取到. 否则一直获取数据 /// </summary> /// <param name="p_Value"></param> /// <returns></returns> private byte[] ReadEnd(byte[] p_Value,POP3Client p_Client) { if (System.Text.Encoding.ASCII.GetString(p_Value).IndexOf("/r/n./r/n") != -1) return p_Value; MemoryStream _Stream = new MemoryStream(); _Stream.Write(p_Value, 0, p_Value.Length); while (true) { byte[] _WriteBytes = new byte[p_Client.Client.ReceiveBufferSize]; p_Client.Client.Client.Receive(_WriteBytes); _Stream.Write(_WriteBytes, 0, _WriteBytes.Length); System.Threading.Thread.Sleep(100); if (System.Text.Encoding.ASCII.GetString(_WriteBytes).IndexOf("/r/n./r/n") != -1) return _Stream.ToArray(); } return _Stream.ToArray(); } } }
试试其它关键字
邮件列表
TcpClient
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
zgke
贡献的其它代码
(
46
)
.
让程序只保证一个进程的类
.
获取PictureBox的SizeMode为Zoom图形的大小
.
获取RICHTEXTBOX所有图形的位置和大小
.
实现系统热键盘注册的类.
.
操作CUR的类
.
读写 Photoshop PSD文件 操作类
.
获取系统鼠标指针
.
隐藏滚动条
.
实现BCD码
.
获取EXCEL所有表名
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3