欢迎来到 IT实训基地-南通科迅教育
咨询电话:0513-81107100
Java mail使用QQ邮箱发送邮件配置方案
2017/3/27
南通科迅教育
477
南通零基础IT培训学校哪里靠谱

java mail配置了QQ邮箱作为服务器发送邮件,结果导致出现503异常:

?
1
Authentication failed; nested exception is javax.mail.AuthenticationFailedException:530Error: A secure connection is requiered(such as ssl).

如果要正确发送邮件,需要确定几件事情:

QQ邮箱的POP3/SMTP服务已开启,并获取授权码(当密码使用)

这里写图片描述

正确的邮件服务配置

我使用的是零配置的方式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Bean(name ="javaMailSender")
    publicJavaMailSenderImpl javaMailSender(){
        JavaMailSenderImpl javaMailSender =newJavaMailSenderImpl();
        javaMailSender.setHost("smtp.qq.com");
        javaMailSender.setPort(465);
        javaMailSender.setUsername("邮箱名@foxmail.com");
        javaMailSender.setPassword("授权码");
        Properties properties =newProperties();
        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);
        returnjavaMailSender;
    }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Autowired
JavaMailSenderImpl javaMailSender;
 
publicvoidsendMail(String code) {
        MimeMessage mailMessage = javaMailSender.createMimeMessage();
        try{
            MimeMessageHelper helper =newMimeMessageHelper(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邮箱,可以参考以下配置:

配置一个邮件发送类,然后再配置一个任务线程池,每次发送邮件的时候,将发送任务放到任务线程池中,提高性能,减少发送邮件过程对性能的影响。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * 邮件发送类
 * @return
 */
@Bean(name="javaMailSender")
publicJavaMailSender javaMailSender(){
    JavaMailSenderImpl javaMailSender =newJavaMailSenderImpl();
    javaMailSender.setDefaultEncoding(emailEncoding);
    javaMailSender.setHost(emailHost);
    javaMailSender.setPort(emailPort);
    javaMailSender.setUsername(emailUsername);
    javaMailSender.setPassword(emailPassword);
    Properties pp =newProperties();
    pp.setProperty("email.smtp.auth","true");
    pp.setProperty("email.smtp.timeout","50000");
    javaMailSender.setJavaMailProperties(pp);
    returnjavaMailSender;
}
/**
 * 任务线程池
 * @return
 */
@Bean(name="taskExecutor")
publicThreadPoolTaskExecutor taskExecutor(){
    ThreadPoolTaskExecutor taskExecutor =newThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(theadPoolMinSize);
    taskExecutor.setMaxPoolSize(theadPoolMaxSize);
    taskExecutor.setQueueCapacity(theadPoolQueueSize);
    taskExecutor.initialize();
    returntaskExecutor;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Autowired
privateJavaMailSender javaMailSender;
@Autowired
privateThreadPoolTaskExecutor taskExecutor;
 
/**
 * 发送邮件
 * @param recipient 收件人
 * @param subject   主题(邮件标题)
 * @param content   邮件内容
 */
publicvoidsendMail(finalString recipient,finalString subject,finalString content){
    taskExecutor.execute(newRunnable() {
        publicvoidrun() {
            try{
                SimpleMailMessage message =newSimpleMailMessage();
                message.setFrom(MAIL_FROM);
                message.setTo(recipient);
                message.setSubject(subject);
                message.setText(content);
                javaMailSender.send(message);
            }catch(Exception e) {
                logger.error("邮件发送失败:", e);
            }
        }
    });
}
77
关闭
先学习,后交费申请表
每期5位名额
在线咨询
免费电话
QQ联系
先学习,后交费
TOP
您好,您想咨询哪门课程呢?
关于我们
机构简介
官方资讯
地理位置
联系我们
0513-91107100
周一至周六     8:30-21:00
微信扫我送教程
手机端访问
南通科迅教育信息咨询有限公司     苏ICP备15009282号     联系地址:江苏省南通市人民中路23-6号新亚大厦三楼             法律顾问:江苏瑞慈律师事务所     Copyright 2008-