bielas
bielas

Reputation: 712

Email service by it.ozimov can't see the send method

I implemented an email service form it.ozimov library. When everything was imported there is a problem with send method. I can't figure out how it should be imported, cause now the service can't see it.

Here it is dependency which I attach

  <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>

Here it is a service code

@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);
}

Of course I added below code at properties:

    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

Upvotes: 1

Views: 491

Answers (1)

JeanValjean
JeanValjean

Reputation: 17713

First, update the dependencies:

<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>

Then, set the application properties:

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

Finally create a test service

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);
    }

}

Pay extreme attention to the packages being imported.

Finally, you need to enable the extension in your main app using the annotation

@EnableEmailTools

You can find more in this article.

Upvotes: 1

Related Questions