【发布时间】:2021-10-23 16:45:08
【问题描述】:
我正在尝试使用 Spring Boot 发送电子邮件,但出现错误 我使用的是 11JDK,登录信息都是正确的。我在视频中做了所有事情,但我遇到了麻烦 启用安全性较低的应用
错误:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketTimeoutException: Read timed out; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Exception reading response;
应用属性:
server.port=80
server.error.include-message=always
spring.mail.username=username@gmail.com
spring.mail.password=password
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.protocol=smtp
spring.mail.test-connection=false
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
我的代码:
@Service
public class EmailService {
@Autowired
JavaMailSender sender;
public ApiResponse sendText(String email) {
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setText("This message from Spring Boot Application");
mailMessage.setSubject("Spring Boot Mail Message");
mailMessage.setTo(email);
sender.send(mailMessage);
return new ApiResponse("Sent", true);
} catch (MailException e) {
e.printStackTrace();
return new ApiResponse("Error", false);
}
}
}
Controller:
@RestController
@RequestMapping("/api")
public class EmailController {
@Autowired
EmailService emailService;
@GetMapping("/sendText/{email}")
public HttpEntity<?> sendText(@PathVariable String email) {
ApiResponse apiResponse = emailService.sendText(email);
return ResponseEntity.ok(apiResponse);
}
}
【问题讨论】:
标签: java spring-boot email gmail