using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
public static string pwd;
public static int k;
public static TcpClient tcpc;
public static string popserver;
public static StreamReader sr;
public static string strRet;
public static string[] arrRet;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string user = TextBox2.Text.ToString();
string pass = TextBox3.Text.ToString();
pwd = pass;
popserver = TextBox1.Text.ToString();
tcpc = new TcpClient();
try
{
tcpc.Connect(popserver, 110);
sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
Response.Write(sr.ReadLine() + "");
strRet = logon(tcpc, user, pass);
if (JudgeString(strRet) != "+OK")
{
Response.Write("没有这个用户名/密码 不匹配");
return;
}
arrRet = StaticMailBox(tcpc);
if (arrRet[0] != "+OK")
{
Response.Write("出错了");
return;
}
Response.Write("当前用户" + user + "的信箱中共有" + arrRet[1] + "封,共占" + arrRet[2] + "Byte");
k = Convert.ToInt32(arrRet[1]);
}
catch (Exception ex) {
Response.Write("连接
服务器失败");
}
}
private string logon(TcpClient tcpc, string user, string pass) {
string strRet;
strRet = SendPopCmd(tcpc, "user " + user);
strRet = SendPopCmd(tcpc, "pass " + pass);
return strRet;
}
private string SendPopCmd(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 strRet;
}
private string JudgeString(string strCheck) {
if (strCheck.Substring(0, 3) != "+OK")
{
return "-ERR";
}
else {
return "+OK";
}
}
private string[] StaticMailBox(TcpClient tcpc) {
string strRet;
strRet = SendPopCmd(tcpc, "stat");
if (JudgeString(strRet) != "+OK")
{
return "-ERR -ERR".Split("".ToCharArray());
}
else {
string[] arrRet = strRet.Split("".ToCharArray());
return arrRet;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Convert.ToInt32(TextBox4.Text) > k || Convert.ToInt32(TextBox4.Text) <= 0) {
Page.RegisterClientScriptBlock("","");
}else{
string[] arrRets;
arrRets=PopMail(tcpc,Convert.ToInt32(TextBox4.Text));
TextBox5.Text="当前是第"+TextBox4.Text+"封信"+"\n"+"邮件日期:"+arrRets[1]+"\n"+"发信人:"+arrRets[2]+"\n"+"收信人:"+arrRets[3]+"\n"+"邮件主题"+arrRets[4]+"\n"+"邮件内容:"+arrRets[5]+"\n";
Literal1.Text = "当前是第" + TextBox4.Text + "封信" + "\n" + "邮件日期:" + arrRets[1] + "\n" + "发信人:" + arrRets[2] + "\n" + "收信人:" + arrRets[3] + "\n" + "邮件主题" + arrRets[4] + "\n" + "邮件内容:" + arrRets[5] + "\n";
for (int j = 0; j < arrRets.Length; j++) {
Response.Write(arrRets[j]);
}
}
}
private string[] PopMail(TcpClient tcpc, int i) {
string strRet;
string[] arrRet = new string[20];
bool strBody = false;
string[] arrTemp;
strRet = SendPopCmd(tcpc, "retr " + i.ToString());
if (JudgeString(strRet) != "+OK") {
return "-ERR ERR".Split("".ToCharArray());
}
StreamReader sr;
sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
Response.Write("\n
");
Response.Write("\n
");
arrRet[0] = "+OK";
while (sr.Peek() != 46) {
strRet = sr.ReadLine();
arrTemp = strRet.Split(":".ToCharArray());
if (strRet == "")
strBody = true;
if (arrTemp[0] == "Date")
arrRet[1] = arrTemp[1];
if (arrTemp[0]=="From")
arrRet[2]=(arrTemp[1].Substring(arrTemp[1].LastIndexOf("<")+1)).Replace(">","");
if (arrTemp[0]=="To")
arrRet[3]=(arrTemp[1].Substring(arrTemp[1].LastIndexOf("<")+1)).Replace(">","");
if (arrTemp[0]=="Subject")
arrRet[4]=arrTemp[1].ToString();
if(strBody)
arrRet[5]=arrRet[5]+strRet+"\n";
}
return arrRet;
}
}
当前名称:pop3接收邮件(asp.net)
当前路径:
http://wjwzjz.com/article/ippioh.html