求“收取” 邮件代码 试过的来 谢谢。代码发全了啊。看好是收取. 发送的代码不要了。不要老给我发链接 看了烦。100分不够 和我讲,我只能发这么多分了。 QQ444201894可以单聊。很救命啊。邮箱 [email protected]. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Jmail收取邮件 protected void Button1_Click(object sender, EventArgs e) { jmail.POP3Class popMail = new jmail.POP3Class(); //建立接收邮件对象 jmail.Message mailMsg; //建立邮件信息接口 jmail.Attachments atts; //建立附件集接口 jmail.Attachment att; //建立附件接口 try { popMail.Connect(TextBox3.Text, TextBox4.Text, TextBox1.Text,Convert.ToInt16(TextBox2.Text)); if (popMail.Count <= 0) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("sender", typeof(string))); dt.Columns.Add(new DataColumn("senderemail", typeof(string))); dt.Columns.Add(new DataColumn("subject", typeof(string))); dt.Columns.Add(new DataColumn("priority", typeof(string))); dt.Columns.Add(new DataColumn("mailsize", typeof(string))); dt.Columns.Add(new DataColumn("attname", typeof(string))); dt.Columns.Add(new DataColumn("mailbody", typeof(string))); DataRow dr = dt.NewRow(); for (int i = 1; i <= popMail.Count; i++) { mailMsg = popMail.Messages[i]; //取得一条邮件信息 atts = mailMsg.Attachments; mailMsg.Charset = "GB2312"; mailMsg.Encoding = "Base64"; mailMsg.ISOEncodeHeaders = false; dr[0] = mailMsg.FromName; dr[1] = mailMsg.From; dr[2] = mailMsg.Subject; dr[3] = mailMsg.Priority.ToString(); dr[4] = mailMsg.Size.ToString(); att = atts[0]; dr[5] = att.Name; dr[6] = mailMsg.Body; dt.Rows.Add(dr); } GridView1.DataSource = dt; GridView1.DataBind(); att = null; atts = null; } else { Response.Write("没有新邮件"); } } catch (Exception ex) { Response.Write("错误!请检查邮件服务器的设置是否正确"); } } 不用JMAIL发的 ,谁能给个嘛. using System.Net;using System.Net.Sockets;using System.IO;using System.Collections;using System.Net.Mail;/// <summary>/// 获取邮件信息/// </summary>/// <param name="subj"></param>/// <returns></returns> public ArrayList GetNewMessages(string subj) { int newcount; ArrayList newmsgs = new ArrayList(); try { newcount = GetNumberOfNewMessages(); Connect(); for(int n=1; n<newcount+1; n++) { ArrayList msglines = GetRawMessage(n); string msgsubj = GetMessageSubject(msglines); if(msgsubj.CompareTo(subj) == 0) { System.Net.Mail.MailMessage msg = new MailMessage(); msg.Subject = msgsubj; msg.From =new MailAddress(GetMessageFrom(msglines)); msg.Body = GetMessageBody(msglines); newmsgs.Add(msg); DeleteMessage(n); } } Disconnect(); return newmsgs; } catch(Exception e) { return newmsgs; } } /**//// <summary> /// 获取新邮件 /// </summary> /// <param name="nIndex"></param> /// <returns></returns> public MailMessage GetNewMessages(int nIndex) { int newcount; System.Net.Mail.MailMessage msg = new MailMessage(); try { newcount = GetNumberOfNewMessages(); Connect(); int n = nIndex+1; if(n<newcount+1) { ArrayList msglines = GetRawMessage(n); string msgsubj = GetMessageSubject(msglines); msg.Subject = msgsubj; msg.From = new MailAddress(GetMessageFrom(msglines)); msg.Body = GetMessageBody(msglines); } Disconnect(); return msg; } catch { return null; } } private ArrayList GetRawMessage (int messagenumber) { Byte[] outbytes; string input; string line = ""; input = "retr " + messagenumber.ToString() + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes,0,outbytes.Length); ArrayList msglines = new ArrayList(); do { line = sr.ReadLine(); msglines.Add(line); } while (line != "."); msglines.RemoveAt(msglines.Count-1); return msglines; } 要 web 的 最好配置邮件 服务器也 说下. /// 连接服务器 private bool Connect() { TcpClient sender = new TcpClient(POPServer, port); byte[] outbytes; string input; try { ns = sender.GetStream(); sr = new StreamReader(ns); sr.ReadLine(); input = "user " + user + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); sr.ReadLine(); input = "pass " + pwd + "\r\n"; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); sr.ReadLine(); return true; } catch { return false; } } 接受的邮件的,没有包括分析邮string strServerName = "";//邮件服务器名 string strUserName = "";//用户名 string strPassWord = ""; //密码 StringBuilder strBuildeMailMessage = new StringBuilder(); int numberOfBytesRead = 0; int length = 1024 * 10; byte[] buttfer = new byte[length]; TcpClient tp = new TcpClient(strServerName, 110); tp.ReceiveTimeout = 1000 * 300; NetworkStream ns = tp.GetStream(); ns.ReadTimeout = 1000 * 300; ns.Read(buttfer, 0, buttfer.Length); byte[] outbytes = System.Text.Encoding.ASCII.GetBytes("user " + strUserName + "\r\n"); ns.Write(outbytes, 0, outbytes.Length); numberOfBytesRead = ns.Read(buttfer, 0, buttfer.Length); strBuildeMailMessage.Append(System.Text.Encoding.ASCII.GetString(buttfer, 0, numberOfBytesRead)); strBuildeMailMessage = new StringBuilder(); outbytes = System.Text.Encoding.ASCII.GetBytes("pass " + strPassWord + "\r\n"); ns.Write(outbytes, 0, outbytes.Length); numberOfBytesRead = ns.Read(buttfer, 0, buttfer.Length); strBuildeMailMessage.Append(System.Text.Encoding.ASCII.GetString(buttfer, 0, numberOfBytesRead)); Regex re = new Regex(@"(?<=has\s+)[\d]+(?=\s+messages)"); int num = int.Parse(re.Match(strBuildeMailMessage.ToString()).Value.Trim()); List<string> emailList = new List<string>(); for (int i = 1; i <= num; i++) { strBuildeMailMessage = new StringBuilder(); outbytes = System.Text.Encoding.ASCII.GetBytes("retr " + i.ToString() + "\r\n"); ns.Write(outbytes, 0, outbytes.Length); Thread.Sleep(800); while (ns.DataAvailable) { numberOfBytesRead = ns.Read(buttfer, 0, buttfer.Length); strBuildeMailMessage.Append(System.Text.Encoding.ASCII.GetString(buttfer, 0, numberOfBytesRead)); } emailList.Add(strBuildeMailMessage.ToString()); } outbytes = System.Text.Encoding.ASCII.GetBytes("quit\r\n"); ns.Write(outbytes, 0, outbytes.Length); * 功能: 收邮件 * 文件名: Pop3Server.cs * */using System;using System.Collections.Generic;using System.Text;using System.Net.Sockets;using System.IO;using System.Collections;namespace ConsoleApplication1{ public class Pop3Server { #region 常量区 private const string COMMAND_DELE = "dele "; private const string COMMAND_RETR = "retr "; private const string COMMAND_SEP = " "; private const string VALUE_USER = "user "; private const string VALUE_PASS = "pass "; private const string VALUE_ERR = "-ERR"; private const int DEFUALT_PORT = 110; private const string DEFUALT_HOST = "localhost"; #endregion #region 变量区 private string _sHost; private string _sPassWord; private string _sUserName; private int _iPort; private string _sErrorMsg = ""; private TcpClient _tcpClient; private NetworkStream ns; #endregion #region 属性区 /// <summary> /// 邮件服务器名称 defual laocalhost /// </summary> public string Host { set { _sHost = value; } } /// <summary> /// 用户密码 /// </summary> public string PassWord { set { _sPassWord = value; } } /// <summary> /// 用户名 /// </summary> public string UserName { set { _sUserName = value; } } /// <summary> /// 端口,defualt 110 /// </summary> public int Port { set { _iPort = value; } } /// <summary> /// 错误信息 /// </summary> public string ErrorMsg { get { return _sErrorMsg; } } #endregion #region 构造器 /// <summary> /// 构造器,要求服务器和端口 /// </summary> /// <param name="sUserName">用户名</param> /// <param name="sPassWord">密码</param> /// <param name="sHost">服务器名称</param> /// <param name="iPort">端口号</param> public Pop3Server(string sUserName, string sPassWord, string sHost, int iPort) { _sUserName = sUserName; _sPassWord = sPassWord; _sHost = sHost; _iPort = iPort; } /// <summary> /// 构造器,使用默认端口号、服务器 /// </summary> /// <param name="sUserName"></param> /// <param name="sPassWord"></param> public Pop3Server(string sUserName, string sPassWord) : this(sUserName, sPassWord, DEFUALT_HOST, DEFUALT_PORT) { // } #endregion #region 可访问方法 /// <summary> /// 连结到服务器,成功返回真 /// </summary> /// <returns>成功返回真</returns> public Boolean DoConnect() { try { _tcpClient = new TcpClient(_sHost, _iPort); byte[] outbytes; string input; ns = _tcpClient.GetStream(); StreamReader sr = new StreamReader(ns); sr.ReadLine(); input = VALUE_USER + _sUserName + COMMAND_SEP; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); sr.ReadLine(); input = VALUE_PASS + _sPassWord + COMMAND_SEP; outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); sr.ReadLine(); return true; } catch (SocketException ex) { _sErrorMsg = ex.SocketErrorCode.ToString(); return false; } // } /// <summary> /// 删除邮件,成功返回真 /// </summary> /// <param name="iIndex">邮件序号</param> /// <returns>成功返回真</returns> public Boolean DoDeleteMail(int iIndex) { Byte[] outbytes; string input; try { input = COMMAND_DELE + iIndex.ToString() + COMMAND_SEP; outbytes = Encoding.ASCII.GetBytes(input.ToCharArray()); ns.Write(outbytes, 0, outbytes.Length); } catch (SocketException ex) { _sErrorMsg = ex.SocketErrorCode.ToString(); return false; } return true; } /// <summary> /// 按序号取回邮件, 错误时返回null /// </summary> /// <param name="iIndex">邮件序号</param> /// <returns>返回邮件对象 RecMailMsg, 错误时返回null</returns> public RecMailMsg GetMail(int iIndex) { //如果没有连结,连结到服务器 if ((_tcpClient == null) || (!_tcpClient.Connected)) { if (!DoConnect()) { return null; } } //取回邮件内容,置入 ArrayList ArrayList msglines = GetSubMail(iIndex); if (msglines == null) { return null; } //测试,保存到本地,查看内容 saveToFile(msglines); //分解邮件各部份 MailDecode mailDec = new MailDecode(msglines); RecMailMsg mail = new RecMailMsg(); //取回邮件详细信息 mail.Subj = GetMailSubject(mailDec.mailHeard); mail.From = GetMailFrom(mailDec.mailHeard); mail.Date = GetMailDate(mailDec.mailHeard); //返回邮件对象 return mail; } #endregion 接 #region 内部方法 /// <summary> /// 测试, 保存邮件内容到本地 /// </summary> /// <param name="msglines"></param> private void saveToFile(ArrayList msglines) { //合法邮件名测试 string sFileName = "c:\mail"; while (File.Exists(sFileName + ".txt")) { sFileName = sFileName + "i"; } //保存 sFileName = sFileName + ".txt"; using (StreamWriter sw = File.CreateText(sFileName)) { IEnumerator msgenum = msglines.GetEnumerator(); string line = ""; while (msgenum.MoveNext()) { line = (string)msgenum.Current; sw.WriteLine(line); } sw.Close(); } } /// <summary> /// 返回指定序号邮件内容,错误返回null /// </summary> /// <param name="iIndex">邮件序号</param> /// <returns>邮件内容,以ArrayList返回, 错误返回null</returns> private ArrayList GetSubMail(int iIndex) { //准备 Byte[] outbytes; string input; string line = ""; //在异常保护中送入命令 input = COMMAND_RETR + iIndex.ToString() + COMMAND_SEP; outbytes = Encoding.ASCII.GetBytes(input.ToCharArray()); StreamReader sr; try { ns.Write(outbytes, 0, outbytes.Length); sr = new StreamReader(ns); line = sr.ReadLine(); if (line.StartsWith(VALUE_ERR)) { _sErrorMsg = line; return null; } } catch (SocketException ex) { _sErrorMsg = ex.SocketErrorCode.ToString(); return null; } //置入arrayList ArrayList msglines = new ArrayList(); do { line = sr.ReadLine(); msglines.Add(line); } while (line != "."); msglines.RemoveAt(msglines.Count - 1); return msglines; } /// <summary> /// 得到邮件内容 /// </summary> /// <param name="msglines">邮件头Array</param> /// <returns>得到邮件内容</returns> private string GetMailSubject(ArrayList msglines) { string[] tokens; IEnumerator msgenum = msglines.GetEnumerator(); while (msgenum.MoveNext()) { string line = (string)msgenum.Current; if (line.StartsWith("subject:")) { tokens = line.Split(new Char[] { ' ' }); return tokens[1].Trim(); } } return "None"; } /// <summary> /// 得到发件人 /// </summary> /// <param name="msglines"></param> /// <returns>发件人地址</returns> private string GetMailFrom(ArrayList msglines) { string[] tokens; IEnumerator msgenum = msglines.GetEnumerator(); while (msgenum.MoveNext()) { string line = (string)msgenum.Current; if (line.StartsWith("from:")) { tokens = line.Split(':'); return tokens[1].Trim(); } } return "None"; } /// <summary> /// 得到发件日期 /// </summary> /// <param name="msglines"></param> /// <returns>发件日期</returns> private string GetMailDate(ArrayList msglines) { string[] tokens; IEnumerator msgenum = msglines.GetEnumerator(); while (msgenum.MoveNext()) { string line = (string)msgenum.Current; if (line.StartsWith("date:")) { tokens = line.Split(':'); return tokens[1].Trim(); } } return "None"; } #endregion } } C# 使用outlook 收取 、读取邮件http://hi.baidu.com/leader_bin/blog/item/cb5c59fbf77769244e4aea74.html 谁能给个 例子吗 我邮箱 [email protected] 100%结贴。 借个位置 请大虾帮忙一下:http://topic.csdn.net/u/20091130/11/44f40ada-23e2-44af-959f-4d094fa14bc7.html纵向js相册 缩略图上onmouseover 显示图片有随机特效 求三道笔试题答案 页面间传值的问题请教 关于关于Repeater分页 DataReader怎样输出数据库指定字段的数据? 框架集使用 忘了问,谢谢你的帮忙 两个网站之间共享数据 Jquery ajax 请求跨域获取json数据,数据可以获得,但json解析的时候报错,请问有什么方法?weather.com.cn支持的是用iframe潜入 紧急求助 请问:在asp,net中的Response.Redirect()事件,如何控制新打开页面的状态呢?比如大小。。 |zyciis| C# 如何动态的创建一个类,谢谢 域名使用问题
protected void Button1_Click(object sender, EventArgs e)
{
jmail.POP3Class popMail = new jmail.POP3Class(); //建立接收邮件对象
jmail.Message mailMsg; //建立邮件信息接口
jmail.Attachments atts; //建立附件集接口
jmail.Attachment att; //建立附件接口 try
{
popMail.Connect(TextBox3.Text, TextBox4.Text, TextBox1.Text,Convert.ToInt16(TextBox2.Text));
if (popMail.Count <= 0)
{ DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("sender", typeof(string)));
dt.Columns.Add(new DataColumn("senderemail", typeof(string)));
dt.Columns.Add(new DataColumn("subject", typeof(string)));
dt.Columns.Add(new DataColumn("priority", typeof(string)));
dt.Columns.Add(new DataColumn("mailsize", typeof(string)));
dt.Columns.Add(new DataColumn("attname", typeof(string)));
dt.Columns.Add(new DataColumn("mailbody", typeof(string)));
DataRow dr = dt.NewRow();
for (int i = 1; i <= popMail.Count; i++)
{
mailMsg = popMail.Messages[i]; //取得一条邮件信息
atts = mailMsg.Attachments;
mailMsg.Charset = "GB2312";
mailMsg.Encoding = "Base64";
mailMsg.ISOEncodeHeaders = false; dr[0] = mailMsg.FromName;
dr[1] = mailMsg.From;
dr[2] = mailMsg.Subject;
dr[3] = mailMsg.Priority.ToString();
dr[4] = mailMsg.Size.ToString();
att = atts[0];
dr[5] = att.Name;
dr[6] = mailMsg.Body;
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind(); att = null;
atts = null;
}
else
{
Response.Write("没有新邮件");
}
}
catch (Exception ex)
{
Response.Write("错误!请检查邮件服务器的设置是否正确");
} }
using System.Net.Sockets;
using System.IO;
using System.Collections;
using System.Net.Mail;/// <summary>
/// 获取邮件信息
/// </summary>
/// <param name="subj"></param>
/// <returns></returns>
public ArrayList GetNewMessages(string subj)
{ int newcount;
ArrayList newmsgs = new ArrayList(); try
{
newcount = GetNumberOfNewMessages();
Connect(); for(int n=1; n<newcount+1; n++)
{
ArrayList msglines = GetRawMessage(n);
string msgsubj = GetMessageSubject(msglines);
if(msgsubj.CompareTo(subj) == 0)
{
System.Net.Mail.MailMessage msg = new MailMessage();
msg.Subject = msgsubj;
msg.From =new MailAddress(GetMessageFrom(msglines));
msg.Body = GetMessageBody(msglines);
newmsgs.Add(msg);
DeleteMessage(n);
}
} Disconnect();
return newmsgs;
}
catch(Exception e)
{
return newmsgs;
}
}
/**//// <summary>
/// 获取新邮件
/// </summary>
/// <param name="nIndex"></param>
/// <returns></returns>
public MailMessage GetNewMessages(int nIndex)
{
int newcount;
System.Net.Mail.MailMessage msg = new MailMessage(); try
{
newcount = GetNumberOfNewMessages();
Connect();
int n = nIndex+1; if(n<newcount+1)
{
ArrayList msglines = GetRawMessage(n);
string msgsubj = GetMessageSubject(msglines);
msg.Subject = msgsubj;
msg.From = new MailAddress(GetMessageFrom(msglines));
msg.Body = GetMessageBody(msglines);
} Disconnect();
return msg;
}
catch
{
return null;
}
}
private ArrayList GetRawMessage (int messagenumber)
{
Byte[] outbytes;
string input;
string line = ""; input = "retr " + messagenumber.ToString() + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length); ArrayList msglines = new ArrayList();
do
{
line = sr.ReadLine();
msglines.Add(line);
} while (line != ".");
msglines.RemoveAt(msglines.Count-1); return msglines;
}
private bool Connect()
{
TcpClient sender = new TcpClient(POPServer, port);
byte[] outbytes;
string input;
try
{
ns = sender.GetStream();
sr = new StreamReader(ns); sr.ReadLine();
input = "user " + user + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine(); input = "pass " + pwd + "\r\n";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine();
return true;
}
catch
{
return false;
}
}
string strUserName = "";//用户名
string strPassWord = ""; //密码
StringBuilder strBuildeMailMessage = new StringBuilder();
int numberOfBytesRead = 0;
int length = 1024 * 10;
byte[] buttfer = new byte[length];
TcpClient tp = new TcpClient(strServerName, 110);
tp.ReceiveTimeout = 1000 * 300;
NetworkStream ns = tp.GetStream();
ns.ReadTimeout = 1000 * 300;
ns.Read(buttfer, 0, buttfer.Length); byte[] outbytes = System.Text.Encoding.ASCII.GetBytes("user " + strUserName + "\r\n");
ns.Write(outbytes, 0, outbytes.Length);
numberOfBytesRead = ns.Read(buttfer, 0, buttfer.Length);
strBuildeMailMessage.Append(System.Text.Encoding.ASCII.GetString(buttfer, 0, numberOfBytesRead)); strBuildeMailMessage = new StringBuilder();
outbytes = System.Text.Encoding.ASCII.GetBytes("pass " + strPassWord + "\r\n");
ns.Write(outbytes, 0, outbytes.Length);
numberOfBytesRead = ns.Read(buttfer, 0, buttfer.Length);
strBuildeMailMessage.Append(System.Text.Encoding.ASCII.GetString(buttfer, 0, numberOfBytesRead));
Regex re = new Regex(@"(?<=has\s+)[\d]+(?=\s+messages)");
int num = int.Parse(re.Match(strBuildeMailMessage.ToString()).Value.Trim());
List<string> emailList = new List<string>();
for (int i = 1; i <= num; i++)
{
strBuildeMailMessage = new StringBuilder();
outbytes = System.Text.Encoding.ASCII.GetBytes("retr " + i.ToString() + "\r\n");
ns.Write(outbytes, 0, outbytes.Length);
Thread.Sleep(800);
while (ns.DataAvailable)
{
numberOfBytesRead = ns.Read(buttfer, 0, buttfer.Length);
strBuildeMailMessage.Append(System.Text.Encoding.ASCII.GetString(buttfer, 0, numberOfBytesRead));
}
emailList.Add(strBuildeMailMessage.ToString());
}
outbytes = System.Text.Encoding.ASCII.GetBytes("quit\r\n");
ns.Write(outbytes, 0, outbytes.Length);
* 功能: 收邮件
* 文件名: Pop3Server.cs
*
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Collections;namespace ConsoleApplication1
{
public class Pop3Server
{
#region 常量区
private const string COMMAND_DELE = "dele ";
private const string COMMAND_RETR = "retr ";
private const string COMMAND_SEP = " ";
private const string VALUE_USER = "user ";
private const string VALUE_PASS = "pass ";
private const string VALUE_ERR = "-ERR";
private const int DEFUALT_PORT = 110;
private const string DEFUALT_HOST = "localhost";
#endregion #region 变量区
private string _sHost;
private string _sPassWord;
private string _sUserName;
private int _iPort;
private string _sErrorMsg = "";
private TcpClient _tcpClient;
private NetworkStream ns;
#endregion #region 属性区
/// <summary>
/// 邮件服务器名称 defual laocalhost
/// </summary>
public string Host
{
set
{
_sHost = value;
}
}
/// <summary>
/// 用户密码
/// </summary>
public string PassWord
{
set
{
_sPassWord = value;
}
}
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
set
{
_sUserName = value;
}
}
/// <summary>
/// 端口,defualt 110
/// </summary>
public int Port
{
set
{
_iPort = value;
}
}
/// <summary>
/// 错误信息
/// </summary>
public string ErrorMsg
{
get
{
return _sErrorMsg;
}
}
#endregion #region 构造器
/// <summary>
/// 构造器,要求服务器和端口
/// </summary>
/// <param name="sUserName">用户名</param>
/// <param name="sPassWord">密码</param>
/// <param name="sHost">服务器名称</param>
/// <param name="iPort">端口号</param>
public Pop3Server(string sUserName, string sPassWord, string sHost, int iPort)
{
_sUserName = sUserName;
_sPassWord = sPassWord;
_sHost = sHost;
_iPort = iPort;
}
/// <summary>
/// 构造器,使用默认端口号、服务器
/// </summary>
/// <param name="sUserName"></param>
/// <param name="sPassWord"></param>
public Pop3Server(string sUserName, string sPassWord)
: this(sUserName, sPassWord, DEFUALT_HOST, DEFUALT_PORT)
{
//
}
#endregion #region 可访问方法
/// <summary>
/// 连结到服务器,成功返回真
/// </summary>
/// <returns>成功返回真</returns>
public Boolean DoConnect()
{
try
{
_tcpClient = new TcpClient(_sHost, _iPort);
byte[] outbytes;
string input;
ns = _tcpClient.GetStream();
StreamReader sr = new StreamReader(ns); sr.ReadLine();
input = VALUE_USER + _sUserName + COMMAND_SEP;
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine(); input = VALUE_PASS + _sPassWord + COMMAND_SEP;
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
sr.ReadLine();
return true;
}
catch (SocketException ex)
{
_sErrorMsg = ex.SocketErrorCode.ToString();
return false;
}
//
}
/// <summary>
/// 删除邮件,成功返回真
/// </summary>
/// <param name="iIndex">邮件序号</param>
/// <returns>成功返回真</returns>
public Boolean DoDeleteMail(int iIndex)
{
Byte[] outbytes;
string input;
try
{
input = COMMAND_DELE + iIndex.ToString() + COMMAND_SEP;
outbytes = Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes, 0, outbytes.Length);
}
catch (SocketException ex)
{
_sErrorMsg = ex.SocketErrorCode.ToString();
return false;
}
return true; }
/// <summary>
/// 按序号取回邮件, 错误时返回null
/// </summary>
/// <param name="iIndex">邮件序号</param>
/// <returns>返回邮件对象 RecMailMsg, 错误时返回null</returns>
public RecMailMsg GetMail(int iIndex)
{
//如果没有连结,连结到服务器
if ((_tcpClient == null) || (!_tcpClient.Connected))
{
if (!DoConnect())
{
return null;
}
} //取回邮件内容,置入 ArrayList
ArrayList msglines = GetSubMail(iIndex);
if (msglines == null)
{
return null;
} //测试,保存到本地,查看内容
saveToFile(msglines); //分解邮件各部份
MailDecode mailDec = new MailDecode(msglines);
RecMailMsg mail = new RecMailMsg(); //取回邮件详细信息
mail.Subj = GetMailSubject(mailDec.mailHeard);
mail.From = GetMailFrom(mailDec.mailHeard);
mail.Date = GetMailDate(mailDec.mailHeard); //返回邮件对象
return mail;
}
#endregion
接
#region 内部方法
/// <summary>
/// 测试, 保存邮件内容到本地
/// </summary>
/// <param name="msglines"></param>
private void saveToFile(ArrayList msglines)
{
//合法邮件名测试
string sFileName = "c:\mail";
while (File.Exists(sFileName + ".txt"))
{
sFileName = sFileName + "i";
} //保存
sFileName = sFileName + ".txt";
using (StreamWriter sw = File.CreateText(sFileName))
{
IEnumerator msgenum = msglines.GetEnumerator();
string line = "";
while (msgenum.MoveNext())
{
line = (string)msgenum.Current;
sw.WriteLine(line);
}
sw.Close(); }
}
/// <summary>
/// 返回指定序号邮件内容,错误返回null
/// </summary>
/// <param name="iIndex">邮件序号</param>
/// <returns>邮件内容,以ArrayList返回, 错误返回null</returns>
private ArrayList GetSubMail(int iIndex)
{
//准备
Byte[] outbytes;
string input;
string line = ""; //在异常保护中送入命令
input = COMMAND_RETR + iIndex.ToString() + COMMAND_SEP;
outbytes = Encoding.ASCII.GetBytes(input.ToCharArray());
StreamReader sr;
try
{
ns.Write(outbytes, 0, outbytes.Length);
sr = new StreamReader(ns);
line = sr.ReadLine();
if (line.StartsWith(VALUE_ERR))
{
_sErrorMsg = line;
return null;
}
}
catch (SocketException ex)
{
_sErrorMsg = ex.SocketErrorCode.ToString();
return null;
} //置入arrayList
ArrayList msglines = new ArrayList();
do
{
line = sr.ReadLine();
msglines.Add(line);
} while (line != ".");
msglines.RemoveAt(msglines.Count - 1); return msglines;
}
/// <summary>
/// 得到邮件内容
/// </summary>
/// <param name="msglines">邮件头Array</param>
/// <returns>得到邮件内容</returns>
private string GetMailSubject(ArrayList msglines)
{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext())
{
string line = (string)msgenum.Current; if (line.StartsWith("subject:"))
{
tokens = line.Split(new Char[] { ' ' });
return tokens[1].Trim();
}
}
return "None";
}
/// <summary>
/// 得到发件人
/// </summary>
/// <param name="msglines"></param>
/// <returns>发件人地址</returns>
private string GetMailFrom(ArrayList msglines)
{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext())
{
string line = (string)msgenum.Current;
if (line.StartsWith("from:"))
{
tokens = line.Split(':');
return tokens[1].Trim();
}
}
return "None";
}
/// <summary>
/// 得到发件日期
/// </summary>
/// <param name="msglines"></param>
/// <returns>发件日期</returns>
private string GetMailDate(ArrayList msglines)
{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext())
{
string line = (string)msgenum.Current;
if (line.StartsWith("date:"))
{
tokens = line.Split(':');
return tokens[1].Trim(); }
}
return "None";
}
#endregion
}
}
100%结贴。
纵向js相册 缩略图上onmouseover 显示图片有随机特效