简体   繁体   中英

Will Spring Mail module be alternate to javax.mail

I have a Spring boot application where I need to send an alert mail to both a gmail account and a Zoho account. I try to use Javax.mail, where I set the properties of a both Gmail and a Zoho account using Java class and use it. Will Spring mail be a best replacement for Javax.mail. I have a doubt if Spring mail module can be used because we set the SMTP server properties in application.yml

The first thing to do is to import the dependency from Maven Central Repository.

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

Then, you populate the application.yml with the following entries

spring.mail.host: smtp.gmail.com
spring.mail.port: 587
spring.mail.username: hari.seldon@gmail.com
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

Now, for the sake of the example, assume that you have a service that sends a very simple plain text email. It will be something like:

    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("hari.seldon@the-foundation.gal",
                            "Hari Seldon"))
                    .to(newArrayList(
                            new InternetAddress("the-real-cleon@trantor.gov",
                            "Cleon I")))
                    .subject("You shall die! It's not me, it's Psychohistory")
                    .body("Hello Planet!")
                    .encoding("UTF-8").build();

            emailService.send(email);
        }

    }

Let's do it in the main application, where we'll send the email just after starting and initialising the Spring context.

package com.test;

import it.ozimov.springboot.mail.configuration.EnableEmailTools;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.batch.JobExecutionExitCodeGenerator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
@EnableEmailTools
public class PlainTextApplication {

    @Autowired
    private TestService testService;

    public static void main(String[] args) {
        SpringApplication.run(PlainTextApplication.class, args);
    }

    @PostConstruct
    public void sendEmail() throws UnsupportedEncodingException, InterruptedException {
        testService.sendEmail();
    }

}

Observe that to enable the Email Tools you need to annotate the main app with the annotation

@EnableEmailTools

that will trigger the configuration class for the extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM