热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

C#通过POP3获取邮件的代码(正文和附件)

C#POP3获取邮件的代码包括正文和附件,需要的朋友可以参考下。
使用方法:
获取第1封邮件
代码如下:

Zgke.Net.POP3 _Popt = new Zgke.Net.POP3("192.168.0.1", 110);
DataTable _Mail = _Popt.GetMail("zk", "zk", 1);

返回DataTable 数据内容为
Type为类型 Text为文字 如果是附件 为byte[] Name 如果是附件里存放的为文件名
代码如下:

using System;
using System.Net.Sockets;
using System.Net;
using System.Security.Cryptography;
using System.IO;
using System.Data;

namespace Zgke.Net
{

///
/// 获取邮件的类
/// zgke@sina.com
/// qq:116149
///

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;
}

///
/// 获取Mail列表
///

/// 用户名
/// 密码
/// Mail信息
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;
}


///
/// 获取邮件内容
///

/// 名称
/// 密码
/// 邮件编号
/// 数据集
public DataTable GetMail(string p_Name, string p_PassWord, int p_MailIndex)
{
POP3Client _Client = new POP3Client();
_Client.UserName = p_Name;
_Client.PassWord = p_PassWord;
_Client.Client = new TcpClient();
_Client.ReadIndex = p_MailIndex;
_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.MailTable;
}

private class POP3Client
{
public TcpClient Client;

public string UserName = "";

public string PassWord = "";

public bool ReturnEnd = false;

public DataTable MailDataTable = new DataTable();

public DataTable MailTable = new DataTable();

public string Error = "";

public bool ReadEnd = false;

public int ReadIndex = -1;


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");

MailTable.Columns.Add("Type",typeof(string));
MailTable.Columns.Add("Text",typeof(object));
MailTable.Columns.Add("Name", typeof(string));
}

private int m_SendMessage = 0;
private int m_TOPIndex = 1;

///
/// 获取下一个登陆到获取列表需要的命令
///

///
///
public byte[] GetSendBytes(byte[] p_Value)
{
ReadEnd = false;
string _Value = System.Text.Encoding.Default.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;
if (ReadIndex != -1)
{
m_SendMessage = 5;
return System.Text.Encoding.ASCII.GetBytes("RETR " + ReadIndex.ToString() + "\r\n");
}
else
{
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:
GetMailText(_Value);
ReturnEnd = true;
return System.Text.Encoding.ASCII.GetBytes("QUIT");

}
}
Error = _Value;
ReturnEnd = true;
return new byte[0];
}

///
/// 转换文字里的字符集
///

///
///
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;
}


#region 获取邮件正文 和 附件
///
/// 获取文字主体
///

///
///
public void GetMailText(string p_Mail)
{
string _COnvertType= GetTextType(p_Mail, "\r\nContent-Type: ", ";");
if (_ConvertType.Length == 0)
{
_COnvertType= GetTextType(p_Mail, "\r\nContent-Type: ", "\r");
}
int _StarIndex = -1;
int _EndIndex = -1;
string _ReturnText = "";
string _Transfer = "";
string _Boundary = "";
string _EncodingName = GetTextType(p_Mail, "charset=\"", "\"").Replace("\"", "");
System.Text.Encoding _Encoding = System.Text.Encoding.Default;
if(_EncodingName!="")_Encoding = System.Text.Encoding.GetEncoding(_EncodingName);
switch (_ConvertType)
{
case "text/html;":
_Transfer = GetTextType(p_Mail, "\r\nContent-Transfer-Encoding: ", "\r\n").Trim();
_StarIndex = p_Mail.IndexOf("\r\n\r\n");
if (_StarIndex != -1) _ReturnText = p_Mail.Substring(_StarIndex, p_Mail.Length - _StarIndex);
switch (_Transfer)
{
case "8bit":

break;
case "quoted-printable":
_ReturnText = DecodeQuotedPrintable(_ReturnText, _Encoding);
break;
case "base64":
_ReturnText = DecodeBase64(_ReturnText, _Encoding);
break;
}
MailTable.Rows.Add(new object[] { "text/html", _ReturnText });
break;
case "text/plain;":
_Transfer = GetTextType(p_Mail, "\r\nContent-Transfer-Encoding: ", "\r\n").Trim();
_StarIndex = p_Mail.IndexOf("\r\n\r\n");
if (_StarIndex != -1) _ReturnText = p_Mail.Substring(_StarIndex, p_Mail.Length - _StarIndex);
switch (_Transfer)
{
case "8bit":

break;
case "quoted-printable":
_ReturnText = DecodeQuotedPrintable(_ReturnText, _Encoding);
break;
case "base64":
_ReturnText = DecodeBase64(_ReturnText, _Encoding);
break;
}
MailTable.Rows.Add(new object[] { "text/plain", _ReturnText });
break;
case "multipart/alternative;":
_Boundary = GetTextType(p_Mail, "boundary=\"", "\"").Replace("\"", "");
_StarIndex = p_Mail.IndexOf("--" + _Boundary + "\r\n");
if (_StarIndex == -1) return;
while (true)
{
_EndIndex = p_Mail.IndexOf("--" + _Boundary, _StarIndex + _Boundary.Length);
if (_EndIndex == -1) break;
GetMailText(p_Mail.Substring(_StarIndex, _EndIndex - _StarIndex));
_StarIndex = _EndIndex;
}
break;
case "multipart/mixed;":
_Boundary = GetTextType(p_Mail, "boundary=\"", "\"").Replace("\"", "");
_StarIndex = p_Mail.IndexOf("--" + _Boundary + "\r\n");
if (_StarIndex == -1) return;
while (true)
{
_EndIndex = p_Mail.IndexOf("--" + _Boundary, _StarIndex + _Boundary.Length);
if (_EndIndex == -1) break;
GetMailText(p_Mail.Substring(_StarIndex, _EndIndex - _StarIndex));
_StarIndex = _EndIndex;
}
break;
default:
if (_ConvertType.IndexOf("application/") == 0)
{
_StarIndex = p_Mail.IndexOf("\r\n\r\n");
if (_StarIndex != -1) _ReturnText = p_Mail.Substring(_StarIndex, p_Mail.Length - _StarIndex);
_Transfer = GetTextType(p_Mail, "\r\nContent-Transfer-Encoding: ", "\r\n").Trim();
string _Name = GetTextType(p_Mail, "filename=\"", "\"").Replace("\"", "");
_Name = GetReadText(_Name);
byte[] _FileBytes = new byte[0];
switch (_Transfer)
{
case "base64":
_FileBytes = Convert.FromBase64String(_ReturnText);
break;
}
MailTable.Rows.Add(new object[] { "application/octet-stream", _FileBytes, _Name });

}
break;
}
}

///
/// 获取类型(正则)
///

/// 原始文字
/// 前文字
/// 结束文字
/// 符合的记录
public string GetTextType(string p_Mail, string p_TypeText, string p_End)
{
System.Text.RegularExpressions.Regex _Regex = new System.Text.RegularExpressions.Regex(@"(?<=" + p_TypeText + ").*?(" + p_End + ")+");
System.Text.RegularExpressions.MatchCollection _Collection = _Regex.Matches(p_Mail);
if (_Collection.Count == 0) return "";
return _Collection[0].Value;
}

///
/// QuotedPrintable编码接码
///

/// 原始文字
/// 编码方式
/// 接码后信息
public string DecodeQuotedPrintable(string p_Text, System.Text.Encoding p_Encoding)
{
System.IO.MemoryStream _Stream = new System.IO.MemoryStream();
char[] _CharValue = p_Text.ToCharArray();
for (int i = 0; i != _CharValue.Length; i++)
{
switch (_CharValue[i])
{
case '=':
if (_CharValue[i + 1] == '\r' || _CharValue[i + 1] == '\n')
{
i += 2;
}
else
{
try
{
_Stream.WriteByte(Convert.ToByte(_CharValue[i + 1].ToString() + _CharValue[i + 2].ToString(), 16));
i += 2;
}
catch
{
_Stream.WriteByte(Convert.ToByte(_CharValue[i]));
}
}
break;
default:
_Stream.WriteByte(Convert.ToByte(_CharValue[i]));
break;
}
}
return p_Encoding.GetString(_Stream.ToArray());
}

///
/// 解码BASE64
///

///
///
///
public string DecodeBase64(string p_Text, System.Text.Encoding p_Encoding)
{
if (p_Text.Trim().Length == 0) return "";
byte[] _ValueBytes = Convert.FromBase64String(p_Text);
return p_Encoding.GetString(_ValueBytes);
}
#endregion


}

///
/// 连接事件
///

///
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);
}

///
/// 连接事件
///

///
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);
}

///
/// 连接事件
///

///
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);
}

///
/// 获取知道获取到. 否则一直获取数据
///

///
///
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();
}
}

}
}

推荐阅读
  • 本文介绍了两种获取和研究 .NET Framework 源代码的有效途径:一是通过官方提供的下载链接获取完整源代码,并使用 Visual Studio 进行本地查看;二是利用在线资源平台,直接在网页上浏览源代码。 ... [详细]
  • 本文介绍了如何使用Gradle和gdx-setup.jar工具来创建LibGDX项目,包括详细的步骤和注意事项,适合初学者和有经验的开发者。 ... [详细]
  • MyEclipse技巧:高效生成toString方法
    本文将介绍如何在MyEclipse中快速且高效地生成toString方法,帮助开发者简化编码过程,提高开发效率。 ... [详细]
  • 本文介绍了如何使用命令行在 Windows 系统中启动或关闭 VMWare 的关键服务,包括 VMwareHostd、VMAuthdService、VMUSBArbService、VMware NAT Service 和 VMnetDHCP。 ... [详细]
  • 本文介绍并分享了三个个人开源项目,涵盖单元测试中HttpContext的可测试性增强、Visual Studio插件开发以及单元测试报告自动生成工具。 ... [详细]
  • 主板市盈率、市净率及股息率的自动化抓取
    本文介绍了如何通过Python脚本自动从中国指数有限公司网站抓取主板的市盈率、市净率和股息率等关键财务指标,并将这些数据存储到CSV文件中。涉及的技术包括网页解析、正则表达式以及异常处理。 ... [详细]
  • 解决ASP.NET Core在IIS中出现的502.5进程失败错误
    本文详细探讨了在Windows Server 2012环境下安装.NET Core后,IIS站点出现502.5错误的原因及解决方案,包括重启服务和系统的方法。 ... [详细]
  • 本文详细介绍了在Delphi环境中对DLL文件进行断点调试的方法,包括设置依赖的可执行文件、编译器和链接器的调试选项,以及运行时参数的配置。 ... [详细]
  • HTML网页出现乱码的主要成因及解决策略
    本文深入分析了HTML网页出现乱码的各种可能原因,并提供了相应的解决方案,帮助开发者有效避免和处理此类问题。 ... [详细]
  • 本文详细探讨了C++中赋值运算符重载函数(operator=)的使用方法和注意事项,结合实例分析了其参数、返回值、调用时机等关键点,并讨论了浅拷贝和深拷贝的区别及其重要性。 ... [详细]
  • 本文将详细介绍NSRunLoop的工作原理,包括其基本概念、消息类型(事件源)、运行模式、生命周期管理以及嵌套运行等关键知识点,帮助开发者更好地理解和应用这一重要技术。 ... [详细]
  • 本文介绍如何在Django项目中利用UpdateView更新数据后,根据主键(pk)自动重定向至对应的DetailView页面,实现流畅的用户交互体验。 ... [详细]
  • Python 中使用 Pyecharts 绘制雷达图详解
    本文将详细介绍如何在 Python 环境中利用 Pyecharts 库来创建美观且功能丰富的雷达图。适合需要图形化展示多维度数据的开发者和研究人员。 ... [详细]
  • 本文探讨了如何通过调整页面的文档模式设置,解决JSP Grid在Quirks模式下无法正常使用的常见问题。 ... [详细]
  • 本文探讨了在渗透测试中信息收集阶段使用的几种端口扫描技术,包括nmap、masscan、socket、telnet及nc等工具的应用与比较。 ... [详细]
author-avatar
古镇飞天
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有