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>
原文地址:https://www.cnblogs.com/lusufei/p/7745977.html