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验证码邮件的更多相关文章

  1. js 验证码 倒计时60秒

    js 验证码 倒计时60秒 <input type="button" id="btn" value="免费获取验证码" /> & ...

  2. js验证码有效时间倒计时

    js验证码有效时间倒计时 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type& ...

  3. 纯js验证码

    纯js验证码 <!DOCTYPE html> <html> <head> <title>纯js验证码</title> </head&g ...

  4. Node.js定时邮件的那些事儿

    近开发一个项目,需要在Node.js程序里实现定期给管理员发邮件的功能. 笔者平时只会在Web界面收发邮件.对邮件的原理完全不懂(可能大学教过,然而全忘了),直到要解决这个问题.请教了几个业务的同事, ...

  5. easyui page添加文本,js验证码

    onLoadSuccess: function (db) { //db是后台数据的返回结果集 $.ajax({ url: "AjaxSource/Buex.ashx", data: ...

  6. js 验证码倒计时

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. angular.js 验证码注册登录

    css部分 header{ height: 50px; line-height: 50px; display: flex; } .callback{ text-align: left; display ...

  8. node.js发邮件

    在node上使用第三方类库(nodemailer)发邮件是一件很esay的事情:) app.js   以QQ邮箱为例 var nodemailer = require('nodemailer'); v ...

  9. 项目一:第八天 1、前台系统导入 实现客户注册 发验证码,邮件 springdata-redis存储数据 3、实现客户登陆

    1 前台系统客户注册功能 页面:signup.html 1.1 验证手机号是否注册(邮箱同样) 1. 使用Jquery-validate插件进行相关校验,使用校验规则 <input type=& ...

随机推荐

  1. 红外NEC协议

    注意: 用示波器在接收头抓的电平看起来和NEC协议刚好相反, 那是因为:HS0038B 这个红外一体化接收头,当收到有载波的信号的时候,会输出一个低电平,空闲的时候会输出高电平. 具体情况,具体分析. ...

  2. Linux的命令技巧

    一.使用apt-get installl 方法安装的库或者程序一般的路径如下 1.下载的软件存放位置       /var/cache/apt/archives 2.安装后软件默认位置    /usr ...

  3. 多端统一框架尝试--Taro

    参考资料 Taro官网Taro GitHubTaro资源汇总Taro-UI 我的demo代码 github地址 Taro介绍和尝试心得 Taro是基于React语法规范开发的多端统一的框架,一套代码可 ...

  4. web socket client

    <!DOCTYPE HTML> <html> <head> <title>My WebSocket</title> </head> ...

  5. e.getMessage 为空NULL

    在日常代码中免不了要try catch 切忌用try catch 去try 整个方法. 在对象操作之前尽量写上if 空判断. 反例: public void send(){ try{ 代码1:获取对象 ...

  6. 【Python】pip国内安装源和yum恢复

    豆瓣安装源 pip install packages -i http://pypi.doubanio.com/simple --upgrade --trusted-host pypi.doubanio ...

  7. Mac 笔记本 开发日记

    1.录屏,截图 Mac 自带录屏功能 command +control +o 2.复制当前应用,在启一个当前app窗口 command+n 3.快速回到桌面 command +f3 4.选中文件,复制 ...

  8. css一些特殊选择器

    css一些特殊选择器1.在box中,从第几个div开始选择,以后的都会选择到,以下代码表示从#box里面的第二个div开始选择:#box div:nth-of-type(n+2){}2.选择奇数个:d ...

  9. postgres on linux red hat 7 配置问题

    记录几个重点的东西吧,具体的步骤 不上了,网上有很多, 1.  redhat subscripiton需要订阅,没钱的需要去更换yum 源,也很简单,就是把自带的yum给删掉,重装其他的,大家可以百度 ...

  10. R语言-简单模型画图

    1.回归拟合 > plot(mtcars$mpg~mtcars$disp) > lmfit<-lm(mtcars$mpg~mtcars$disp) #线性回归模型 > abli ...