JavaWeb 发送 激活邮件功能

时间:2022-09-24 13:10:14
   //发送激活邮件

Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.host", "smtp.qq.com");
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.debug", "true");
//腾讯邮箱需要下面3个代码开启ssl 安全认证
prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.setProperty("mail.smtp.port", "465");
prop.setProperty("mail.smtp.socketFactory.port", "465");
Session session = Session.getInstance(prop);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("974600840@qq.com"));//这里是发信人
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("这里填写收信人邮箱");
msg.setSubject("邮件标题");
msg.setText("邮件主题内容");
Transport tran = session.getTransport();
tran.connect("974600840@qq.com", "mgumxnxhsojhbegg");//这里的随机码 需要到qq邮箱设置中打开smtp协议开关拿到生成的随机码
tran.sendMessage(msg, msg.getAllRecipients());