【问题标题】:Mail Send Exception in Spring BootSpring Boot 中的邮件发送异常
【发布时间】: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


【解决方案1】:

如果您使用 gmail 作为邮件服务器,您需要允许不太安全的应用程序能够发送邮件。请参阅以下链接了解如何启用此功能:https://support.google.com/accounts/answer/6010255?hl=en

【讨论】:

  • 已被允许。问题不存在
猜你喜欢
  • 1970-01-01
  • 2020-11-26
  • 2015-11-07
  • 2021-01-11
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
相关资源
最近更新 更多