前台代码:html
<%@ Page language="c#" Codebehind="SocketMail.aspx.cs" AutoEventWireup="false" Inherits="UseFramework2.MailSys.Mail" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Mail</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Mail" method="post" runat="server">
<FONT face="宋体">
<asp:TextBox id="tbServer" style="Z-INDEX: 101; LEFT: 143px; POSITION: absolute; TOP: 104px" runat="server"></asp:TextBox>
<asp:TextBox id="tbSend" style="Z-INDEX: 120; LEFT: 413px; POSITION: absolute; TOP: 223px" runat="server"></asp:TextBox>
<asp:Label id="Label10" style="Z-INDEX: 119; LEFT: 334px; POSITION: absolute; TOP: 231px" runat="server">发件人</asp:Label>
<asp:Button id="btnSend" style="Z-INDEX: 118; LEFT: 441px; POSITION: absolute; TOP: 325px" runat="server" Width="76" Text="发送" Height="25"></asp:Button>
<asp:Label id="Label9" style="Z-INDEX: 117; LEFT: 332px; POSITION: absolute; TOP: 179px" runat="server">收件人</asp:Label>
<asp:Label id="Label8" style="Z-INDEX: 116; LEFT: 329px; POSITION: absolute; TOP: 278px" runat="server">邮件主题</asp:Label>
<asp:Label id="Label7" style="Z-INDEX: 115; LEFT: 320px; POSITION: absolute; TOP: 102px" runat="server">SMTP服务器</asp:Label>
<asp:TextBox id="tbSubject" style="Z-INDEX: 114; LEFT: 415px; POSITION: absolute; TOP: 270px" runat="server"></asp:TextBox>
<asp:TextBox id="tbReceive" style="Z-INDEX: 113; LEFT: 416px; POSITION: absolute; TOP: 171px" runat="server"></asp:TextBox>
<asp:TextBox id="tbSmtp" style="Z-INDEX: 112; LEFT: 419px; POSITION: absolute; TOP: 100px" runat="server"></asp:TextBox>
<asp:Label id="Label5" style="Z-INDEX: 109; LEFT: 54px; POSITION: absolute; TOP: 245px" runat="server">密码</asp:Label>
<asp:Label id="Label4" style="Z-INDEX: 108; LEFT: 54px; POSITION: absolute; TOP: 181px" runat="server">用户名称</asp:Label>
<asp:label id="Label1" style="Z-INDEX: 106; LEFT: 414px; POSITION: absolute; TOP: 18px" runat="server" Width="175px" Font-Names="隶书" Font-Size="XX-Large">发邮件</asp:label>
<asp:label id="Label2" style="Z-INDEX: 105; LEFT: 85px; POSITION: absolute; TOP: 14px" runat="server" Width="175px" Font-Names="隶书" Font-Size="XX-Large">收邮件</asp:label>
<asp:TextBox id="tbUser" style="Z-INDEX: 102; LEFT: 143px; POSITION: absolute; TOP: 182px" runat="server"></asp:TextBox>
<asp:TextBox id="tbPass" style="Z-INDEX: 103; LEFT: 143px; POSITION: absolute; TOP: 242px" runat="server" TextMode="Password"></asp:TextBox>
<asp:TextBox id="tbBody" style="Z-INDEX: 104; LEFT: 141px; POSITION: absolute; TOP: 363px" runat="server" Width="458px" Height="87px"></asp:TextBox>
<asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 54px; POSITION: absolute; TOP: 103px" runat="server">POP3服务器</asp:Label>
<asp:Button id="btnCheck" style="Z-INDEX: 110; LEFT: 67px; POSITION: absolute; TOP: 315px" runat="server" Width="76" Text="检查" Height="25"></asp:Button>
<asp:Label id="Label6" style="Z-INDEX: 111; LEFT: 60px; POSITION: absolute; TOP: 387px" runat="server" Width="71px" Height="27px">邮件正文</asp:Label>
<asp:DropDownList id="ddlNew" style="Z-INDEX: 121; LEFT: 200px; POSITION: absolute; TOP: 319px" runat="server" AutoPostBack="True"></asp:DropDownList></FONT></form>
</body>
</HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Mail</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Mail" method="post" runat="server">
<FONT face="宋体">
<asp:TextBox id="tbServer" style="Z-INDEX: 101; LEFT: 143px; POSITION: absolute; TOP: 104px" runat="server"></asp:TextBox>
<asp:TextBox id="tbSend" style="Z-INDEX: 120; LEFT: 413px; POSITION: absolute; TOP: 223px" runat="server"></asp:TextBox>
<asp:Label id="Label10" style="Z-INDEX: 119; LEFT: 334px; POSITION: absolute; TOP: 231px" runat="server">发件人</asp:Label>
<asp:Button id="btnSend" style="Z-INDEX: 118; LEFT: 441px; POSITION: absolute; TOP: 325px" runat="server" Width="76" Text="发送" Height="25"></asp:Button>
<asp:Label id="Label9" style="Z-INDEX: 117; LEFT: 332px; POSITION: absolute; TOP: 179px" runat="server">收件人</asp:Label>
<asp:Label id="Label8" style="Z-INDEX: 116; LEFT: 329px; POSITION: absolute; TOP: 278px" runat="server">邮件主题</asp:Label>
<asp:Label id="Label7" style="Z-INDEX: 115; LEFT: 320px; POSITION: absolute; TOP: 102px" runat="server">SMTP服务器</asp:Label>
<asp:TextBox id="tbSubject" style="Z-INDEX: 114; LEFT: 415px; POSITION: absolute; TOP: 270px" runat="server"></asp:TextBox>
<asp:TextBox id="tbReceive" style="Z-INDEX: 113; LEFT: 416px; POSITION: absolute; TOP: 171px" runat="server"></asp:TextBox>
<asp:TextBox id="tbSmtp" style="Z-INDEX: 112; LEFT: 419px; POSITION: absolute; TOP: 100px" runat="server"></asp:TextBox>
<asp:Label id="Label5" style="Z-INDEX: 109; LEFT: 54px; POSITION: absolute; TOP: 245px" runat="server">密码</asp:Label>
<asp:Label id="Label4" style="Z-INDEX: 108; LEFT: 54px; POSITION: absolute; TOP: 181px" runat="server">用户名称</asp:Label>
<asp:label id="Label1" style="Z-INDEX: 106; LEFT: 414px; POSITION: absolute; TOP: 18px" runat="server" Width="175px" Font-Names="隶书" Font-Size="XX-Large">发邮件</asp:label>
<asp:label id="Label2" style="Z-INDEX: 105; LEFT: 85px; POSITION: absolute; TOP: 14px" runat="server" Width="175px" Font-Names="隶书" Font-Size="XX-Large">收邮件</asp:label>
<asp:TextBox id="tbUser" style="Z-INDEX: 102; LEFT: 143px; POSITION: absolute; TOP: 182px" runat="server"></asp:TextBox>
<asp:TextBox id="tbPass" style="Z-INDEX: 103; LEFT: 143px; POSITION: absolute; TOP: 242px" runat="server" TextMode="Password"></asp:TextBox>
<asp:TextBox id="tbBody" style="Z-INDEX: 104; LEFT: 141px; POSITION: absolute; TOP: 363px" runat="server" Width="458px" Height="87px"></asp:TextBox>
<asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 54px; POSITION: absolute; TOP: 103px" runat="server">POP3服务器</asp:Label>
<asp:Button id="btnCheck" style="Z-INDEX: 110; LEFT: 67px; POSITION: absolute; TOP: 315px" runat="server" Width="76" Text="检查" Height="25"></asp:Button>
<asp:Label id="Label6" style="Z-INDEX: 111; LEFT: 60px; POSITION: absolute; TOP: 387px" runat="server" Width="71px" Height="27px">邮件正文</asp:Label>
<asp:DropDownList id="ddlNew" style="Z-INDEX: 121; LEFT: 200px; POSITION: absolute; TOP: 319px" runat="server" AutoPostBack="True"></asp:DropDownList></FONT></form>
</body>
</HTML>
后台代码:cs
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
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;
}
}
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;
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;
}
}
private void Disconnect()
{
string input = "quit" + "\r\n";
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" + "\r\n";
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);
}
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 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 + "\r\n";
}
return body;
}
private void DeleteMessage(int messagenumber)
{
Byte[] outbytes;
string input;
try
{
input = "dele " + messagenumber.ToString() + "\r\n";
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\r\n";
strCmd=strCmd+"From: " + strSend +"\r\n";
strCmd=strCmd+"To: " + strReceive +"\r\n";
strCmd=strCmd+"Subject: " + strSubject +"\r\n\r\n";
strCmd=strCmd + strContent +"\r\n\r\n";
SenSmtpCmd(tcpc,strCmd);
strCmd="\r\n.\r\n";
SenSmtpCmd(tcpc,strCmd);
//最后 关闭与smtp 服务器的连接
tcpc.Close();
return true;
}
catch
{
return false;
}
}
void SenSmtpCmd(TcpClient tcpc,String strCmd)
{
byte[] arrCmd;
string strRet;
StreamReader sr;
Stream s;
s=tcpc.GetStream();
strCmd = strCmd + "\r\n";
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
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;
}
}
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;
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;
}
}
private void Disconnect()
{
string input = "quit" + "\r\n";
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" + "\r\n";
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);
}
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 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 + "\r\n";
}
return body;
}
private void DeleteMessage(int messagenumber)
{
Byte[] outbytes;
string input;
try
{
input = "dele " + messagenumber.ToString() + "\r\n";
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\r\n";
strCmd=strCmd+"From: " + strSend +"\r\n";
strCmd=strCmd+"To: " + strReceive +"\r\n";
strCmd=strCmd+"Subject: " + strSubject +"\r\n\r\n";
strCmd=strCmd + strContent +"\r\n\r\n";
SenSmtpCmd(tcpc,strCmd);
strCmd="\r\n.\r\n";
SenSmtpCmd(tcpc,strCmd);
//最后 关闭与smtp 服务器的连接
tcpc.Close();
return true;
}
catch
{
return false;
}
}
void SenSmtpCmd(TcpClient tcpc,String strCmd)
{
byte[] arrCmd;
string strRet;
StreamReader sr;
Stream s;
s=tcpc.GetStream();
strCmd = strCmd + "\r\n";
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;
}
}
}