jshtml
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. */
htmljava
<!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包jquery
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.xmlweb
<?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>