当前位置:编程学习 > C#/ASP.NET >>

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
{

/// <summary>
/// 获取邮件的类
/// zgke@sina.com
/// qq:116149
/// </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;
}

/// <summary>
/// 获取Mail列表
/// </summary>
/// <param name="p_Name">用户名</param>
/// <param name="p_PassWord">密码</param>
/// <returns>Mail信息</returns>
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;
}


/// <summary>
/// 获取邮件内容
/// </summary>
/// <param name="p_Name">名称</param>
/// <param name="p_PassWord">密码</param>
/// <param name="p_MailIndex">邮件编号</param>
/// <returns>数据集</returns>
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;

/// <summary>
/// 获取下一个登陆到获取列表需要的命令
/// </summary>
/// <param name="p_Value"></param>
/// <returns></returns>
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];
}

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

上一个:asp.net下Repeater使用 AspNetPager分页控件
下一个:asp.net 数据库连接池浅析

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,