赞
踩
java mail配置了QQ邮箱作为服务器发送邮件,结果导致出现503异常:
Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl).
如果要正确发送邮件,需要确定几件事情:
我使用的是零配置的方式:
@Bean(name = "javaMailSender")
public JavaMailSenderImpl javaMailSender(){
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("smtp.qq.com");
javaMailSender.setPort(465);
javaMailSender.setUsername("邮箱名@foxmail.com");
javaMailSender.setPassword("授权码");
Properties properties = new Properties();
properties.setProperty("mail.host", "smtp.qq.com");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.port", "465");
properties.setProperty("mail.smtp.socketFactory.port", "465");
javaMailSender.setJavaMailProperties(properties);
return javaMailSender;
}
@Autowired
JavaMailSenderImpl javaMailSender;
public void sendMail(String code) {
MimeMessage mailMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "utf-8");
helper.setFrom("邮箱名@foxmail.com");// 设置发件人
helper.setTo("收件人@163.com");// 设置收件人
// helper.setCc(cc);// 设置抄送
helper.setSubject("验证码");// 设置主题
helper.setText("验证码:【"+code+"】");// 邮件体
javaMailSender.send(mailMessage);// 发送邮件
log.info("邮件发送成功...");
} catch (Exception e) {
log.error("邮件发送发生异常:" + e.getMessage(), e);
try {
Thread.sleep(1000 * 1000);
this.sendMail(code);
} catch (InterruptedException e1) {
log.error("重发邮件发生异常:" + e1.getMessage(), e1);
}
}
}
这里面最重要的是端口【mail.smtp.port】和协议【mail.transport.protocol】,一定不能写错。
另外,如果不是QQ邮箱,可以参考以下配置:
配置一个邮件发送类,然后再配置一个任务线程池,每次发送邮件的时候,将发送任务放到任务线程池中,提高性能,减少发送邮件过程对性能的影响。
/**
* 邮件发送类
* @return
*/
@Bean(name="javaMailSender")
public JavaMailSender javaMailSender(){
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setDefaultEncoding(emailEncoding);
javaMailSender.setHost(emailHost);
javaMailSender.setPort(emailPort);
javaMailSender.setUsername(emailUsername);
javaMailSender.setPassword(emailPassword);
Properties pp = new Properties();
pp.setProperty("email.smtp.auth", "true");
pp.setProperty("email.smtp.timeout", "50000");
javaMailSender.setJavaMailProperties(pp);
return javaMailSender;
}
/**
* 任务线程池
* @return
*/
@Bean(name="taskExecutor")
public ThreadPoolTaskExecutor taskExecutor(){
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(theadPoolMinSize);
taskExecutor.setMaxPoolSize(theadPoolMaxSize);
taskExecutor.setQueueCapacity(theadPoolQueueSize);
taskExecutor.initialize();
return taskExecutor;
}
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
/**
* 发送邮件
* @param recipient 收件人
* @param subject 主题(邮件标题)
* @param content 邮件内容
*/
public void sendMail(final String recipient, final String subject, final String content){
taskExecutor.execute(new Runnable() {
public void run() {
try {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(MAIL_FROM);
message.setTo(recipient);
message.setSubject(subject);
message.setText(content);
javaMailSender.send(message);
} catch (Exception e) {
logger.error("邮件发送失败:", e);
}
}
});
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。