0

I've generated a Spring Boot web application using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.

Technologies used:

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

I've created this service to send emails

@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("[email protected]");
            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());
        }
    }
}

But I got this error when Init the 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

and this is my 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 
0

1 Answer 1

1

You cannot have comments in the same line as the property. Everycomment must be in its own line starting with '#' The error message shows

Value: 25 # SMTP server port

so the value is the String '25 # SMTP server port' and cannot be converted into an Integer.

Move the comment in its own line, above the property :

# SMTP server port
spring.mail.port=25
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.