Email Spring邮件模块将替代javax.Mail吗

Email Spring邮件模块将替代javax.Mail吗,email,spring-boot,jakarta-mail,Email,Spring Boot,Jakarta Mail,我有一个SpringBoot应用程序,我需要向gmail帐户和Zoho帐户发送一封提醒邮件。我尝试使用Javax.mail,在那里我使用Java类设置Gmail和Zoho帐户的属性并使用它。SpringMail将是Javax.mail的最佳替代品。我怀疑是否可以使用Spring mail模块,因为我们在application.yml中设置了SMTP服务器属性。首先要做的是从Maven Central Repository导入依赖项 <dependency> <group

我有一个SpringBoot应用程序,我需要向gmail帐户和Zoho帐户发送一封提醒邮件。我尝试使用Javax.mail,在那里我使用Java类设置Gmail和Zoho帐户的属性并使用它。SpringMail将是Javax.mail的最佳替代品。我怀疑是否可以使用Spring mail模块,因为我们在application.yml中设置了SMTP服务器属性。首先要做的是从Maven Central Repository导入依赖项

<dependency>
    <groupId>it.ozimov</groupId>
    <artifactId>spring-boot-email-core</artifactId>
    <version>0.5.0</version>
</dependency>
现在,为了示例,假设您有一个发送非常简单的纯文本电子邮件的服务。它将类似于:

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

    }
让我们在主应用程序中这样做,我们将在启动和初始化Spring上下文之后发送电子邮件

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

}
请注意,要启用电子邮件工具,您需要使用注释对主应用程序进行注释

@EnableEmailTools

这将触发扩展的配置类。

Spring Mail使用
javax.Mail
那么为什么不能使用它呢?
@EnableEmailTools