【问题标题】:Spring: Not receiving messages on emailSpring:不接收电子邮件消息
【发布时间】:2020-01-04 11:17:13
【问题描述】:

我没有通过电子邮件帐户收到激活注册的消息。 注册时,我在数据库中看到了激活码,并且可以登录,但我想向激活后的帐户发送电子邮件。

我尝试更改端口并尝试更改“smtp”上的“smtps” 我的属性:

spring.mail.host=smtp.gmail.com
spring.mail.username=mymail@gmail.com
spring.mail.password=mypass
spring.mail.port=587
spring.mail.protocol= smtps
spring.mail.properties.mail.smtp.auth = true;
spring.mail.properties.mail.smtp.starttls.enable = true;
mail.debug= true

邮件配置:

@Configuration
public class MailConfig {

    @Value("${spring.mail.host}")
    private String host;
    @Value("${spring.mail.username}")
    private String username;
    @Value("${spring.mail.password}")
    private String password;
    @Value("${spring.mail.port}")
    private int port;
    @Value("${spring.mail.protocol}")
    private String protocol;
    @Value("${mail.debug}")
    private String debug;
    @Value("${spring.mail.properties.mail.smtp.auth}")
    private String auth;
    @Value("${spring.mail.properties.mail.smtp.starttls.enable}")
    private String enable;

    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);
        mailSender.setUsername(username);
        mailSender.setPassword(password);

        Properties properties = mailSender.getJavaMailProperties();

        properties.setProperty("mail.transport.protocol", protocol);
        properties.setProperty("mail.debug", debug);
        properties.setProperty("mail.smtp.auth", auth);
        properties.setProperty("mail.smtp.starttls.enable", enable);

        return mailSender;
    }

}

用户服务:

@Service
public class UserService implements UserDetailsService {

    private final UserRepository userRepository;
    private final MailSender mailSender;

    public UserService(UserRepository userRepository, MailSender mailSender) {
        this.userRepository = userRepository;
        this.mailSender = mailSender;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userRepository.findByUsername(username);
    }

    public boolean registerUser(User user) {
        User userFromDb = userRepository.findByUsername(user.getUsername());

        if (userFromDb != null) {
            return false;
        }

        user.setActive(true);
        user.setRoles(Collections.singleton(Role.USER));
        user.setActivationCode(UUID.randomUUID().toString());
        userRepository.save(user);

        if (StringUtils.isEmpty(user.getEmail())) {
            String message = String.format("Hello, %s \n" + "Welcome to Sweater. Please visit link: http://localhost:8080/activate/%s",
                    user.getUsername(),
                    user.getActivationCode()
            );
            mailSender.send(user.getEmail(), "Activation code", message);
        }
        return true;
    }

    public boolean activateCode(String code) {
        User user = userRepository.findByActivationCode(code);

        if (user == null) {
            return false;
        }

        user.setActivationCode(null);
        userRepository.save(user);

        return true;
    }
}

控制器

@GetMapping("/activate/{code}")
    public String activate(Model model, @PathVariable String code) {

        boolean isActivated = userService.activateCode(code);

        if(isActivated){
            model.addAttribute("info", "User activated");
        }else {
            model.addAttribute("info", "Activation code is not found");
        }

        return "login";

    }

【问题讨论】:

  • @AutowiredMailSender
  • @PatelRomil 但为什么呢?我听说私有 final + 构造函数比 Autowired 更好。但是当我将所有内容都更改为 Autowired 时,它就可以工作了)

标签: java spring spring-boot email dependency-injection


【解决方案1】:

@AutowiredMailSender

依赖注入:spring 中的@Autowired 注释自动将依赖bean注入到POJO 类的关联引用中。此注解将通过匹配数据类型(即在内部作为 Autowiring byType 工作)来注入依赖 bean。我们可以

  • @Autowired 在财产上
  • @Autowired 关于 setter 方法
  • @Autowired 在构造函数上(优于单个字段自动装配)

【讨论】:

  • 所以我必须自动编写构造函数?因为我想,如果使用构造函数,那么我不需要使用注解 Autowired
  • 您可以自动装配构造和特定属性,这取决于我们,但拥有构造函数与依赖注入无关。
猜你喜欢
  • 2017-12-05
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
相关资源
最近更新 更多