从Spring Boot Aplication MailSendException发送电子邮件

时间:2018-12-22 07:30:59

标签: spring spring-boot email

Iam正在使用Spring Boot,我想将Spring Boot应用程序中的电子邮件发送给注册该应用程序的用户,但是Iam收到以下异常

org.springframework.mail.MailSendException: Failed messages: java.lang.NullPointerException

属性文件中的电子邮件配置为

spring.mail.default-encoding=UTF-8
spring.mail.protocol=smtp
spring.mail.host= smtp.gmail.com
spring.mail.username= myeamil@gmail.com
spring.mail.password= password
spring.mail.port= 587
spring.mail.properties.mail.smtp.starttls.enable= true
spring.mail.properties.mail.auth= true

这是我的服务记录中的代码

     @Autowired
    private JavaMailSender mailSender;

    private SimpleMailMessage simpleMailMessage=new SimpleMailMessage();

public ResponseDTO saveUser(UserDTO userDTO) {
            userRepositry.save(userDTO);
            simpleMailMessage.setFrom("myemail@gmail.com");
            simpleMailMessage.setSubject("some data");
            simpleMailMessage.setText("some data");
            mailSender.send(simpleMailMessage);
            simpleMailMessage.setTo(userDTO.getEmail());
            return new ResponseDTO("Registeration is done successfully..",true);

    }

1 个答案:

答案 0 :(得分:0)

问题出在我的服务类中,并且通过将代码更改为以下代码来解决了该问题。

 @Autowired
private JavaMailSender mailSender;
private SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
    public ResponseDTO saveUser(UserDTO userDTO) {
        userRepositry.save(userDTO);
        simpleMailMessage.setFrom("myemail@gmail.com");
        simpleMailMessage.setTo(userDTO.getEmail());
        simpleMailMessage.setSubject("some data");
        simpleMailMessage.setText("some data");
        mailSender.send(simpleMailMessage);
        return new ResponseDTO("Registeration is done successfully..",true);
     }