想到現(xiàn)在不止手機(jī)可以,還有郵箱,而且郵箱比短信平臺(tái)穩(wěn)定點(diǎn),以防萬(wàn)一,所以有加了一個(gè)郵箱驗(yàn)證碼的功能。
先上效果圖:
(后臺(tái))
(郵箱)
(驗(yàn)證)
1、獲取郵箱授權(quán)碼
2、只開(kāi)啟這個(gè)就好了,按要求發(fā)送短信,保存授權(quán)碼
導(dǎo)包
<!– 郵箱發(fā)送 –> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.17.RELEASE</version> </dependency>
jsp頁(yè)面
<!– 存放返回的驗(yàn)證碼 –> <input type="hidden" name="keepCode" id="keepCode" value="gzh" /> <div class="form-group" style="display: none;" id="emailDiv"> <label for="input_email"> <span class="glyphicon glyphicon-envelope"></span> <label for="email">密保郵箱</label></label> <input class="form-control input-lg" id="email" name="email" placeholder="請(qǐng)輸入注冊(cè)的密保郵箱!" type="text" value=""> </div>
js
var mail = $("#email").val(); var paras2 = "o_mail=" mail; $.post(path "/mail/sendMail?" paras2, function(data) { if (data != null && typeof (data) != "undefined") { var mailCode = data; $("#keepCode").attr("value", mailCode); } }, "json");
controller
@Controller @RequestMapping("/mail") public class EmailController { @ResponseBody @RequestMapping(value = "/sendMail",method = RequestMethod.POST) public String sendMail(HttpServletRequest request,HttpServletResponse response) throws ClientException { String email = request.getParameter("o_mail");//獲取前端傳送過(guò)來(lái)的電話號(hào)碼 String randomNum = createRandomNum(6);//隨機(jī)生成6位數(shù)的驗(yàn)證碼 JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("smtp.qq.com");//qq郵箱服務(wù)器 mailSender.setPort(587);//端口 mailSender.setUsername("你的郵箱");//發(fā)送者 mailSender.setPassword("你的授權(quán)碼");//POP3/SMTP服務(wù)授權(quán)碼 SimpleMailMessage mail = new SimpleMailMessage(); mail.setTo(email);// 接受者 mail.setFrom("你的郵箱");//發(fā)送者 mail.setSubject("MIP影視管理系統(tǒng)");//主題 mail.setText("您好!您的郵箱驗(yàn)證碼:" randomNum " 打死也不能告訴別人!");// 郵件內(nèi)容 mailSender.send(mail); System.out.println(mail); return randomNum; } /** * 生成隨機(jī)數(shù) * @param num 位數(shù) * @return */ public static String createRandomNum(int num){ String randomNumStr = ""; for(int i = 0; i < num;i ){ int randomNum = (int)(Math.random() * 10); randomNumStr = randomNum; } return randomNumStr; } }
以上全部
更多關(guān)于云服務(wù)器,域名注冊(cè),虛擬主機(jī)的問(wèn)題,請(qǐng)?jiān)L問(wèn)西部數(shù)碼官網(wǎng):bingfeng168.cn