Fork me on GitHub

SpringBoot整合Email遇到的问题

今天,在学习SpringBoot整合Email时,遇到一个异常:java.net.UnknownHostException: smtp.qq.com ,Couldn’t connect to host, port: smtp.qq.com , 25; timeout -1;用的是QQ邮箱的POP3/SMTP服务。

分析问题:显示不能连接主机,端口:smtp.qq.com 25,代码没问题。一访问Controller就显示500错误。可能情况:

1.要在邮件设置中把POP3/SMTP服务开启。

2.ping或者telnet下smtp.qq.com,看是否接通

3.改变下端口号

4.如有安装要杀毒软件或防火墙,比如:McAFee 要在控制台->访问保护(右键)->属性->防病毒标准保护->禁止群发邮件蠕虫发送邮件->前面没有打钩

更改前配置代码

1
2
3
4
5
6
spring.mail.host=smtp.qq.com 
spring.mail.username=719196335@qq.com
spring.mail.password=yxkcobqdkhmjbcac
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

更改后如下代码:

配置文件代码

1
2
3
4
5
6
7
8
9
10
11
spring.mail.host=smtp.qq.com
spring.mail.username=719196335@qq.com
spring.mail.password=qhimjcetrjmbbdhb
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.qq.com
spring.mail.properties.mail.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.port=465
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

EmialConfig代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package net.zzqd.email;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class EmailConfig {
@Value("${spring.mail.username}")
private String emailForm;


public String getEmailForm() {
return emailForm;
}

public void setEmailForm(String emailForm) {
this.emailForm = emailForm;
}

}

EmialController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package net.zzqd.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class EmailController {
@Autowired
private EmailService emailService;

@RequestMapping("/simple")
@ResponseBody
public String sendSimpleEmail()
{
emailService.sendSimpleMail("719196335@qq.com", "你好","明天找你玩");
return "success";
}
}

EmailService

1
2
3
4
5
6
package net.zzqd.email;

public interface EmailService {
//发送简单的邮件
void sendSimpleMail(String sendTo, String title, String content);
}

EmailServiceImpl

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
package net.zzqd.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailServiceImpl implements EmailService{

@Autowired
private EmailConfig emailConfig;

@Autowired
private JavaMailSender mailSender;

@Override
public void sendSimpleMail(String sendTo,String title,String content) {
//简单邮件的发送
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(emailConfig.getEmailForm());
message.setTo(sendTo);
message.setSubject(title);
message.setText(content);

mailSender.send(message);
}
}

附加:经过上述一切判断发现都没问题,有将端口号改为465,发现出现501错误码,了解以后发现是从application.properties获取的账号和Message中设置的不一致造成的,在ServiceImpl中添加控制台打印发现是null,仔细观察了代码,发现注解写错位置了。如下图,完美解决。