SSM解决阿里云服务器邮箱激活25端口被占用,通过JAVA代码调用端口465及535
SSM框架实现商城邮箱激活账号出现535问题
网上有很多实现邮箱激活的代码,这次实现我也是调用网上的代码来实现我的商城课设,然后在实现激活的时候出现org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256 错误,当时看了网上的建议,有人说是授权码问题,我改了好几遍,但还是报这种错误,就很忧伤了,后面用手机数据开热点给我电脑连接的时候,它居然居然就好了(黑人大写懵逼问号脸),实在是有点受不了,当时心里就在埋怨长城宽带,实在是太不给力了,没办法这是学校的网,没法为学校改网络,就只能心里MMP接受了,然后就有了下面的显示,QQ也显示收到信息了
阿里云服务器25端口关闭问题
现在阿里云为了服务器的安全问题,已经淘汰了使用25端口进行邮箱发送,所以当开启QQ邮箱POP3与SMTP服务的时候,挂到服务器上面,运行的时候是没有一丁点错误的,但你就是接受不到验证吗,嘿嘿,气不气,当时我去服务器配置了端口,按网上配了半天愣是没成功,当时就留下了不争气的眼泪,还去云盾安全管控那里申请
也是没通过,哇,一口20几年的老血就要吐了出来,内心那个难受啊,不过得亏百度爸爸,有许多大神发了很多他们的代码,我终于找到了我的解决方案,就是加入以下几行代码
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
然后重新把代码挂到服务器上面,QQ邮箱也显示收到了验证码,perfect,开森的一比,以下是邮箱激活的代码,我的是整合SSM框架的代码,希望能够帮助到你们
控制器EmailCtrl代码:
package com.e_buy.control;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.e_buy.service.UserService2;
import com.e_buy.util.JavaMailUtil;
import com.e_buy.util.RandomUtil;
import com.e_buy.util.htmlText;
@Controller
public class EmailCtrl {
@Autowired
private UserService2 userService;
@RequestMapping(value="/e_index/SendEmailServlet.do")
public String SendEmailServlet(HttpServletRequest request,HttpServletResponse response) {
try {
String email = request.getParameter("email");
JavaMailUtil.receiveMailAccount = email; // 给用户输入的邮箱发送邮件
// 1、创建参数配置,用于连接邮箱服务器的参数配置
Properties props = new Properties();
// 开启debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置右键服务器的主机名
props.setProperty("mail.host", JavaMailUtil.emailSMTPHost);
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
// 2、根据配置创建会话对象,用于和邮件服务器交互
Session session = Session.getInstance(props);
// 设置debug,可以查看详细的发送log
session.setDebug(true);
// 3、创建一封邮件
String code = RandomUtil.getRandom();
System.out.println("邮箱验证码:" + code);
String html = htmlText.html(code);
MimeMessage message = JavaMailUtil.creatMimeMessage(session, JavaMailUtil.emailAccount,
JavaMailUtil.receiveMailAccount, html);
// 4、根据session获取邮件传输对象
Transport transport = session.getTransport();
// 5、使用邮箱账号和密码连接邮箱服务器emailAccount必须与message中的发件人邮箱一致,否则报错
transport.connect(JavaMailUtil.emailAccount, JavaMailUtil.emailPassword);
// 6、发送邮件,发送所有收件人地址
transport.sendMessage(message, message.getAllRecipients());
// 7、关闭连接
transport.close();
// 写入session
request.getSession().setAttribute("code", code);
} catch (Exception e) {
e.printStackTrace();
request.getSession().setAttribute("error", "邮件发送失败");
}
return null;
}
@RequestMapping(value="/e_index/RegistServlet.do")
public String RegistServlet(HttpServletRequest request,HttpServletResponse response) {
String sessionCode = (String) request.getSession().getAttribute("code");
String userId = (String) request.getSession().getAttribute("userId");
//System.out.println(sessionCode);
// 获取session中的验证码
if(sessionCode != null) {
// 获取页面提交的验证码
String inputCode = request.getParameter("code");
//System.out.println("页面提交的验证码:" + inputCode);
int count = userService.updateStatus(userId,inputCode);
if (sessionCode.toLowerCase().equals(inputCode.toLowerCase())&&count>0) {
// 验证成功,跳转成功页面
// 移除session中的验证码
request.removeAttribute("code");
request.removeAttribute("userId");
return "activate_success";
}else {
// 移除session中的验证码
request.removeAttribute("code");
// 验证失败
return "activate_failure";
}
}else {
// 移除session中的验证码
request.removeAttribute("code");
// 验证失败
return "activate_failure";
}
}
}