首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Mail模块会替代javax.mail吗

Spring Mail模块会替代javax.mail吗
EN

Stack Overflow用户
提问于 2018-06-19 14:44:29
回答 1查看 672关注 0票数 0

我有一个Spring启动应用程序,我需要向gmail帐户和Zoho帐户发送警报邮件。我尝试使用Javax.mail,我使用Java类设置了Gmail和Zoho帐户的属性并使用它。Spring mail会成为Javax.mail的最佳替代品吗?我怀疑是否可以使用Spring mail模块,因为我们在application.yml中设置了SMTP服务器属性

EN

回答 1

Stack Overflow用户

发布于 2018-06-19 17:33:33

要做的第一件事是从Maven Central Repository导入依赖项。

代码语言:javascript
复制
<dependency>
    <groupId>it.ozimov</groupId>
    <artifactId>spring-boot-email-core</artifactId>
    <version>0.5.0</version>
</dependency>

然后,使用以下条目填充application.yml

代码语言:javascript
复制
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

现在,为了便于示例,假设您有一个服务,它发送一个非常简单的纯文本电子邮件。它将类似于:

代码语言:javascript
复制
    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上下文之后发送电子邮件。

代码语言:javascript
复制
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();
    }

}

请注意,要启用电子邮件工具,您需要用注解注解主应用程序

代码语言:javascript
复制
@EnableEmailTools

这将触发扩展的配置类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50922130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档