伪邮件系统实验代码
登录代码
protected void inload_Click(object sender, EventArgs e)
{
String conStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
String selStr = "select * from emailuser where emailuser='" +inusername.Text.Trim()+ "'";
String psw = inpassword.Text.Trim();
SqlConnection myConn = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter(selStr, myConn);
//SqlCommand com = new SqlCommand(selStr);
//SqlDataAdapter da = new SqlDataAdapter();
//da.SelectCommand = com;
myConn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "emailuser");
if (ds.Tables["emailuser"].Rows.Count == 1)
{
if (ds.Tables["emailuser"].Rows[0]["password"].ToString() == psw)
{
Session["login.aspx"] = inusername.Text;
Response.Redirect("mailsys.aspx");
}
else
Response.Write("密码错误!");
}
else
{
Response.Write("用户不存在!");
}
myConn.Close();
}
注册代码:
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.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Button2_Click(object sender, EventArgs e)
{
TBuser.Text = "";
TBpassword1.Text = "";
TBpassword2.Text = "";
TBtelephone.Text = "";
TByear.Text = "";
TBmonth.Text = "";
TBday.Text = "";
TBemail.Text = "";
TBquestion.Text = "";
TBrespon.Text = "";
DDsex.Text = "男";
}
protected void Button1_Click1(object sender, EventArgs e)
{
String conStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
String userName = TBuser.Text.Trim();
String selStr = "select * from emailuser where emailuser='" + userName + "'";
SqlConnection myConn = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter(selStr, myConn);
myConn.Open();
DataSet ds = new DataSet();
// da.Fill(ds, "emailuser");
//if (ds.Tables["emailuser"].Rows.Count >= 1)
//{
// Response.Write("用户名已被使用!");
//}
//else
{
String values = "'" + userName + "',";
values = values + "'" + TBpassword2.Text.Trim() + "',";
values = values + "'" + DateTime.Now.ToShortDateString() + "',";
values = values + "'" + TBtelephone.Text.Trim() + "',";
values = values + "'" + DDsex.SelectedValue + "',";
values = values + "'" + TByear.Text.Trim() + "-" + TBmonth.Text.Trim() + "-" +
TBday.Text.Trim() + "',";
values = values + "'" + TBemail.Text.Trim() + "',";
values = values + "'" + TBquestion.Text.Trim() + "',";
values = values + "'" + TBrespon.Text.Trim() + "'";
Label1.Text = values;
String insStr = "insert into emailuser values(" + values + ")";
SqlCommand myComm = new SqlCommand(insStr,myConn);
myComm.Connection = myConn;
myComm.ExecuteReader();
Response.Write("注册成功");
Response.Redirect("mailsys.aspx");
}
myConn.Close();
}
protected void usertest(object sender, EventArgs e)
{
String conStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
String userName = TBuser.Text.Trim();
String selStr = "select * from emailuser where emailuser='" + userName + "'";
SqlConnection myConn = new SqlConnection(conStr);
SqlDataAdapter da = new SqlDataAdapter(selStr, myConn);
myConn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "emailuser");
if (ds.Tables["emailuser"].Rows.Count >= 1)
{
Response.Write("用户名已被使用@!");
}
else
Response.Write("用户名可以使用");
myConn.Close();
}
}
写信代码
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.Data.SqlClient;
using System.IO;
public partial class sendmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = Convert.ToString(Session["login.aspx"]);
}
protected void Button1_Click(object sender, EventArgs e)
{
String s_fn;
s_fn = FileUpload1.PostedFile.FileName;
String conStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
SqlConnection myConn = new SqlConnection(conStr);
//如何调用其他login.aspx窗体中的 inusername.text中的资料
String insSql = "insert into
email(s_email,d_email,subject,content,email_flag,sendtime,att_flag) values(";
insSql = insSql + "'" + Session["login.aspx"] + "',";
insSql = insSql + "'" + TBemailto.Text.Trim() + "',";
insSql = insSql + "'" + TBsubject.Text.Trim() + "',";
insSql = insSql + "'" + TBcontent.Text.Trim() + "',";
insSql = insSql + "1,";
insSql = insSql + "'" + DateTime.Now.ToShortDateString().ToString() + "',";
if(s_fn=="")
insSql = insSql + "'0')";//先处理成无附件,后期要修改
else
insSql = insSql + 1 + " ) ";
Label1.Text = insSql;
SqlCommand insert = new SqlCommand(insSql, myConn);
myConn.Open();
insert.ExecuteReader();//
email增加一条记录
myConn.Close();
// Response.Redirect("send_success.aspx");
if (s_fn != "")
{//有附件要上传附件到服务器,并为表email_att插入一条记录,更新att_number id的值
Label1.Text = "有附件要上传附件到服务器";
String d_fileFolder = Server.MapPath("") + "\\mail_att";
String d_fn;
int att_number;
String selStr_attNumber = "select id from att_number";
SqlDataAdapter da = new SqlDataAdapter(selStr_attNumber, myConn);
DataSet ds = new DataSet();
myConn.Open();
da.Fill(ds, "att_number");
att_number = int.Parse(ds.Tables["att_number"].Rows[0]["id"].ToString());
att_number++;
ds.Tables["att_number"].Rows[0]["id"] = att_number;
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.UpdateCommand = cb.GetUpdateCommand();
da.Update(ds, "att_number");//更新att_number id的值
if (!Directory.Exists(d_fileFolder))
Directory.CreateDirectory(d_fileFolder);
s_fn = Path.GetFileName(s_fn);
d_fn = d_fileFolder + "\\" + s_fn ;
FileUpload1.SaveAs(d_fn);//上传附件到服务器
Label3.Text = d_fn;
String selStr_emailId = "select max(id) maxId from email";
insert.CommandText = selStr_emailId;
da.SelectCommand = insert;
da.Fill(ds, "emailId");
String emailId = ds.Tables["emailId"].Rows[0]["maxId"].ToString();
String insSql_att = "insert into email_att(email_id,s_attach,d_attach) values(";
insSql_att = insSql_att + "'" + emailId + "',";
insSql_att = insSql_att + "'" + s_fn + "',";
insSql_att = insSql_att + "'" + d_fn + "')";
// Label1.Text = insSql_att;
insert.CommandText = insSql_att;
insert.ExecuteReader();
myConn.Close();
}
else
{
int att_number;
String selStr_attNumber = "select id from att_number";
SqlDataAdapter da = new SqlDataAdapter(selStr_attNumber, myConn);
DataSet ds = new DataSet();
myConn.Open();
da.Fill(ds, "att_number");
att_number = int.Parse(ds.Tables["att_number"].Rows[0]["id"].ToString());
att_number++;
ds.Tables["att_number"].Rows[0]["id"] = att_number;
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.UpdateCommand = cb.GetUpdateCommand();
da.Update(ds, "att_number");//更新att_number id的值
String selStr_emailId = "select max(id) maxId from email";
insert.CommandText = selStr_emailId;
da.SelectCommand = insert;
da.Fill(ds, "emailId");
String emailId = ds.Tables["emailId"].Rows[0]["maxId"].ToString();
String insSql_att = "insert into email_att(email_id,s_attach,d_attach) values(";
insSql_att = insSql_att + "'" + emailId + "',";
insSql_att = insSql_att + "'" + null + "',";
insSql_att = insSql_att + "'" + null + "')";
// Label1.Text = insSql_att;
insert.CommandText = insSql_att;
insert.ExecuteReader();
myConn.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TBcontent.Text = "";
TBsubject.Text = "";
TBemailto.Text = "";
}
}
收件功能
收件代码:
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.Data.SqlClient;
using System.IO;
public partial class inbox : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Convert.ToString(Session["login.aspx"]);
String connStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
String updateStr = "update email set email_flag=0 where id='" + Request.QueryString["id"]
+ "'";
SqlConnection myConn = new SqlConnection(connStr);
SqlCommand myComm = new SqlCommand(updateStr, myConn);
myConn.Open();
myComm.ExecuteReader();
myConn.Close();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Gridview1_PageIndexChang(object sender, GridViewPageEventArgs e)
{
CheckBox2.Checked = false;
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
String connStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
SqlConnection myConn = new SqlConnection(connStr);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Image ima_email = (Image)GridView1.Rows[i].FindControl("Image1");
Image ima_att = (Image)GridView1.Rows[i].FindControl("Image2");
int id = int.Parse(GridView1.Rows[i].Cells[1].Text);
String selStr = "select email_flag,att_flag from email where id=" + id;
SqlDataAdapter da = new SqlDataAdapter(selStr, myConn);
DataSet ds = new DataSet();
myConn.Open();
da.Fill(ds, "email");
int email_flag = int.Parse(ds.Tables["email"].Rows[0]["email_flag"].ToString());
String att_flag = ds.Tables["email"].Rows[0]["att_flag"].ToString().Trim();
if (email_flag == 1)
{
ima_email.Visible = true;
}
else ima_email.Visible = false;
if (att_flag == "1")
ima_att.Visible = true;
else
ima_att.Visible = false;
myConn.Close();
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
int id;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
id = int.Parse(GridView1.Rows[i].Cells[1].Text.ToString());
if (cb.Checked)
deleEmail(id);
}
GridView1.DataBind();
}
protected void deleEmail(int id)
{
String connStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
SqlConnection myConn = new SqlConnection(connStr);
String sel_d_attach = "select s_attach from email_att where email_id=" + id;
Label1.Text = sel_d_attach;
SqlDataAdapter da = new SqlDataAdapter(sel_d_attach, myConn);
DataSet ds = new DataSet();
myConn.Open();
da.Fill(ds, "attach");
String s_attach = ds.Tables["attach"].Rows[0]["s_attach"].ToString();
String d_filefold = Server.MapPath("") + "\\mail_att";
if (File.Exists(d_filefold + "\\" + s_attach))
File.Delete(d_filefold + "\\" + s_attach);
myConn.Close();
String delStr2 = "delete from email_att where email_id=" + id;
SqlCommand myComm = new SqlCommand(delStr2, myConn);
myConn.Open();
myComm.ExecuteReader();
myConn.Close();
String delStr = "delete from email where id=" + id;
myConn.Open();
myComm = new SqlCommand(delStr, myConn);
myComm.ExecuteReader();
myConn.Close();
String d_fileFolder = Server.MapPath("") + "\\mail_att";
String d_fn;
if (Directory.Exists(d_fileFolder))
{
String selStr_attNumber = "select id from att_number";
SqlDataAdapter da = new SqlDataAdapter(selStr_attNumber, myConn);
DataSet ds = new DataSet();
da.Fill(ds, "email_att");
att_number = (ds.Tables["att_number"].Rows[0]["id"].ToString());
att_number++;
ds.Tables["att_number"].Rows[0]["id"] = att_number;
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.UpdateCommand = cb.GetUpdateCommand();
da.Update(ds, "att_number");//更新att_number id的值
s_fn = Path.GetFileName(s_fn);
d_fn = d_fileFolder + "\\" + att_number + s_fn;// +Path.GetExtension(s_fn);
FileUpload filedel;//删除服务器上的附件服
}
*/
}
protected void Button2_Click1(object sender, EventArgs e)
{
int id;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
id = int.Parse(GridView1.Rows[i].Cells[1].Text.ToString());
if (cb.Checked)
toGarbage(id);
}
GridView1.DataBind();
}
protected void toGarbage(int id)
{
String connStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
String updateStr = "update email set email_flag=2 where id=" + id;
SqlConnection myConn = new SqlConnection(connStr);
SqlCommand myComm = new SqlCommand(updateStr, myConn);
myConn.Open();
myComm.ExecuteReader();
myConn.Close();
}
protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox2.Checked)
cb.Checked = true;
else
cb.Checked = false;
}
}
}
删除邮件功能
邮件放入垃圾箱
显示邮件
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.Data.SqlClient;
using System.IO;
public partial class unread : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Convert.ToString(Session["login.aspx"]);
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void CheckBox2_CheckedChanged1(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox2.Checked)
cb.Checked = true;
else
cb.Checked = false;
}
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
String connStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
SqlConnection myConn = new SqlConnection(connStr);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Image ima_email = (Image)GridView1.Rows[i].FindControl("Image1");
Image ima_att = (Image)GridView1.Rows[i].FindControl("Image2");
int id = int.Parse(GridView1.Rows[i].Cells[1].Text);
String selStr = "select email_flag,att_flag from email where id=" + id;
SqlDataAdapter da = new SqlDataAdapter(selStr, myConn);
DataSet ds = new DataSet();
myConn.Open();
da.Fill(ds, "email");
int email_flag = int.Parse(ds.Tables["email"].Rows[0]["email_flag"].ToString());
String att_flag = ds.Tables["email"].Rows[0]["att_flag"].ToString().Trim();
if (email_flag == 1)
{
ima_email.Visible = true;
}
else ima_email.Visible = false;
if (att_flag == "1")
ima_att.Visible = true;
else
ima_att.Visible = false;
myConn.Close();
}
}
protected void Gridview1_PageIndexChang(object sender, GridViewPageEventArgs e)
{
CheckBox2.Checked = false;
}
protected void Button1_Click1(object sender, EventArgs e)
{
int id;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
id = int.Parse(GridView1.Rows[i].Cells[1].Text.ToString());
if (cb.Checked)
deleEmail(id);
}
GridView1.DataBind();
}
protected void deleEmail(int id)
{
String connStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated
Security=True";
SqlConnection myConn = new SqlConnection(connStr);
String sel_d_attach = "select s_attach from email_att where email_id=" + id;
// Label1.Text = sel_d_attach;
SqlDataAdapter da = new SqlDataAdapter(sel_d_attach, myConn);
DataSet ds = new DataSet();
myConn.Open();
da.Fill(ds, "attach");
String s_attach = ds.Tables["attach"].Rows[0]["s_attach"].ToString();
String d_filefold = Server.MapPath("") + "\\mail_att";
if (File.Exists(d_filefold + "\\" + s_attach))
File.Delete(d_filefold + "\\" + s_attach);
myConn.Close();
String delStr2 = "delete from email_att where email_id=" + id;
SqlCommand myComm = new SqlCommand(delStr2, myConn);
myConn.Open();
myComm.ExecuteReader();
myConn.Close();
String delStr = "delete from email where id=" + id;
myConn.Open();
myComm = new SqlCommand(delStr, myConn);
myComm.ExecuteReader();
myConn.Close();
}
protected void Button2_Click1(object sender, EventArgs e)
{
int id;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
id = int.Parse(GridView1.Rows[i].Cells[1].Text.ToString());
if (cb.Checked)
toGarbage(id);
}
GridView1.DataBind();
}
protected void toGarbage(int id)
{
String connStr = "Data Source=WILDWOLF;Initial Catalog=mailsys;Integrated Security=True";
String updateStr = "update email set email_flag=2 where id=" + id;
SqlConnection myConn = new SqlConnection(connStr);
SqlCommand myComm = new SqlCommand(updateStr, myConn);
myConn.Open();
myComm.ExecuteReader();
myConn.Close();
}
}
用户资料修改