获取验证码

获取验证码的接口

	/**
	 * @param request
	 * @param response
	 * @param model
	 * @throws Exception
	 * 获取验证码
	 * Administrator
	 */
	@RequestMapping(value = "/forApp/***.jspx")
	public void getNewregister(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws Exception {
		
		response.addHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/plain;charset=UTF-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		PrintWriter out = response.getWriter();
		//输入注册信息
		 String email = request.getParameter("email");
		 //String realname = "旅日侨网";
		 //String matchCode = "1234";
		if (!BaseAppUtils.isEmptyAfterTrim(email)
				&& !BaseAppUtils.isEmptyAfterTrim(email)) {
			randomSecurity = EmailUtils.sendApps(email,request);
			if(randomSecurity!=null){
				//out.println(BaseAppUtils.SUCCESS());
				BaseAppUtils.responseStr(response, "成功");
				out.flush();
				out.close();
			}else{
				//out.println(BaseAppUtils.FAIL_ORC("验证码发送失败"));
				BaseAppUtils.responseStrError(response, "验证码发送失败");
				out.flush();
				out.close();
			}
			
		}else{
			//out.println(BaseAppUtils.FAIL_ORC("邮箱为空"));
			BaseAppUtils.responseStrError(response, "邮箱为空");
			out.flush();
			out.close();
		}
		
	}

工具类

public class EmailUtils {

	/**
	 * @param email
	 * @param realname
	 * @param matchCode
	 * @param request
	 * @return
	 * 邮箱验证码
	 * Administrator
	 */
	 //存储验证码的集合
	private static Map<String,String> randomSecurity = new TreeMap<String, String>();

	public static Map<String,String> sendApps(String email, HttpServletRequest request)
	{
		int random=((int)(Math.random()*9000+1000));
		try {
			// 将旧的验证码移除
			randomSecurity.remove(email);
			//request.getSession().setAttribute(appToken, String.valueOf(random));
			// 向map中放入新的验证码
			randomSecurity.put(email, String.valueOf(random));
			// 发件人的账号
			String userName = "***";
			// 账号密码
			String password = "***";
			//阿里云邮箱服务地址
			String smtp_server = "***";
			
			//发件人
			String from_mail_address = userName;
			//收件人
			String to_mail_address = email;
			
			// 构建授权信息,用于进行SMTP进行身份验证
			Authenticator auth = new PopupAuthenticator(userName, password);
			
			// 配置发送邮件的环境属性
			Properties mailProps = new Properties();
			//账号密码
			mailProps.put("mail.user", userName);
			mailProps.put("mail.password", password);
			// 表示SMTP发送邮件,需要进行身份验证
			mailProps.put("mail.smtp.host", smtp_server);
			mailProps.put("mail.smtp.port", "25");
			mailProps.put("mail.smtp.auth", "true");
			// 使用环境属性和授权信息,创建邮件会话
			Session mailSession = Session.getDefaultInstance(mailProps, auth);
			mailSession.setDebug(true);
			//邮件昵称
			String nick=javax.mail.internet.MimeUtility.encodeText("****");
			
			// 创建邮件消息
			MimeMessage message = new MimeMessage(mailSession);
			// 设置发件人
			message.setFrom(new InternetAddress(nick+" <"+from_mail_address+">"));
			// 设置收件人
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(
					to_mail_address));
			// 设置邮件标题
			message.setSubject("验证信息");
			
			MimeMultipart multi = new MimeMultipart();
			BodyPart textBodyPart = new MimeBodyPart();
			//发送邮件内容
			textBodyPart.setContent(""
					+ "您好:您的邮箱验证码为:
"
+random+"
"
+ "
"
+ "
此邮件为"
+ "系统自动发送,请勿回复。", "text/html;charset=utf-8"); multi.addBodyPart(textBodyPart); message.setContent(multi); message.saveChanges(); //发送 Transport.send(message); } catch (Exception ex) { System.err.println("邮件发送失败的原因是:" + ex.getMessage()); System.err.println("具体的错误原因"); ex.printStackTrace(System.err); return null; } return randomSecurity; } }

你可能感兴趣的:(程序人生)