java发送邮件503错误!!!急急急急,求各位大神帮忙看看

小疯疯子 2016-09-26 06:54:25
代码如下

sendEmail.java:
package test;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class SendEmail {
public static final String HOST = "smtp.qq.com";
public static final String PROTOCOL = "smtp";
public static final int PORT = 25;
public static final String FROM = "123456789@qq.com";//发件人的email ,由于个人邮箱,修改了,自己测试时用的是自己的正确的邮箱密码
public static final String PWD = "123456";//发件人密码

/**
* 获取Session
* @return
*/
private static Session getSession() {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);//设置服务器地址
props.put("mail.store.protocol" , PROTOCOL);//设置协议
props.put("mail.smtp.port", PORT);//设置端口
props.put("mail.smtp.auth" , true);

Authenticator authenticator = new Authenticator() {

@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM, PWD);
}

};
Session session = Session.getDefaultInstance(props , authenticator);

return session;
}

public static void send(String toEmail , String content) {
Session session = getSession();
try {
System.out.println("--send--"+content);
// Instantiate a message
Message msg = new MimeMessage(session);

//Set message attributes
msg.setFrom(new InternetAddress(FROM));
InternetAddress[] address = {new InternetAddress(toEmail)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("账号激活邮件");
msg.setSentDate(new Date());
msg.setContent(content , "text/html;charset=utf-8");

//Send the message
Transport.send(msg);
Transport tran = session.getTransport("smtp"); // 通过SMTP效验用户,密码等进行连接
tran.connect(HOST, FROM, PWD);
tran.sendMessage(msg, msg.getAllRecipients());
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}


}





registervalidateService.java:

package test.service;

import java.util.Date;

import javax.annotation.Resource;
import javax.mail.internet.ParseException;

import org.springframework.stereotype.Service;

import test.MD5Util;
import test.SendEmail;
import test.ServiceException;
import test.dao.UserDAO;
import test.entity.UserModel;

@Service
public class RegisterValidateService {
@Resource(name="userDAO")
private UserDAO userDAO;
/**
* 处理注册
*/

public void processregister(String email){
UserModel user=new UserModel();
user.setId(1);
user.setName("xiaoming");
user.setPassword("324545");
user.setEmail(email);
user.setRegisterTime(new Date());
user.setStatus(0);
//如果处于安全,可以将激活码处理的更复杂点,这里稍做简单处理
user.setValidateCode(MD5Util.encode2hex(email));
System.out.println(user);
userDAO.save(user);//保存注册信息
System.out.println("************************************");
///邮件的内容
StringBuffer sb=new StringBuffer("点击下面链接激活账号,48小时生效,否则重新注册账号,链接只能使用一次,请尽快激活!</br>");
sb.append("<a href=\"http://localhost:8080/email/user/register?action=activate&email=");
sb.append(email);
sb.append("&validateCode=");
sb.append(user.getValidateCode());
sb.append("\">http://localhost:8080/email-test/user/register?action=activate&email=");
sb.append(email);
sb.append("&validateCode=");
sb.append(user.getValidateCode());
sb.append("</a>");

//发送邮件
SendEmail.send(email, sb.toString());
System.out.println("发送邮件");

}

/**
* 处理激活
* @throws ParseException
*/
//传递激活码和email过来
public void processActivate(String email , String validateCode)throws ServiceException, ParseException{
//数据访问层,通过email获取用户信息
UserModel user=userDAO.findByEmail(email);
//验证用户是否存在
if(user!=null) {
//验证用户激活状态
if(user.getStatus()==0) {
///没激活
Date currentTime = new Date();//获取当前时间
//验证链接是否过期
currentTime.before(user.getRegisterTime());
if(currentTime.before(user.getLastActivateTime())) {
//验证激活码是否正确
if(validateCode.equals(user.getValidateCode())) {
//激活成功, //并更新用户的激活状态,为已激活
System.out.println("==sq==="+user.getStatus());
user.setStatus(1);//把状态改为激活
System.out.println("==sh==="+user.getStatus());
userDAO.update(user);
} else {
throw new ServiceException("激活码不正确");
}
} else { throw new ServiceException("激活码已过期!");
}
} else {
throw new ServiceException("邮箱已激活,请登录!");
}
} else {
throw new ServiceException("该邮箱未注册(邮箱地址不存在)!");
}

}
}


registerController.java:

package test.controller;

import javax.annotation.Resource;
import javax.mail.internet.ParseException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import test.ServiceException;
import test.service.RegisterValidateService;

@Controller
public class RegisterController {
@Resource
private RegisterValidateService service;

@RequestMapping(value="/user/register",method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView load(HttpServletRequest request,HttpServletResponse response) throws ParseException{
String action = request.getParameter("action");

System.out.println("-----r----"+action);
ModelAndView mav=new ModelAndView();

if("register".equals(action)) {
//注册
String email = request.getParameter("email");
System.out.println(email);
service.processregister(email);//发邮箱激活
mav.addObject("text","注册成功");
mav.setViewName("register/register_success");
}
else if("activate".equals(action)) {
//激活
String email = request.getParameter("email");//获取email
String validateCode = request.getParameter("validateCode");//激活码

try {
service.processActivate(email , validateCode);//调用激活方法
mav.setViewName("register/activate_success");
} catch (ServiceException e) {
request.setAttribute("message" , e.getMessage());
mav.setViewName("register/activate_failure");
}

}
return mav;
}
}





出现错误:


com.sun.mail.smtp.SMTPSendFailedException: 503 Error: need EHLO and AUTH first !

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at test.SendEmail.send(SendEmail.java:64)
at test.service.RegisterValidateService.processregister(RegisterValidateService.java:50)
at test.controller.RegisterController.load(RegisterController.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)


...全文
733 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
小疯疯子 2016-09-29
  • 打赏
  • 举报
回复
引用 8楼sd_lijunliang 的回复:
你把密码改成激活POP3/SMTP服务 时产生的激活码试一下
对的 我就是那个激活码
耳冉_erran 2016-09-28
  • 打赏
  • 举报
回复
你把密码改成激活POP3/SMTP服务 时产生的激活码试一下
小疯疯子 2016-09-28
  • 打赏
  • 举报
回复
引用 6楼花谢尊前不敢香 的回复:
1、打开debug调试模式 props.setProperty("mail.debug", "true"); 2、使用ssl看下 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf);
谢啦
小疯疯子 2016-09-27
  • 打赏
  • 举报
回复
引用 4 楼 fangmingshijie 的回复:
    props.put("mail.smtp.auth" , true)字符串true,不是Boolean类型
改成字符串还是错误
  • 打赏
  • 举报
回复
1、打开debug调试模式 props.setProperty("mail.debug", "true"); 2、使用ssl看下 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf);
  • 打赏
  • 举报
回复
    props.put("mail.smtp.auth" , true)字符串true,不是Boolean类型
I,Frankenstein 2016-09-26
  • 打赏
  • 举报
回复
引用 2 楼 weixin_35132583 的回复:
[quote=引用 1楼浩浩学习 的回复:]1) 检查你的QQ邮箱的POP/SMTP功能有没有打开? 2) 有可能你的程序多次登录,被QQ邮箱当作恶意登录给拒绝了,换其他邮箱发送试试看。 3) 有可能你没有线登录
你好 前两条都是对的 第三条线登录是什么东东 谢谢[/quote] 打错了,“先登录”,以前我做过一次Javamail,当时就是请求过去的时候没带用户名密码,所以报了这个错误。
小疯疯子 2016-09-26
  • 打赏
  • 举报
回复
引用 1楼浩浩学习 的回复:
1) 检查你的QQ邮箱的POP/SMTP功能有没有打开? 2) 有可能你的程序多次登录,被QQ邮箱当作恶意登录给拒绝了,换其他邮箱发送试试看。 3) 有可能你没有线登录
你好 前两条都是对的 第三条线登录是什么东东 谢谢
I,Frankenstein 2016-09-26
  • 打赏
  • 举报
回复
1) 检查你的QQ邮箱的POP/SMTP功能有没有打开? 2) 有可能你的程序多次登录,被QQ邮箱当作恶意登录给拒绝了,换其他邮箱发送试试看。 3) 有可能你没有线登录

81,092

社区成员

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

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