using
System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Web.Mail;
using System.Text;
namespace UseFramework2.MailSys
... {
/**//// <summary>
/// Mail 的摘要说明。
/// </summary>
public class Mail : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.TextBox tbServer;
protected System.Web.UI.WebControls.TextBox tbUser;
protected System.Web.UI.WebControls.TextBox tbPass;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Button btnCheck;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.TextBox tbSmtp;
protected System.Web.UI.WebControls.TextBox tbReceive;
protected System.Web.UI.WebControls.TextBox tbSubject;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.Button btnSend;
protected System.Web.UI.WebControls.Label Label10;
protected System.Web.UI.WebControls.TextBox tbSend;
protected System.Web.UI.WebControls.TextBox tbBody;
protected System.Web.UI.WebControls.DropDownList ddlNew;
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
}
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.btnCheck.Click += new System.EventHandler(this.btnCheck_Click);
this.ddlNew.SelectedIndexChanged += new System.EventHandler(this.ddlNew_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnCheck_Click(object sender, System.EventArgs e)
...{
POP3 pop = new POP3(tbServer.Text,tbUser.Text,tbPass.Text);
int n = pop.GetNumberOfNewMessages();
if(n==-1)
...{
Response.Write("<script language='javascript'>alert('服务器连接错误!')</script>");
return;
}
ddlNew.Items.Clear();
for(int i=1;i<=n;i++)
ddlNew.Items.Add("第"+i.ToString()+"封邮件");
if(n>0)
...{
MailMessage msg = pop.GetNewMessages(0);
if(msg!=null)
tbBody.Text = msg.Body;
Response.Write(tbBody.Text);
}
}
private void ddlNew_SelectedIndexChanged(object sender, System.EventArgs e)
...{
POP3 pop = new POP3(tbServer.Text,tbUser.Text,tbPass.Text);
int n = ddlNew.SelectedIndex;
if(n>=0)
...{
MailMessage msg = pop.GetNewMessages(n);
if(msg!=null)
tbBody.Text = msg.Body;
}
}
private void btnSend_Click(object sender, System.EventArgs e)
...{
SMTP smtp = new SMTP();
bool bSuccess = smtp.Send(tbSmtp.Text,25,tbSend.Text,tbReceive.Text,tbSubject.Text,tbBody.Text);
if(bSuccess)
Response.Write("<script language='javascript'>alert('邮件发送成功!')</script>");
else
Response.Write("<script language='javascript'>alert('邮件发送失败!')</script>");
}
}
public class POP3
...{
string POPServer;
string user;
string pwd;
NetworkStream ns;
StreamReader sr;
public POP3()...{}
public POP3(string server, string _user, string _pwd)
...{
POPServer = server;
user = _user;
pwd = _pwd;
}
private bool Connect()
...{
TcpClient sender = new TcpClient(POPServer,110);
byte[] outbytes;
string input;
System.Text.Encoding DefaultEncoding = System.Text.Encoding.GetEncoding("GB2312");
try
...{
ns = sender.GetStream();
sr = new StreamReader(ns,DefaultEncoding);
sr.ReadLine();
input = "user " + user + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
sr.ReadLine();
input = "pass " + pwd + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
sr.ReadLine();
return true;
}
catch
...{
return false;
}
}
private void Disconnect()
...{
string input = "quit" + " ";
Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
ns.Close();
}
public int GetNumberOfNewMessages()
...{
byte[] outbytes;
string input;
try
...{
Connect();
input = "stat" + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
string resp = sr.ReadLine();
string[] tokens = resp.Split(new Char[] ...{' '});
Disconnect();
return Convert.ToInt32(tokens[1]);
}
catch
...{
return -1;
}
}
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.Web.Mail.MailMessage msg = new MailMessage();
msg.Subject = msgsubj;
msg.From = GetMessageFrom(msglines);
msg.Body = GetMessageBody(msglines);
newmsgs.Add(msg);
DeleteMessage(n);
}
}
Disconnect();
return newmsgs;
}
catch(Exception e)
...{
return newmsgs;
}
}
public MailMessage GetNewMessages(int nIndex)
...{
int newcount;
System.Web.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 = GetMessageFrom(msglines);
msg.Body = GetMessageBody(msglines);
if(msg.Subject.IndexOf("gb2312")!=-1)//中文邮件
...{
msg.Body = ConvertBM(msg.Body);
}
}
Disconnect();
return msg;
}
catch
...{
return null;
}
}
private string ConvertBM(string strS)
...{
char[] ch = strS.ToCharArray();
byte[] bt = Convert.FromBase64CharArray(ch,0,ch.Length);
System.Text.Encoding gbEncode = System.Text.Encoding.GetEncoding("GB2312");
System.Text.Encoding defaultEncode = System.Text.Encoding.UTF8;
byte[] defaultBytes = Encoding.Convert(gbEncode, defaultEncode, bt);
string strDes = defaultEncode.GetString(defaultBytes);
return strDes;
}
private ArrayList GetRawMessage (int messagenumber)
...{
Byte[] outbytes;
string input;
string sTemp = "";
string line = "";
input = "retr " + messagenumber.ToString() + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
ArrayList msglines = new ArrayList();
do
...{
line = sr.ReadLine();
System.Text.Encoding gbEncode = System.Text.Encoding.GetEncoding("GB2312");
System.Text.Encoding defaultEncode = System.Text.Encoding.ASCII;
byte[] defaultBytes = defaultEncode.GetBytes(line);
byte[] gbBytes = Encoding.Convert(defaultEncode,gbEncode,defaultBytes);
line = gbEncode.GetString(gbBytes);
sTemp += line;
msglines.Add(line);
} while (line != ".");
msglines.RemoveAt(msglines.Count-1);
return msglines;
}
private string GetMessageSubject(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";
}
private string GetMessageFrom (ArrayList msglines)
...{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext() )
...{
string line = (string)msgenum.Current;
if(line.StartsWith("From:") )
...{
tokens = line.Split(new Char[] ...{'<'});
return tokens[1].Trim(new Char[] ...{'<','>'});
}
}
return "None";
}
private string GetMessageBody(ArrayList msglines)
...{
string body = "";
string line = " ";
IEnumerator msgenum = msglines.GetEnumerator();
while(line.CompareTo("") != 0)
...{
msgenum.MoveNext();
line = (string)msgenum.Current;
}
while (msgenum.MoveNext() )
...{
body = body + (string)msgenum.Current + " ";
}
return body;
}
private void DeleteMessage(int messagenumber)
...{
Byte[] outbytes;
string input;
try
...{
input = "dele " + messagenumber.ToString() + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
}
catch(Exception e)
...{
return;
}
}
}
public class SMTP
...{
public SMTP()...{}
public bool Send(string strSmtpServer,int nPort,string strSend,string strReceive
,string strSubject,string strContent)
...{
string smtpserver=strSmtpServer;// smtp服务器的IP地址
TcpClient tcpc = new TcpClient();
try
...{
tcpc.Connect(smtpserver, nPort);
StreamReader sr ;
string strCmd;
sr = new StreamReader(tcpc.GetStream(),Encoding.Default);
//服务器连接成功以后,首先向服务器发送HeLlo命令
strCmd="HELO shaozhd";
SenSmtpCmd(tcpc,strCmd);
//然后向服务器发送信件的成员的信箱
strCmd="mail from:"+ strSend;
SenSmtpCmd(tcpc,strCmd);
//向服务器发送收件人的信箱
strCmd="rcpt to:" + strReceive;
SenSmtpCmd(tcpc,strCmd);
//所有的准备工作都已经作好了,下面开始进行邮件的部分
strCmd="data";
SenSmtpCmd(tcpc,strCmd);
//邮件内容
strCmd="Date: 1234567 ";
strCmd=strCmd+"From: " + strSend +" ";
strCmd=strCmd+"To: " + strReceive +" ";
strCmd=strCmd+"Subject: " + strSubject +" ";
strCmd=strCmd + strContent +" ";
SenSmtpCmd(tcpc,strCmd);
strCmd=" . ";
SenSmtpCmd(tcpc,strCmd);
//最后 关闭与smtp 服务器的连接
tcpc.Close();
return true;
}
catch(Exception ex)
...{
return false;
}
}
void SenSmtpCmd(TcpClient tcpc,String strCmd)
...{
byte[] arrCmd;
string strRet;
StreamReader sr;
Stream s;
s=tcpc.GetStream();
strCmd = strCmd + " ";
arrCmd= Encoding.Default.GetBytes(strCmd.ToCharArray());
s=tcpc.GetStream();
s.Write(arrCmd, 0, strCmd.Length);
//以下用于程序调试,显示服务器回应信息
sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
strRet=sr.ReadLine();
return;
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Web.Mail;
using System.Text;
namespace UseFramework2.MailSys
... {
/**//// <summary>
/// Mail 的摘要说明。
/// </summary>
public class Mail : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.TextBox tbServer;
protected System.Web.UI.WebControls.TextBox tbUser;
protected System.Web.UI.WebControls.TextBox tbPass;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Button btnCheck;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.TextBox tbSmtp;
protected System.Web.UI.WebControls.TextBox tbReceive;
protected System.Web.UI.WebControls.TextBox tbSubject;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.Button btnSend;
protected System.Web.UI.WebControls.Label Label10;
protected System.Web.UI.WebControls.TextBox tbSend;
protected System.Web.UI.WebControls.TextBox tbBody;
protected System.Web.UI.WebControls.DropDownList ddlNew;
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
}
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.btnCheck.Click += new System.EventHandler(this.btnCheck_Click);
this.ddlNew.SelectedIndexChanged += new System.EventHandler(this.ddlNew_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnCheck_Click(object sender, System.EventArgs e)
...{
POP3 pop = new POP3(tbServer.Text,tbUser.Text,tbPass.Text);
int n = pop.GetNumberOfNewMessages();
if(n==-1)
...{
Response.Write("<script language='javascript'>alert('服务器连接错误!')</script>");
return;
}
ddlNew.Items.Clear();
for(int i=1;i<=n;i++)
ddlNew.Items.Add("第"+i.ToString()+"封邮件");
if(n>0)
...{
MailMessage msg = pop.GetNewMessages(0);
if(msg!=null)
tbBody.Text = msg.Body;
Response.Write(tbBody.Text);
}
}
private void ddlNew_SelectedIndexChanged(object sender, System.EventArgs e)
...{
POP3 pop = new POP3(tbServer.Text,tbUser.Text,tbPass.Text);
int n = ddlNew.SelectedIndex;
if(n>=0)
...{
MailMessage msg = pop.GetNewMessages(n);
if(msg!=null)
tbBody.Text = msg.Body;
}
}
private void btnSend_Click(object sender, System.EventArgs e)
...{
SMTP smtp = new SMTP();
bool bSuccess = smtp.Send(tbSmtp.Text,25,tbSend.Text,tbReceive.Text,tbSubject.Text,tbBody.Text);
if(bSuccess)
Response.Write("<script language='javascript'>alert('邮件发送成功!')</script>");
else
Response.Write("<script language='javascript'>alert('邮件发送失败!')</script>");
}
}
public class POP3
...{
string POPServer;
string user;
string pwd;
NetworkStream ns;
StreamReader sr;
public POP3()...{}
public POP3(string server, string _user, string _pwd)
...{
POPServer = server;
user = _user;
pwd = _pwd;
}
private bool Connect()
...{
TcpClient sender = new TcpClient(POPServer,110);
byte[] outbytes;
string input;
System.Text.Encoding DefaultEncoding = System.Text.Encoding.GetEncoding("GB2312");
try
...{
ns = sender.GetStream();
sr = new StreamReader(ns,DefaultEncoding);
sr.ReadLine();
input = "user " + user + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
sr.ReadLine();
input = "pass " + pwd + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length) ;
sr.ReadLine();
return true;
}
catch
...{
return false;
}
}
private void Disconnect()
...{
string input = "quit" + " ";
Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
ns.Close();
}
public int GetNumberOfNewMessages()
...{
byte[] outbytes;
string input;
try
...{
Connect();
input = "stat" + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
string resp = sr.ReadLine();
string[] tokens = resp.Split(new Char[] ...{' '});
Disconnect();
return Convert.ToInt32(tokens[1]);
}
catch
...{
return -1;
}
}
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.Web.Mail.MailMessage msg = new MailMessage();
msg.Subject = msgsubj;
msg.From = GetMessageFrom(msglines);
msg.Body = GetMessageBody(msglines);
newmsgs.Add(msg);
DeleteMessage(n);
}
}
Disconnect();
return newmsgs;
}
catch(Exception e)
...{
return newmsgs;
}
}
public MailMessage GetNewMessages(int nIndex)
...{
int newcount;
System.Web.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 = GetMessageFrom(msglines);
msg.Body = GetMessageBody(msglines);
if(msg.Subject.IndexOf("gb2312")!=-1)//中文邮件
...{
msg.Body = ConvertBM(msg.Body);
}
}
Disconnect();
return msg;
}
catch
...{
return null;
}
}
private string ConvertBM(string strS)
...{
char[] ch = strS.ToCharArray();
byte[] bt = Convert.FromBase64CharArray(ch,0,ch.Length);
System.Text.Encoding gbEncode = System.Text.Encoding.GetEncoding("GB2312");
System.Text.Encoding defaultEncode = System.Text.Encoding.UTF8;
byte[] defaultBytes = Encoding.Convert(gbEncode, defaultEncode, bt);
string strDes = defaultEncode.GetString(defaultBytes);
return strDes;
}
private ArrayList GetRawMessage (int messagenumber)
...{
Byte[] outbytes;
string input;
string sTemp = "";
string line = "";
input = "retr " + messagenumber.ToString() + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
ArrayList msglines = new ArrayList();
do
...{
line = sr.ReadLine();
System.Text.Encoding gbEncode = System.Text.Encoding.GetEncoding("GB2312");
System.Text.Encoding defaultEncode = System.Text.Encoding.ASCII;
byte[] defaultBytes = defaultEncode.GetBytes(line);
byte[] gbBytes = Encoding.Convert(defaultEncode,gbEncode,defaultBytes);
line = gbEncode.GetString(gbBytes);
sTemp += line;
msglines.Add(line);
} while (line != ".");
msglines.RemoveAt(msglines.Count-1);
return msglines;
}
private string GetMessageSubject(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";
}
private string GetMessageFrom (ArrayList msglines)
...{
string[] tokens;
IEnumerator msgenum = msglines.GetEnumerator();
while (msgenum.MoveNext() )
...{
string line = (string)msgenum.Current;
if(line.StartsWith("From:") )
...{
tokens = line.Split(new Char[] ...{'<'});
return tokens[1].Trim(new Char[] ...{'<','>'});
}
}
return "None";
}
private string GetMessageBody(ArrayList msglines)
...{
string body = "";
string line = " ";
IEnumerator msgenum = msglines.GetEnumerator();
while(line.CompareTo("") != 0)
...{
msgenum.MoveNext();
line = (string)msgenum.Current;
}
while (msgenum.MoveNext() )
...{
body = body + (string)msgenum.Current + " ";
}
return body;
}
private void DeleteMessage(int messagenumber)
...{
Byte[] outbytes;
string input;
try
...{
input = "dele " + messagenumber.ToString() + " ";
outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());
ns.Write(outbytes,0,outbytes.Length);
}
catch(Exception e)
...{
return;
}
}
}
public class SMTP
...{
public SMTP()...{}
public bool Send(string strSmtpServer,int nPort,string strSend,string strReceive
,string strSubject,string strContent)
...{
string smtpserver=strSmtpServer;// smtp服务器的IP地址
TcpClient tcpc = new TcpClient();
try
...{
tcpc.Connect(smtpserver, nPort);
StreamReader sr ;
string strCmd;
sr = new StreamReader(tcpc.GetStream(),Encoding.Default);
//服务器连接成功以后,首先向服务器发送HeLlo命令
strCmd="HELO shaozhd";
SenSmtpCmd(tcpc,strCmd);
//然后向服务器发送信件的成员的信箱
strCmd="mail from:"+ strSend;
SenSmtpCmd(tcpc,strCmd);
//向服务器发送收件人的信箱
strCmd="rcpt to:" + strReceive;
SenSmtpCmd(tcpc,strCmd);
//所有的准备工作都已经作好了,下面开始进行邮件的部分
strCmd="data";
SenSmtpCmd(tcpc,strCmd);
//邮件内容
strCmd="Date: 1234567 ";
strCmd=strCmd+"From: " + strSend +" ";
strCmd=strCmd+"To: " + strReceive +" ";
strCmd=strCmd+"Subject: " + strSubject +" ";
strCmd=strCmd + strContent +" ";
SenSmtpCmd(tcpc,strCmd);
strCmd=" . ";
SenSmtpCmd(tcpc,strCmd);
//最后 关闭与smtp 服务器的连接
tcpc.Close();
return true;
}
catch(Exception ex)
...{
return false;
}
}
void SenSmtpCmd(TcpClient tcpc,String strCmd)
...{
byte[] arrCmd;
string strRet;
StreamReader sr;
Stream s;
s=tcpc.GetStream();
strCmd = strCmd + " ";
arrCmd= Encoding.Default.GetBytes(strCmd.ToCharArray());
s=tcpc.GetStream();
s.Write(arrCmd, 0, strCmd.Length);
//以下用于程序调试,显示服务器回应信息
sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
strRet=sr.ReadLine();
return;
}
}
}