📄 default.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
public partial class Default2 : 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;
protected void Page_Load(object sender, EventArgs e)
{
}
private string SendPopCmd(TcpClient tcpc,string strCmd)
{
Byte[] arrCmd;
string strRet;
Stream s;
s=tcpc.GetStream();
strCmd = strCmd + "\r\n";
arrCmd= Encoding.Default.GetBytes(strCmd.ToCharArray());
s=tcpc.GetStream();
s.Write(arrCmd, 0, strCmd.Length);
strRet=sr.ReadLine();
return strRet;
}
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[] 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;
}
}
private string JudgeString(string strCheck)
{
if(strCheck.Substring(0,3)!="+OK")
{
return "-ERR";
}
else
return "+OK";
}
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());
}
Response.Write("\n<BR>");
Response.Write("<font color=red>\n<BR>");
arrRet[0] = "+OK";
while (sr.Peek() != 46)
{
strRet = sr.ReadLine();
arrTemp = strRet.Split(":".ToCharArray());
if (strRet == "")
strBody = true; //现在开始接收 Body 的信息
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;
}
protected void Button2_Click(object sender, EventArgs e)//连接邮箱
{
string user = TextBox2.Text.ToString();
string pass = TextBox3.Text.ToString();
string[] arrRet;
pwd = pass;
popserver = TextBox1.Text.ToString();
tcpc = new TcpClient();
try
{
tcpc.Connect(popserver,110);
sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
sr.ReadLine();
strRet = Logon(tcpc, user, pass);
if (JudgeString(strRet) != "+OK")
{
Response.Write("对不起,没有这个用户/密码 不匹配");
return;
}
arrRet = StaticMailBox(tcpc);
if (arrRet[0] != "+OK")
{
Response.Write("出错了!");
return;
}
Page.RegisterStartupScript("","<script>alert('当前的用户:" + user + "的信箱中共有" + arrRet[1] + "封,共占" + arrRet[2] + "Byte"+"')</script>");
k = Convert.ToInt32(arrRet[1]);
Button2.Enabled = false;
Button1.Enabled = true;
Button3.Enabled = true;
TextBox1.Enabled = false;
TextBox2.Enabled = false;
TextBox3.Enabled = false;
}
catch (Exception ex)
{
Response.Write("Could not connect to server!");
}
}
public static string Base64Decode(string str)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(str));
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox4.Text != "")
{
if (Convert.ToInt32(TextBox4.Text) > k || Convert.ToInt32(TextBox4.Text) <= 0)
{
Page.RegisterStartupScript("", "<script>alert('输入的索引错误')</script>");
}
else
{
string[] arrRets;
arrRets = PopMail(tcpc, Convert.ToInt32(TextBox4.Text));
TextBox5.Text = "当前是第" + TextBox4.Text + "封信" + "\n" + "邮件日期:" + arrRets[1] + "\n" + "发信人:" + arrRets[2] + "\n" + "收信人:" + arrRets[3] + "\n" + "邮件主题:" +Base64Decode(arrRets[4]) + "\n" + "邮件内容:" +Base64Decode(arrRets[5]) + "\n";
}
}
else
{
Page.RegisterStartupScript("","<script>alert('邮件索引错误')</script>");
}
}
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
tcpc.Close();
TextBox1.Enabled = true;
TextBox2.Enabled = true;
TextBox3.Enabled = true;
Button2.Enabled = true;
Button3.Enabled = false;
Button1.Enabled = false;
TextBox5.Text = "";
TextBox4.Text = "";
TextBox3.Text = "";
TextBox2.Text = "";
TextBox1.Text = "";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -