using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
namespace _13._1._4练习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.Subject = this.textBox2.Text; //标题
msg.Body = this.textBox3.Text; //内容
msg.From=new MailAddress("duan_linlin@163.com"); //邮件来自哪
msg.To.Add(this.textBox1.Text);
SmtpClient client=new SmtpClient(); //允许传输协议
client.Host = "smtp.163.com"; //发件方服务器地址
client.Port = 25; //发件方端口
NetworkCredential credential = new NetworkCredential();
credential.UserName = "Missdandanzhang@163.com";
credential.Password = "dandanlove910822";
client.Credentials = credential; //说明证书要给代理证书credential
Attachment att = new Attachment(this.textBox4.Text);
msg.Attachments.Add(att);
client.Send(msg);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
this.textBox4.Text = this.openFileDialog1.FileName;
}
}
}
}
2.实现用户注册时,向其油箱发送激活码邮件,并进行状态处理。
注册页面:
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td>
用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
邮箱:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Xοnclick="Button1_Click" Text="注册" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
namespace 激活验证
{
public partial class region : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void sendMail(string Email,string activeCode)
{
MailMessage msg = new MailMessage();
msg.From=new MailAddress("duan_linlin@163.com"); //邮件来自哪
msg.To.Add(Email);
msg.Subject = "请激活注册!";
StringBuilder contentBuilder=new StringBuilder();
contentBuilder.Append("请单击一下连接完成激活!");
contentBuilder.Append("<a href='http://localhost:5199/CheckActiveCode.aspx?activecode="+activeCode+"&id=3'>激活</a>");
msg.Body = contentBuilder.ToString();
msg.IsBodyHtml=true;
SmtpClient client = new SmtpClient(); //允许传输协议
client.Host = "smtp.163.com"; //发件方服务器地址
client.Port = 25; //发件方端口
NetworkCredential credential = new NetworkCredential();
credential.UserName = "Missdandanzhang@163.com";
credential.Password = "dandanlove910822";
client.Credentials = credential; //说明证书要给代理证书credential
client.Send(msg);
}
protected void Button1_Click(object sender, EventArgs e)
{
string userName = TextBox1.Text.Trim();
string password = TextBox2.Text.Trim();
string Email = TextBox3.Text.Trim();
string activeCode = Guid.NewGuid().ToString().Substring(0, 8); //生成激活码
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
int number;
using (SqlConnection con=new SqlConnection(conStr))
{
string sql = "insert into T_Users (UserName,Password,Email,Active,ActiveCode) values(@username,@password,@Email,@active,@activecode)";
SqlParameter[] prams = new SqlParameter[]{
new SqlParameter("@username",userName),
new SqlParameter("@password",password),
new SqlParameter("@Email",Email),
new SqlParameter("@active",false),
new SqlParameter("@activecode",activeCode)
};
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddRange(prams);
number= cmd.ExecuteNonQuery();
}
}
if (number>0)
{
sendMail( Email, activeCode);//给注册用户发邮件
Response.Redirect("regionMessage.aspx");
}
else
{
Response.Write("注册失败");
}
}
}
}
CheckActiveCode后台:
namespace 激活验证
{
public partial class CheckActiveCode : System.Web.UI.Page
{
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
int number;
protected void Page_Load(object sender, EventArgs e)
{
//去除参数id
int id = Convert.ToInt32(Request["id"]);
string activeCode = Request["ActiveCode"].ToString();
//判断id为id的记录值是否存在
//连接数据库
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select count(*) from T_Users where id=@id";
using (SqlCommand cmd=new SqlCommand(sql,con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
number = Convert.ToInt32(cmd.ExecuteScalar());
}
}
if ( number > 0)
{
//如果该用户存在取出其ActiveCode字段进行比较,如果一样,把Active字段修改为true。
//连接数据库
string AC;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select ActiveCode from T_Users where id=@id";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
AC =cmd.ExecuteScalar().ToString();
}
}
if (activeCode == AC)
{
Response.Write("激活成功!");
//连接数据库
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "update T_Users set Active=1 where id=@id";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
number =Convert.ToInt32(cmd.ExecuteScalar());
}
}
}
else
{
Response.Write("用户存在,但是激活码错误!");
}
}
else
{
Response.Write("用户不存在!注册失败!");
}
}
}
}
regionMessage前台:
<body>
<form id="form1" runat="server">
<div>
<h2> 恭喜你注册成功!</h2>
</div>
</form>
</body>
</html>