JS验证码邮件
js
var time = 30;
var canSend = true; function f5() {
if (canSend) {//判断是否要ajax发送刷新验证码 验证码在后台刷新
//alert("验证");
send();
}
if (time == 0) {//时间为0是button设置可用 并且设置可发送
$('#vbtn').attr("disabled", false);
$("#vbtn").text("重新发送")
time = 30;
canSend = true;
} else {//时间不等于0时button设置不可点击 并且不能发送
canSend = false;
$('#vbtn').attr("disabled", true);
time--;
setTimeout(function () {
$("#vbtn").text(time + "秒后可重新发送")
f5();
}, 1000)
}
} function send() {
$.ajax({
type : "post",
url : getRootPath() + "/sendMail",
data : {
"mail" : $("#mail").val()
},
success : function(result) {
alert("send success");
}
})
} // 得到绝对路径
function getRootPath() {
// 获取当前网址,如: http://localhost:9527/zdss-web/login/login.do
var curWwwPath = window.document.location.href;
// console.log("当前网址:" + curWwwPath); // 获取主机地址之后的目录,如:zdss-web/login/login.do
var pathName = window.document.location.pathname;
// console.log("当前路径:" + pathName); var pos = curWwwPath.indexOf(pathName);
// console.log("路径位置:" + pos); // 获取主机地址,如: http://localhost:9527
var localhostPath = curWwwPath.substring(0, pos);
console.log("当前主机地址:" + localhostPath); // 获取带"/"的项目名,如:/zdss-web
var projectName = pathName
.substring(0, pathName.substr(1).indexOf('/') + 1);
console.log("当前项目名称:" + projectName);
console.log(localhostPath + projectName);
return localhostPath + projectName;
}
/**
* Created by Administrator on 2017/10/30.
*/
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JsMail</title>
</head>
<body>
<body>
<p>输入邮箱</p><input type="text" id="mail"/>
<button id="vbtn" onclick="f5()">发送</button>
</body> <script src="js/jquery-3.2.1.min.js"></script>
<script src="js/F5VerificationCode.js"></script>
</body>
</html>
java 需导入javamail jar包
package com.acm.web; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.UUID; /**
* Created by Administrator on 2017/10/30.
*/
@Controller
public class MailController { @ResponseBody
@RequestMapping("sendMail")
public String SendMail(String mail) throws MessagingException { UUID uuid = UUID.randomUUID();
String code = uuid.toString().substring(0,6);
System.out.println(code); Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp"); // 连接协议
properties.put("mail.smtp.host", "smtp.qq.com"); // 主机名
properties.put("mail.smtp.port", 465); // 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true"); // 设置是否使用ssl安全连接 (一般都使用)
properties.put("mail.debug", "true"); // 设置是否显示debug信息 true 会在控制台显示相关信息
// 得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress("发件邮箱")); // 设置收件人地址
message.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress[]{new InternetAddress(mail)});
// 设置邮件标题
message.setSubject("验证码邮件");
// 设置邮件内容
message.setContent("验证码为" + code, "text/html;Charset=UTF-8");
// 得到邮差对象
Transport transport = session.getTransport();
// 连接自己的邮箱账户
transport.connect("发件邮箱", password); // password 为在qq邮箱内的到的授权码
// 发送邮件
transport.sendMessage(message, message.getAllRecipients()); return null;
} }
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 扫描web相关的bean -->
<context:component-scan base-package="com.acm.web" /> <!--两个标准配置 -->
<!-- 将springmvc不能处理的请求交给tomcat -->
<mvc:default-servlet-handler/>
<!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 -->
<mvc:annotation-driven/> </beans>
JS验证码邮件的更多相关文章
- js 验证码 倒计时60秒
js 验证码 倒计时60秒 <input type="button" id="btn" value="免费获取验证码" /> & ...
- js验证码有效时间倒计时
js验证码有效时间倒计时 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...
- 纯js验证码
纯js验证码 <!DOCTYPE html> <html> <head> <title>纯js验证码</title> </head&g ...
- Node.js定时邮件的那些事儿
近开发一个项目,需要在Node.js程序里实现定期给管理员发邮件的功能. 笔者平时只会在Web界面收发邮件.对邮件的原理完全不懂(可能大学教过,然而全忘了),直到要解决这个问题.请教了几个业务的同事, ...
- easyui page添加文本,js验证码
onLoadSuccess: function (db) { //db是后台数据的返回结果集 $.ajax({ url: "AjaxSource/Buex.ashx", data: ...
- js 验证码倒计时
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- angular.js 验证码注册登录
css部分 header{ height: 50px; line-height: 50px; display: flex; } .callback{ text-align: left; display ...
- node.js发邮件
在node上使用第三方类库(nodemailer)发邮件是一件很esay的事情:) app.js 以QQ邮箱为例 var nodemailer = require('nodemailer'); v ...
- 项目一:第八天 1、前台系统导入 实现客户注册 发验证码,邮件 springdata-redis存储数据 3、实现客户登陆
1 前台系统客户注册功能 页面:signup.html 1.1 验证手机号是否注册(邮箱同样) 1. 使用Jquery-validate插件进行相关校验,使用校验规则 <input type=& ...
随机推荐
- 使用睿云智合开源 Breeze 工具部署 Kubernetes v1.12.3 高可用集群
一.Breeze简介 Breeze 项目是深圳睿云智合所开源的Kubernetes 图形化部署工具,大大简化了Kubernetes 部署的步骤,其最大亮点在于支持全离线环境的部署,且不需要FQ获取 G ...
- WHERE 子句操作符
操作符(operator) 用来联结或改变WHERE子句中得子句的关键字,也称为逻辑操作符(logical operator): 操作符 说 明 = 等于 <> 不等于 != 不等于 & ...
- Spring Bean生命周期详解
对象生命周期:创建(实例化----初始化)---使用----销毁,而在Spring中,Bean对象周期当然遵从这一过程,但是Spring提供了许多对外接口,允许开发者对三个过程(实例化.初始化.销毁) ...
- 【Linux】【Jenkins】邮件发送失败的问题javax.mail.MessagingException: Could not connect to SMTP host:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.126.com,port:25 解决方案: 之前用的是126邮箱 ...
- Java面试——线程池
1.类比介绍 假如有一个工厂,工厂里面有10个工人,每个工人同时只能做一件任务. 因此只要当10个工人中有工人是空闲的,来了任务就分配给空闲的工人做: 当10个工人都有任务在做时,如果还来了任务,就把 ...
- leetcode23
public class Solution { public ListNode MergeKLists(ListNode[] lists) { var ary = new List<int> ...
- virtual 函数只有在用指针或引用的方式访问,才会导致多态。
只有用指针和引用,才会动态绑定.才会在运行时候从虚表中找对应的成员函数. 如果只是用.访问成员函数,是静态绑定,在编译时刻就固定好的. 另外,父类的虚函数,子类不管加不加virtual关键字,都是虚函 ...
- js:捕获冒泡和事件委托
一.事件流(捕获,冒泡) 事件流:指从页面中接收事件的顺序,有冒泡流和捕获流. 当页面中发生某种事件(比如鼠标点击,鼠标滑过等)时,毫无疑问子元素和父元素都会接收到该事件,可具体顺序是怎样的呢?冒 ...
- python多线程简单爬虫
爬虫本质就是将网站或者接口的数据经过筛选后按需求保存 这里实现一个简单爬虫仅供参考 import requests import bs4 import threading import queue i ...
- Unity3D AssetBundle相关
Unity3D AssetBundle相关 首先,先看一下原理吧 Unity3D研究院之Assetbundle的原理(六十一) 其次,接着往下看:Unity3D研究院之Assetbundle的实战( ...