gpt4 book ai didi

java - 在 Spring Boot 中使用 JavaMailSender

转载 作者:行者123 更新时间:2023-12-02 13:41:21 25 4
gpt4 key购买 nike

我使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎生成了一个 Spring Boot Web 应用程序,并将其打包为可执行 JAR 文件。

使用的技术:

Spring Boot 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat Embed 8.5.6、Maven 3、Java 8

我创建了此服务来发送电子邮件

@Service
public class MailClient {

protected static final Logger looger = LoggerFactory.getLogger(MailClient.class);

@Autowired
private JavaMailSender mailSender;

private MailContentBuilder mailContentBuilder;

@Autowired
public MailClient(JavaMailSender mailSender, MailContentBuilder mailContentBuilder) {
this.mailSender = mailSender;
this.mailContentBuilder = mailContentBuilder;
}

//TODO: in a properties
public void prepareAndSend(String recipient, String message) {
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("nunito@calzada.com");
messageHelper.setTo(recipient);
messageHelper.setSubject("Sample mail subject");
String content = mailContentBuilder.build(message);
messageHelper.setText(content, true);
};
try {
if (looger.isDebugEnabled()) {
looger.debug("sending email to " + recipient);
}
mailSender.send(messagePreparator);
} catch (MailException e) {
looger.error(e.getMessage());
}
}
}

但是在初始化 SpringBootApplication 时出现此错误

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target org.springframework.boot.autoconfigure.mail.MailProperties@1e94ed11 failed:

Property: spring.mail.defaultEncoding
Value: UTF-8
Reason: Failed to convert property value of type 'java.lang.String' to required type 'java.nio.charset.Charset' for property 'defaultEncoding'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.nio.charset.Charset]


Action:

Update your application's configuration

这是我的 application.properties

spring.mail.host=localhost
spring.mail.port=25
spring.mail.username=
spring.mail.password=
spring.mail.protocol=smtp
spring.mail.defaultEncoding=UTF-8

最佳答案

注释不能与属性位于同一行。每个注释必须在自己的行中,以“#”开头错误信息显示

Value: 25 # SMTP server port

所以该值为字符串'25 # SMTP服务器端口',无法转换为整数。

将注释移到其自己的行中,位于属性上方:

# SMTP server port
spring.mail.port=25

关于java - 在 Spring Boot 中使用 JavaMailSender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42726317/

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