赞
踩
1、注意开启pop3/SMTP服务,同时注意发送邮箱时,使用的密码为开启后所随机提供的密码:
2、删除jdk安全文件(java.security)的加密算法,我的具体路径为:D:\Environment\jdk_18.0.1\conf\security(较新的版本安全稳健路径)
D:\Environment\jdk_18.0.1\lib\security(早先的版本在该路径下)
找到:
jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, \
DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL
改为如下:
jdk.tls.disabledAlgorithms=RC4, DES, MD5withRSA, \
DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL
3、Could not connect to SMTP host: smtp.163.com, port: 465, response: -1
解决办法是在java代码中添加如下语句:
props.put("mail.smtp.ssl.enable", true);
4、注意如下语句的第二个参数要使用发送的邮箱后缀
props.put("mail.smtp.host", "smtp.163.com");
5、选择端口时注意如下事项:
import dao.UserDAOImpl; import model.User; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Properties; /** * Servlet implementation class ResetPassword */ public class ResetPassword extends HttpServlet { private static final long serialVersionUID = 1L; private UserDAOImpl userDAO = new UserDAOImpl(); /** * @see HttpServlet#HttpServlet() */ public ResetPassword() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String err = ""; if (username.equals("")) { err += "必须输入完整的信息!"; } else { if (userDAO.checkUser(username) == false) { err += "用户名不存在!"; } } if (err.length() > 0) { request.setAttribute("err", err); } String url = "/resetpassword.jsp"; try { if (err.length() == 0) err. Lengthr u = userDAO.getUser(username); User new_user = new User(u.getUser_id(), username, "passwordreset", u.getNgaysinh(), u.getGioitinh(), u.getEmail(), u.getSdt(), u.getDiachi(), u.getRole()); userDAO.updateUser(new_user); url = "/login.jsp"; String mess = "检查电子邮件接收新密码!"; request.setAttribute("mess", mess); //通过电子邮件发送新密码。 final String username_mail = "xxx@163.com"; final String password = "随机码"; String to = u.getEmail(); String subject = "Reset Password"; String text ="<i>Reset Password</i><br/>"; text+="<p>User: <strong>"; text+=username; text+="</strong></p>"; text+="<p>New password: <strong>passwordreset</strong></p>"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smprops. Putls.enable", "true"); props.put("mail.smtp.ssl.enable", true); props.put("mail.smprops. Put "smtp.163.com"); props.put("mail.smtp.port", "25"); Session session_mail = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username_mail, password); } }); try { Message message = new MimeMessage(session_mail); message.setHeader("Content-Type", "text/plain; charset=UTF-8"); message.setFrom(new InternetAddress(username_mail)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); message.setContent(text, "text/html; charset=utf-8"); Transport.send(message); } catch (MessagingException e) { throw new RuntimeException(e); } } else { url = "/resetpassword.jsp"; } RequestDispatcher rd = getServletContext() .getRequestDispatcher(url); rd.forward(request, response); } catch (Exception e) { e.printStackTrace(); response.sendRedirect("/resetpassword.jsp"); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。