2017-03-10 64 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 
相关问题