使用javamail做验证的时候报535,邮件不能发送

大瓶子呀 2015-05-22 07:30:37
报错的代码

package control;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.MyAuthenticator;

public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//获取前台用户输入的邮箱以及用户名
String toMail = request.getParameter("email");
String registerName = request.getParameter("userName");

// 创建一个临时用户注册ID
String registerId = "" + Math.random() * Math.random();

//邮件发送成功后,用户点在邮箱中点击这个链接回到注册网站。
String url = "http://localhost:8080/JavaMailChecker/servlet/MailBackServlet?registerId=" + registerId;

// 设置session值,将用户ID和用户名保存在session值中
HttpSession httpSession = request.getSession();
httpSession.setAttribute(registerId, registerName);
// 设置session的有效时间,为10分钟,10分钟内没有点击链接的话,注册将失败
httpSession.setMaxInactiveInterval(600);

// 设置邮箱的属性
Properties props = new Properties();
// 若发送者的邮箱为其他服务器,例如163邮箱的话,就将下面服务器的值改为smtp.163.com
props.setProperty("mail.smtp.host", "smtp.qq.com");
props.setProperty("mail.smtp.auth", "true");

// 设置验证值,发送者的邮箱要通过验证才可以发送邮件
// 设置发送者的邮箱和密码,对其进行修改
String userName = "603367003@qq.com";
String password = "****";
Authenticator authenticator = new MyAuthenticator(userName, password);

javax.mail.Session session = javax.mail.Session.getDefaultInstance(props,authenticator);
session.setDebug(true);

try{
Address from = new InternetAddress(userName);
Address to = new InternetAddress(toMail);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(from);
msg.setSubject("###网站注册");
msg.setSentDate(new Date());
msg.setContent("点击<a href='" + url + "'>" + url + "</a>完成注册!", "text/html;charset=utf-8");
msg.setRecipient(RecipientType.TO, to);
Transport.send(msg);

} catch(MessagingException e){
e.printStackTrace();
}

request.getRequestDispatcher("/sendMailSuccess.jsp").forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}


错信息
DEBUG: setDebug: JavaMail version 1.4.4
javax.mail.internet.MimeMessage@120bffd++++++++++++++++++++++
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25, isSSL false
220 smtp.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host "smtp.qq.com", port: 25

EHLO PC-20140118BBVU
250-smtp.qq.com
250-PIPELINING
250-SIZE 73400320
250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN
250-MAILCOMPRESS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "73400320"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "MAILCOMPRESS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
AUTH LOGIN
334 VXNlcm5hbWU6
NjAzMzY3MDAzQHFxLmNvbQ==
334 UGFzc3dvcmQ6
MTk5NDAzMDJ3YXdqLiwsLg==
535 Authentication failed
javax.mail.AuthenticationFailedException: 535 Authentication failed

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at control.RegisterServlet.doGet(RegisterServlet.java:68)
at control.RegisterServlet.doPost(RegisterServlet.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
...全文
975 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
点滴寸土 2017-04-17
  • 打赏
  • 举报
回复
Authenticator authenticator = new MyAuthenticator(userName, password); 这里的userName不一定是你的邮箱地址,比如 123456@163.com,可能就是123456,具体需要看自己邮箱设置的用户名是啥
110成成 2017-04-17
  • 打赏
  • 举报
回复
你的邮件服务器需要验证,继承Authenticator 实现重写getPasswordAuthentication方法,即可。
夜澜静铭 2017-04-17
  • 打赏
  • 举报
回复
端口错了。。qq邮件不是这个端口
qq_36571958 2016-11-02
  • 打赏
  • 举报
回复
没人解决吗我也报这个错了
qq_29037001 2015-09-05
  • 打赏
  • 举报
回复
大哥解决没有,,

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧