这是前台需要的对象
<asp:TextBox runat="server" CssClass="noneinput" placeholder="请输入邮箱账号" onblur="email()"></asp:TextBox>
<asp:Button runat="server" Text="获取验证码" CssClass="zcnext am-fr am-btn-default" />
<asp:HiddenField runat="server" />//这个是存放时间
<asp:HiddenField runat="server" />//这个是用以判断验证码过期
这是js代码部分
<script>
var interval = 1000;
function ShowCountDown() {
var date = $("#<%=hfTimer.ClientID %>").val();//这个是获取前面存放时间的隐藏控件的值
var now = new Date();
var endDate = new Date(date);
if (now < endDate) {
var leftTime = endDate.getTime() - now.getTime();
var leftsecond = parseInt(leftTime / 1000);
//ar day1=parseInt(leftsecond/(24*60*60*6));
var day1 = Math.floor(leftsecond / (60 * 60 * 24));
var hour = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
var minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour * 3600) / 60);
var second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour * 3600 - minute * 60);
//document.getElementById("TimerH").innerHTML = day1 * 24 + hour;
//document.getElementById("TimerM").innerHTML = minute;
document.getElementById("miao").innerHTML = second;
} else {
document.getElementById("miao").innerHTML = "";//其时间过期酿成空
var ts = $("#<%=time.ClientID %>").val("Failure");//这个是用以判断验证码过期的隐藏控件的值
}
}
window.setInterval(function () { ShowCountDown(); }, interval);
</script>
这下面是后台代码
protected void Button2_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage(); //实例化一个email类
string email = Textemail.Text.Trim();
if(email!="")
{
msg.To.Add(email);
msg.CC.Add("[email protected]"); //抄送人地点
msg.From = new MailAddress("[email protected]", "崔小强"); //发送人地点
msg.Subject = "验证码"; //邮件标题
msg.SubjectEncoding = Encoding.UTF8; // 字符格局
msg.Body =suiji(); //邮件内容 这里调的一个要领返回一个四位验证码
Session["yan"] = msg.Body; //把邮件内容存到Session中
msg.BodyEncoding = Encoding.UTF8; //设置邮件内容的编码格局
SmtpClient client = new SmtpClient(); //用来传输简单邮件的smtp协议
client.Host = "smtp.qq.com"; //QQ的IP地点
client.Port = 587; //事务的端口
client.EnableSsl = true; //加密链接
client.Credentials = new NetworkCredential("[email protected]", "rrcntqllbhahjgcd"); //用于验证发件人的身份根据
client.Send(msg); //将邮件发送到smtp处事器,通报
DateTime timed = System.DateTime.Now;
DateTime times = timed.AddMinutes(1);//给当前时间加上一分钟
hfTimer.Value = times.ToString("yyyy/MM/dd HH:mm:ss");//给前台的隐藏控件赋值
time.Value = "";//给用于验证验证码过期的控件赋空
}