SSM框架解决QQ邮箱激活535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´及端口25被占用问题

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

接下来是Util工具包的代码

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值