1.制作客户端邮件发送系统(winform版)实现用户注册时,向其油箱发送激活码邮件,并进行状态处理。

1.制作客户端邮件发送系统(winform版)
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>
&nbsp;</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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值