使用javaMail发送邮件失败,求大神指点迷津
我想实现一个注册功能,然后注册里面添加一个激活功能。下面是运行效果图[图片]
[图片]
[图片]
当界面出现恭喜注册成功,代码应该是没有抛异常才对,也就是能发送出去,而我是通过163邮箱向QQ邮箱发送消息,网上一查要打开授权,我按照网上的做法打开了
[图片]
然后我的配置文件里的密码也是授权时写的密码
[图片]
然后呢,现在就是注册功能是实现了,授权也开了,代码也不出错,但是就是发送不成功。我这个菜鸟已经不知道该怎么办了,求大神帮帮忙
[图片]
数据库里面是有数据的,说明注册成功了,下面是我的UserServlet里面的代码
程序代码:
package user.servlet; import import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.Session; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.import cn.itcast.mail.Mail; import cn.itcast.mail.MailUtils; import cn.itcast.servlet.BaseServlet; import user.domain.User; import user.service.UserException; import user.service.UserService; public class UserServlet extends BaseServlet { private UserService userService = new UserService(); /* * 激活功能 */ public String active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("恭喜激活成功!"); return null; } public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 封装表单数据 User form = CommonUtils.toBean(request.getParameterMap(), User.class); // 补全数据 form.setUid(CommonUtils.uuid()); form.setCode(CommonUtils.uuid() + CommonUtils.uuid()); // 输入校验 Map<String,String> errors = new HashMap<String,String>(); String username = form.getUsername(); if (username == null || username.trim().isEmpty()) { errors.put("username", "用户名不能为空"); } else if(username.length() < 3 || username.length() > 10) { errors.put("username", "用户名长度必须在3~10之间!"); } String password = form.getPassword(); if (password == null || password.trim().isEmpty()) { errors.put("password", "密码不能为空"); } else if(password.length() < 3 || password.length() > 10) { errors.put("password", "密码长度必须在3~10之间!"); } String email = form.getEmail(); if (email == null || email.trim().isEmpty()) { errors.put("email", "邮箱不能为空"); } else if(!email.matches("\\w+@\\w+\\.\\w+")) { errors.put("email", "邮箱格式不对!"); } // 判断是否存在错误信息 if (errors.size() > 0) { // 保存错误信息 // 保存到表单数据 // 转发到registe.jsp request.setAttribute("errors", errors); request.setAttribute("form", form); return "/jsps/user/regist.jsp"; } // 调用service的registe()方法 try { userService.regist(form); } catch (UserException e) { // 保存异常信息 // 保存form // 转发到registe.jsp request.setAttribute("msg", e.getMessage()); request.setAttribute("form", form); return "/jsps/user/regist.jsp"; } // 发邮件 Properties props = new Properties(); props.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties")); String host = props.getProperty("host");// 获取服务器主机 String uname = props.getProperty("uname");// 获取用户名 String pwd = props.getProperty("pwd");// 获取密码 String from = props.getProperty("from");// 获取发件人 String to = form.getEmail();// 获取收件人 String subject = props.getProperty("subject");// 获取主题 String content = props.getProperty("content");// 获取邮件内容 content = MessageFormat.format(content, form.getCode());// 替换{0} Session session = MailUtils.createSession(host, uname, pwd);// 得到session Mail mail = new Mail(from, to, subject, content);// 创建邮件对象 try { MailUtils.send(session, mail);// 发邮件 } catch (MessagingException e) { } /* * 保存成功信息 * 转发到msg.jsp */ request.setAttribute("msg", "恭喜注册成功,请到邮箱激活!"); return "/jsps/user/regist.jsp"; } }
下面是我register.jsp的代码
程序代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java. %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>注册</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>注册</h1> <p style="color: red; font-weight: 900">${msg }</p> <form action="<c:url value='/UserServlet'/>" method="post"> <input type="hidden" name="method" value="regist"/> 用户名:<input type="text" name="username" value="${form.username }"/> <span style="color: red; font-weight: 900">${errors.username }</span> <br/> 密 码:<input type="password" name="password" value="${form.password }"/> <span style="color: red; font-weight: 900">${errors.password }</span> <br/> 邮 箱:<input type="text" name="email" value="${form.email }"/> <span style="color: red; font-weight: 900">${errors.email }</span> <br/> <input type="submit" value="注册"/> </form> </body> </html>
这个是我配置文件的代码
程序代码:
host=smpt.\u8FD9\u662F\u6765\u81EA\u81EA\u5DF1\u7684\u6FC0\u6D3B\u90AE\u4EF6 content=<a href\="http\://localhost\:8080/bookstore/UserServlet?method\=active&code\={0}">\u70B9\u51FB\u8FD9\u91CC\u5B8C\u6210\u6FC0\u6D3B</a>
如果发帖格式有什么不错的地方,请多包涵,下次会改正的,谢谢大家!!!