java web激活郵箱並找回暗碼。本站提示廣大學習愛好者:(java web激活郵箱並找回暗碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java web激活郵箱並找回暗碼正文
簡直每一個網站或服裝論壇t.vhao.net之類的用戶注冊後都須要經由過程發送郵件到郵箱激活用戶,若何激活郵箱吶?
設計激活步調:
1、發送激活操作鏈接地址至用戶郵箱。
2、用戶至郵箱查收郵件。
3、用戶點擊鏈接,跳轉至勝利頁面(修正激活狀況),激活勝利。
完成:
/** * 發送郵件 * * @param request * @return */ @RequestMapping(value = "/sendEmail.jhtml") @ResponseBody public String sendEmail(HttpServletRequest request) { DynamicParams params = new DynamicParams(request); StringBuilder builder = new StringBuilder(); StringBuffer url = new StringBuffer(); String type = params.getString("type"); Long uid = params.getLong("id"); User user = userManager.find(uid); String subject = ""; // 斷定能否已激活 if ("1".equals(String.valueOf(user.getEmailStatus()))) { return errMsg(); } String contextPath = request.getContextPath(); String rUrl = String.valueOf(request.getRequestURL()); url.append(rUrl.substring(0, rUrl.indexOf(contextPath))); url.append(contextPath + "/account"); // 郵箱激活 // url.append("/activateEmail.jhtml?email=" + user.getEmail() + // "&id=" + uid + "&mode=activate"); url.append("/activateEmail.jhtml?id=" + uid + "&mode=activate"); // 注釋 builder.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" </head><body>"); builder.append("請點擊下方鏈接激活您的郵箱,完成激活郵箱的操作!"); builder.append("<br/><br/>"); builder.append("<a href=\"" + url + "\">"); builder.append(url); builder.append("</a>"); builder.append("</body></html>"); subject = "郵箱地址激活 - xxxx"; MailSender.mailSimple(user.getEmail(), subject, builder.toString(), false, null); return successMsg(); } /** * 激活郵箱 * * @param request * @return */ @RequestMapping(value = "/activateEmail.jhtml") public String activateEmail(HttpServletRequest request, Model model) { DynamicParams params = new DynamicParams(request); // 激活之前查詢 User user = userManager.find(params.getLong("id")); if ("1".equals(String.valueOf(user.getEmailStatus()))) { // 已激活 model.addAttribute("flag", false); } else { // 未激活 userManager.activateEmail(params); // 激活以後查詢 user = userManager.find(params.getLong("id")); request.getSession().setAttribute("sessionUser", user); model.addAttribute("mode", params.getString("mode")); model.addAttribute("flag", true); } return "site/modules/account/activateSuccess"; }
MailSender.mailSimple(user.getEmail(), subject, builder.toString(),false, null);即為發送郵件對象類,若有不明確可參考我之前的文章(spring mail發送郵件)
忘卻暗碼(重置暗碼)
配景:異樣是各年夜網站都邑具有的一個功效,經由過程發送驗證碼到用戶注冊的郵箱重置暗碼
設計:1)輸出用戶名/手機號碼/郵箱,驗證碼。
2)用戶選擇手機驗證或郵箱驗證,發送驗證碼得手機或郵箱。假如用戶郵箱未激活,則不許可郵箱驗證。
3)用戶輸出驗證碼勝利,進入暗碼從新設置頁面。
4)重置暗碼
流程:
完成:
/** * 發送郵件 * * @param request * @return */ @RequestMapping(value = "/sendEmail.jhtml") @ResponseBody public String sendEmail(HttpServletRequest request) { DynamicParams params = new DynamicParams(request); StringBuilder builder = new StringBuilder(); StringBuffer url = new StringBuffer(); String type = params.getString("type"); Long uid = params.getLong("id"); User user = userManager.find(uid); String subject = ""; // type = forget 暗碼重置 String verifyCode = VerifyCodeUtils.generateVerifyCode(4); request.getSession().setAttribute("resetCertCode", verifyCode); url.append("<font color='red'>" + verifyCode + "</font>"); // 注釋 builder.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>"); builder.append("要應用新的暗碼, 請將已下字符輸出驗證框中,完成重置暗碼的操作!"); builder.append("<br/><br/>"); builder.append("<div>" + url + "</div>"); builder.append("</body></html>"); subject = "暗碼重置 - xxxx"; MailSender.mailSimple(user.getEmail(), subject, builder.toString(), false, null); return successMsg(); }
至此,激活郵箱和忘卻暗碼功效已初步完成!願望對年夜家的進修有所贊助,年夜家可以著手完成激活郵箱並找回暗碼。