gpt4 book ai didi

java - 无法 Autowiring javamailSender

转载 作者:行者123 更新时间:2023-12-02 01:09:45 26 4
gpt4 key购买 nike

Autowiring javamailsender 时出现以下错误:

Field javaMailSender in com.frs.services.MailService.impl.MailServiceImpl required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.

注入(inject)点有以下注释:

@org.springframework.beans.factory.annotation.Autowired(required=true)
    @Autowired
private JavaMailSender javaMailSender;

@Override
@Transactional
public void addMail(MailServiceDTO mailServiceDTO) throws MessagingException {
logger.debug(LogMarker.ENTRY, "Send Email with Subject Message to Email Addresses ");

Properties props = ((JavaMailSenderImpl) javaMailSender).getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");

MimeMessage mimeMessage = javaMailSender.createMimeMessage();

MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);

message.setFrom(mailServiceDTO.getEmailFrom());
message.setTo(mailServiceDTO.getEmailTo());
message.setSubject("Text");
javaMailSender.send(mimeMessage);

使用的依赖项:

    <!-- Mail Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

最佳答案

您需要在配置文件中提供邮件属性

# configuration email
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=email
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

或者您需要在配置文件中拥有自己的 bean

    @Bean
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

javaMailSender.setProtocol("SMTP");
javaMailSender.setHost("smtp.gmail.com");
javaMailSender.setPort(587);

return javaMailSender;
}

关于java - 无法 Autowiring javamailSender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59564045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com