当前位置:   article > 正文

java使用网易邮箱163.com发送邮件(servlet编程)_java 网易邮箱

java 网易邮箱

注意事项

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
  • 1
  • 2

改为如下:

jdk.tls.disabledAlgorithms=RC4, DES, MD5withRSA, \
    DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL
  • 1
  • 2

3、Could not connect to SMTP host: smtp.163.com, port: 465, response: -1
解决办法是在java代码中添加如下语句:

props.put("mail.smtp.ssl.enable", true);
  • 1

4、注意如下语句的第二个参数要使用发送的邮箱后缀

props.put("mail.smtp.host", "smtp.163.com");
  • 1

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");
		}
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/591812
推荐阅读
相关标签
  

闽ICP备14008679号