我为我的spring-boot应用程序编写了简单的mailsender。但是我在创建bean时遇到问题。这是我的MailSender类

@Component
public class MailSender {
    @Autowired
    private JavaMailSender javaMailSender;

    public void send() {
        MimeMessage mail = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mail, true);
            helper.setTo("[email protected]");
            helper.setReplyTo("");
            helper.setFrom("[email protected]");
            helper.setSubject("Lorem ipsum");
            helper.setText("Lorem ipsum dolor sit amet [...]");
        } catch (MessagingException e) {
            e.printStackTrace();
        } finally {}
        javaMailSender.send(mail);
        System.out.println("Mail has been sent !");

    }

}


接下来,我尝试在主类中创建自动装配的MailSender实例,这是我收到错误的地方

@SpringBootApplication

public class myApplication {

    private static final Logger LOGGER = LoggerFactory.getLogger(myApplication.class);
    @Autowired
    private MailReceiver mailReceiver;
    @Autowired
    private MailSender ms;

    public static void main(String[] args) {
        SpringApplication.run(myApplication.class, "--debug");
    }
...
//more code


错误是

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myApplication': Injection
of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private info.some.mail.MailSender
info.some.myApplication.ms; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [info.some.mail.MailSender]


你能帮我吗

编辑:
当然,我的build.gradle文件中包含spring-boot-starter-mail

最佳答案

好的,我什至不知道这怎么可能,但是我将类名从“ MailSender”更改为其他名称后,一切都开始正常运行

09-26 06:36