📄 mailbox.aspx.cs
字号:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Sockets;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Data.SqlClient;
public partial class MailBox : System.Web.UI.Page
{
System.Net.Sockets.TcpClient tcpC = new System.Net.Sockets.TcpClient();
DataSet ds = new DataSet("ds_dry");
DataTable dt = new DataTable("dt_dry");
public string From,Subject,Date;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string type = Request.QueryString["type"].ToString();
switch (type)
{
case "1":
if (Session["Server"] == null || Session["User"] == null || Session["Password"] == null)
{
Response.Redirect("default.aspx");
}
string host = Session["Server"].ToString();
string uname = Session["User"].ToString();
string pwd = Session["Password"].ToString();
ReadMail(host, uname, pwd);
// Bind_Mails();
break;
case "2":
Bind_SendMails();
break;
case "3":
Bind_preSendMails();
break;
case "4":
Bind_DeletedMails();
break;
}
}
}
//读取信箱邮件
private void ReadMail(string host, string user, string pass)
{
dt.Columns.Add(new DataColumn("From",typeof(string)));
dt.Columns.Add(new DataColumn("Subject", typeof(string)));
dt.Columns.Add(new DataColumn("Date", typeof(string)));
DataRow dr = dt.NewRow();
POP3 pop = new POP3(host, user, pass, 110);
// if (Int32.Parse(numMess) > 0)
if (pop.GetNumberOfNewMessages() > 0)
{
try
{
//lbl_msg.Text = "您的邮箱中共有:" + numMess + "封邮件";
lbl_msg.Text = "您的邮箱中共有:" + pop.GetNumberOfNewMessages() + "封邮件";
for (int i = 0; i < pop.GetNumberOfNewMessages(); i++)
{
System.Net.Mail.MailMessage msg = new MailMessage();
From = "";
Subject = "";
Date = "";
msg = pop.GetNewMessages(i);
From=Convert.ToString(msg.From);
Subject=msg.Subject;
Date = pop.Date;
dr = dt.NewRow();
dr["From"] = From;
dr["Subject"] = Subject;
dr["Date"] = Date;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception e)
{
lbl_msg1.Text = e.Message.ToString();
}
}
else
{
lbl_msg.Text = "您的邮箱中没有邮件";
}
// 关闭连接
}
//读取信箱邮件
//private void ReadMail(string host, string user, string pass)
//{
// try
// {
// POP3 pop = new POP3(host, user, pass, 110);
// lbl_msg1.Text = "登陆成功";
// lbl_msg.Text = "共有" + pop.GetNumberOfNewMessages() + "封邮件";
// for (int i = 0; i < pop.GetNumberOfNewMessages(); i++)
// {
// System.Net.Mail.MailMessage msg = new MailMessage();
// msg = pop.GetNewMessages(i);
// lbl_msg2.Text = lbl_msg2.Text + msg.From;
// lbl_msg2.Text = lbl_msg2.Text + msg.Subject;
// lbl_msg2.Text = lbl_msg2.Text + msg.Body;
// }
// }
// catch (Exception e)
// {
// lbl_msg1.Text = e.Message.ToString();
// }
//}
void Bind_Mails()
{
//读取数据库连接字符串
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
//创建数据库连接对象
SqlConnection myconn = new SqlConnection(settings);
//打开数据库连接
myconn.Open();
//创建数据库命令
SqlCommand mycmd = new SqlCommand("GetMails", myconn);
//数据库命令类型
mycmd.CommandType = CommandType.StoredProcedure;
//SqlDataReader读取数据到记录集后,会自动关闭数据库的连接
SqlDataReader result = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
gv_Maillist.DataSource = result;
gv_Maillist.DataBind();
}
void Bind_SendMails()
{
//读取数据库连接字符串
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
//创建数据库连接对象
SqlConnection myconn = new SqlConnection(settings);
//打开数据库连接
myconn.Open();
//创建数据库命令
SqlCommand mycmd = new SqlCommand("GetSendMail", myconn);
//数据库命令类型
mycmd.CommandType = CommandType.StoredProcedure;
//SqlDataReader读取数据到记录集后,会自动关闭数据库的连接
SqlDataReader result = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
gv_Maillist.DataSource = result;
gv_Maillist.DataBind();
}
void Bind_preSendMails()
{
//读取数据库连接字符串
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
//创建数据库连接对象
SqlConnection myconn = new SqlConnection(settings);
//打开数据库连接
myconn.Open();
//创建数据库命令
SqlCommand mycmd = new SqlCommand("GetpreSendMail", myconn);
//数据库命令类型
mycmd.CommandType = CommandType.StoredProcedure;
//SqlDataReader读取数据到记录集后,会自动关闭数据库的连接
SqlDataReader result = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
gv_Maillist.DataSource = result;
gv_Maillist.DataBind();
}
void Bind_DeletedMails()
{
//读取数据库连接字符串
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
//创建数据库连接对象
SqlConnection myconn = new SqlConnection(settings);
//打开数据库连接
myconn.Open();
//创建数据库命令
SqlCommand mycmd = new SqlCommand("GetDeletedMail", myconn);
//数据库命令类型
mycmd.CommandType = CommandType.StoredProcedure;
//SqlDataReader读取数据到记录集后,会自动关闭数据库的连接
SqlDataReader result = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
gv_Maillist.DataSource = result;
gv_Maillist.DataBind();
}
protected void gv_Maillist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//读取数据库连接字符串
string settings = Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);
//创建数据库连接
SqlConnection myconn = new SqlConnection(settings);
//打开数据库连接
myconn.Open();
//int id = (int)gv_Maillist.DataKeys[e.RowIndex].Value;
//string strsql = "update Mails Set IsDeleted=1 Where MailID=" + id;
// //创建数据库命令
// SqlCommand mycmd = new SqlCommand(strsql, myconn);
// //进行数据库操作
// mycmd.ExecuteNonQuery();
string type = Request.QueryString["type"].ToString();
if (type == "2")
{
int id = (int)gv_Maillist.DataKeys[e.RowIndex].Value;
string strsql = "update Mails Set IsDeleted=1 Where MailID=" + id;
//创建数据库命令
SqlCommand mycmd = new SqlCommand(strsql, myconn);
//进行数据库操作
mycmd.ExecuteNonQuery();
Response.Redirect("MailBox.aspx?type=2");
}
if (type == "1")
{
Response.Redirect("MailBox.aspx?type=1");
}
if (type == "3")
{
int mailid = (int)gv_Maillist.DataKeys[e.RowIndex].Value;
string strsqlDelete = "delete from Mails where MailID=" + mailid;
SqlCommand mycmdD = new SqlCommand(strsqlDelete, myconn);
mycmdD.ExecuteNonQuery();
Response.Redirect("MailBox.aspx?type=3");
}
if (type == "4")
{
int mailid = (int)gv_Maillist.DataKeys[e.RowIndex].Value;
string strsqlDelete = "delete from Mails where MailID=" + mailid;
SqlCommand mycmdD = new SqlCommand(strsqlDelete, myconn);
mycmdD.ExecuteNonQuery();
Response.Redirect("MailBox.aspx?type=4");
}
//关闭数据库连接
myconn.Close();
}
protected void gv_Maillist_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
dt.Columns.Add(new DataColumn("From", typeof(string)));
dt.Columns.Add(new DataColumn("Subject", typeof(string)));
dt.Columns.Add(new DataColumn("Date", typeof(string)));
DataRow dr = dt.NewRow();
string host = Session["Server"].ToString();
string uname = Session["User"].ToString();
string pwd = Session["Password"].ToString();
POP3 pop = new POP3(host, uname, pwd, 110);
if (pop.GetNumberOfNewMessages() > 0)
{
try
{
//lbl_msg.Text = "您的邮箱中共有:" + numMess + "封邮件";
lbl_msg.Text = "您的邮箱中共有:" + pop.GetNumberOfNewMessages() + "封邮件";
for (int i = 0; i < pop.GetNumberOfNewMessages(); i++)
{
System.Net.Mail.MailMessage msg = new MailMessage();
From = "";
Subject = "";
Date = "";
msg = pop.GetNewMessages(i);
From = Convert.ToString(msg.From);
Subject = msg.Subject;
Date = pop.Date;
dr = dt.NewRow();
dr["From"] = From;
dr["Subject"] = Subject;
dr["Date"] = Date;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception x)
{
lbl_msg1.Text = x.Message.ToString();
}
}
else
{
lbl_msg.Text = "您的邮箱中没有邮件";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -