2017-03-10 67 views
0

我使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎以及程序包生成了一個Spring Boot Web應用程序作爲可執行JAR文件。使用在SpringBoot中使用JavaMailSender

技術:

春季啓動1.4.2.RELEASE,春天4.3.4.RELEASE,Thymeleaf 2.1.5.RELEASE,Tomcat的嵌入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("[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()); 
     } 
    } 
} 

但我得到這個錯誤時初始化的SpringBootApplication

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

Description: 

Binding to target [email protected]d11 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 

回答

1

您不能在屬性的同一行註釋。 Everycomment必須在其自己的行以「#」 開始的錯誤信息顯示

Value: 25 # SMTP server port 

因此該值是字符串'25#SMTP服務器端口」,並且不能被轉換成一個整數。

移動在評論自己的路線,物業上面:

# SMTP server port 
spring.mail.port=25 
相關問題