2017-02-23 161 views
1

我使用it.ozimov庫實現了電子郵件服務。當所有東西都被導入時,發送方法有問題。我無法弄清楚它應該如何導入,因爲現在服務看不到它。it.ozimov的電子郵件服務無法看到發送方法

這依賴其附上

<dependency> 
     <groupId>it.ozimov</groupId> 
     <artifactId>spring-boot-email-core</artifactId> 
     <version>0.4.2</version> 
    </dependency> 
    <dependency> 
     <groupId>it.ozimov</groupId> 
     <artifactId>spring-boot-freemarker-email</artifactId> 
     <version>0.4.2</version> 
    </dependency> 

在這裏它是一個業務碼

@Autowired 
public EmailService emailService; 

public void sendEmailWithoutTemplating() throws UnsupportedEncodingException { 
    final Email email = DefaultEmail.builder() 
      .from(new InternetAddress("[email protected]", "Marco Tullio Cicerone ")) 
      .to(Lists.newArrayList(new InternetAddress("[email protected]", "Pomponius Attĭcus"))) 
      .subject("Laelius de amicitia") 
      .body("Firmamentum autem stabilitatis constantiaeque eius, quam in amicitia quaerimus, fides est.") 
      .encoding(String.valueOf(Charset.forName("UTF-8"))).build(); 

    emailService.send(email); 
} 

當然,我加入下面的代碼在屬性:

spring.mail.host=smtp.gmail.com 
spring.mail.port=587 
[email protected] 
spring.mail.password=V3ry_Str0ng_Password 
spring.mail.properties.mail.smtp.auth=true 
spring.mail.properties.mail.smtp.starttls.enable=true 
spring.mail.properties.mail.smtp.starttls.required=true 

spring.mail.scheduler.persistence.enabled=false 
spring.mail.scheduler.persistence.redis.embedded=false 
spring.mail.scheduler.persistence.redis.enabled=false 

回答

1

首先,更新依賴關係:

<dependency> 
    <groupId>it.ozimov</groupId> 
    <artifactId>spring-boot-email-core</artifactId> 
    <version>0.5.0</version> 
</dependency> 
<dependency> 
    <groupId>it.ozimov</groupId> 
    <artifactId>spring-boot-freemarker-email</artifactId> 
    <version>0.5.0</version> 
</dependency> 

然後,設置應用程序屬性:

spring.mail.host: smtp.gmail.com 
spring.mail.port: 587 
spring.mail.username: [email protected] 
spring.mail.password: Th3MuleWh0 
spring.mail.properties.mail.smtp.auth: true 
spring.mail.properties.mail.smtp.starttls.enable: true 
spring.mail.properties.mail.smtp.starttls.required: true 

最後創建一個測試服務

package com.test; 

import com.google.common.collect.Lists; 
import it.ozimov.springboot.mail.model.Email; 
import it.ozimov.springboot.mail.model.defaultimpl.DefaultEmail; 
import it.ozimov.springboot.mail.service.EmailService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

import javax.mail.internet.InternetAddress; 
import java.io.UnsupportedEncodingException; 

import static com.google.common.collect.Lists.newArrayList; 

@Service 
public class TestService { 

    @Autowired 
    private EmailService emailService; 

    public void sendEmail() throws UnsupportedEncodingException { 
     final Email email = DefaultEmail.builder() 
       .from(new InternetAddress("[email protected]", 
         "Hari Seldon")) 
       .to(newArrayList(
         new InternetAddress("[email protected]", 
         "Cleon I"))) 
       .subject("You shall die! It's not me, it's Psychohistory") 
       .body("Hello Planet!") 
       .encoding("UTF-8").build(); 

     emailService.send(email); 
    } 

} 

收費極端重視導入的包。

最後,你需要使用註釋

@EnableEmailTools 

你可以找到更多的this article,使您的主應用程序的擴展名。