欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot+mail 輕松實(shí)現(xiàn)各類郵件自動(dòng)推送

 更新時(shí)間:2024年07月03日 10:08:44   投稿:mrr  
在實(shí)際的項(xiàng)目開(kāi)發(fā)過(guò)程中,經(jīng)常需要用到郵件通知功能,例如,通過(guò)郵箱注冊(cè),郵箱找回密碼,郵箱推送報(bào)表等等,實(shí)際的應(yīng)用場(chǎng)景非常的多,今天通過(guò)這篇文章,我們一起來(lái)學(xué)習(xí)如何在 Spring Boot 中快速實(shí)現(xiàn)一個(gè)自動(dòng)發(fā)送郵件的功能

一、簡(jiǎn)介

在實(shí)際的項(xiàng)目開(kāi)發(fā)過(guò)程中,經(jīng)常需要用到郵件通知功能。例如,通過(guò)郵箱注冊(cè),郵箱找回密碼,郵箱推送報(bào)表等等,實(shí)際的應(yīng)用場(chǎng)景非常的多。

早期的時(shí)候,為了能實(shí)現(xiàn)郵件的自動(dòng)發(fā)送功能,通常會(huì)使用 JavaMail 相關(guān)的 api 來(lái)完成。后來(lái) Spring 推出的 JavaMailSender 工具,進(jìn)一步簡(jiǎn)化了郵件的自動(dòng)發(fā)送過(guò)程,調(diào)用其 send 方法即可發(fā)送郵件。再之后, Spring Boot 針對(duì)郵件推送功能推出了spring-boot-starter-mail工具包,開(kāi)發(fā)者可以通過(guò)它來(lái)快速實(shí)現(xiàn)郵件發(fā)送服務(wù)。

今天通過(guò)這篇文章,我們一起來(lái)學(xué)習(xí)如何在 Spring Boot 中快速實(shí)現(xiàn)一個(gè)自動(dòng)發(fā)送郵件的功能。

二、環(huán)境準(zhǔn)備

在介紹郵件推送實(shí)現(xiàn)之前,我們需要先準(zhǔn)備一臺(tái)郵件推送的服務(wù)器,以便實(shí)現(xiàn)相關(guān)功能。

這里以騰訊郵箱為例,將其作為郵件發(fā)送的中轉(zhuǎn)平臺(tái)。

2.1、開(kāi)啟 SMTP 服務(wù)

登陸騰訊郵箱,打開(kāi)【設(shè)置】-》【收發(fā)信設(shè)置】,開(kāi)啟 SMTP 服務(wù),最后點(diǎn)擊【保存更改】。

2.2、生成客戶端專用密碼

點(diǎn)擊【設(shè)置】-》【賬戶】,進(jìn)入頁(yè)面后點(diǎn)擊【開(kāi)啟安全登陸】,點(diǎn)擊【生成新密碼】。

這個(gè)新密碼會(huì)用于郵箱的自動(dòng)發(fā)送,因此需要記錄下來(lái),最后點(diǎn)擊【保存更改】。

2.3、相關(guān)擴(kuò)展知識(shí)

  • 什么是 SMTP?

SMTP(simple mail transfer protocol),也被稱為簡(jiǎn)單郵件傳輸協(xié)議,主要用于發(fā)送電子郵件的,通過(guò)它可以實(shí)現(xiàn)郵件的發(fā)送或者中轉(zhuǎn)。遵循 SMTP 協(xié)議的服務(wù)器,通常稱為發(fā)送郵件服務(wù)器。

  • 什么是 POP3?

POP3(Post Office Protocol),一種郵局通信協(xié)議。主要用于接受電子郵件的,POP3 允許用戶從服務(wù)器上把郵件存儲(chǔ)到自己的計(jì)算機(jī)上,同時(shí)刪除保存在郵件服務(wù)器上的郵件。同理,遵循 POP3 協(xié)議的服務(wù)器,通常稱為接收郵件服務(wù)器。

  • 什么是 IMAP?

IMAP(Internet Mail Access Protocol),一種交互式郵件存取協(xié)議。與 POP3 協(xié)議類似,主要用于接收電子郵件,稍有不同的是:IMAP 允許電子郵件客戶端收取的郵件仍然保留在服務(wù)器上,同時(shí)在客戶端上的操作都會(huì)反饋到服務(wù)器上,例如刪除郵件,標(biāo)記已讀等,服務(wù)器上的郵件也會(huì)做相應(yīng)的動(dòng)作。所以無(wú)論從瀏覽器登錄郵箱或者客戶端軟件登錄郵箱,看到的郵件以及狀態(tài)都是一致的。

總結(jié)下來(lái)就是:SMTP 負(fù)責(zé)發(fā)送郵件,POP3/IMAP 負(fù)責(zé)接收郵件。

常見(jiàn)郵箱發(fā)、收服務(wù)器如下!

三、郵件推送實(shí)現(xiàn)

用于發(fā)送郵件的服務(wù)器、賬戶和密碼準(zhǔn)備好了之后,就可以正式使用了。下面我們以 Spring Boot 的 2.1.0版本為基礎(chǔ),實(shí)現(xiàn)過(guò)程如下。

2.1、添加依賴包

pom.xml文件中,添加spring-boot-starter-mail依賴包。

<!--mail 支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.2、添加相關(guān)配置

application.properties中添加郵箱相關(guān)配置。

# 配置郵件發(fā)送主機(jī)地址
spring.mail.host=smtp.exmail.qq.com
# 配置郵件發(fā)送服務(wù)端口號(hào)
spring.mail.port=465
# 配置郵件發(fā)送服務(wù)協(xié)議
spring.mail.protocol=smtp
# 配置郵件發(fā)送者用戶名或者賬戶
spring.mail.username=xxx@qq.com
# 配置郵件發(fā)送者密碼或者授權(quán)碼
spring.mail.password=xxxxxxx
# 配置郵件默認(rèn)編碼
spring.mail.default-encoding=UTF-8
# 配置smtp相關(guān)屬性
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.ssl.required=true

2.3、簡(jiǎn)單發(fā)送一封郵件

通過(guò)單元測(cè)試來(lái)實(shí)現(xiàn)一封簡(jiǎn)單郵件的發(fā)送,示例如下:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailSimpleTest {
    @Autowired
    private JavaMailSender mailSender;
    @Test
    public void sendSimpleMail() throws Exception {
        SimpleMailMessage message = new SimpleMailMessage();
        // 配置發(fā)送者郵箱
        message.setFrom("xxxx@qq.com");
        // 配置接受者郵箱
        message.setTo("xxxxxx@qq.com");
        // 配置郵件主題
        message.setSubject("主題:簡(jiǎn)單郵件");
        // 配置郵件內(nèi)容
        message.setText("測(cè)試郵件內(nèi)容");
        // 發(fā)送郵件
        mailSender.send(message);
    }
}

運(yùn)行單元測(cè)試之后,如果不出意外的話,接受者會(huì)收到這樣的一封郵件。

至此,郵件發(fā)送成功!

2.4、發(fā)送 HTML 格式郵件

在實(shí)際的業(yè)務(wù)開(kāi)發(fā)中,郵件的內(nèi)容通常會(huì)要求豐富,比如會(huì)發(fā)送一些帶有圖片的內(nèi)容,包括字體大小,各種超鏈接等,這個(gè)時(shí)候如何實(shí)現(xiàn)呢?

實(shí)際上,郵件內(nèi)容支持 HTML 格式,因此可以借助頁(yè)面模板引擎來(lái)實(shí)現(xiàn)絢麗多彩的內(nèi)容。

下面我們以freemarker模板引擎為例,發(fā)送一封內(nèi)容為 HTML 格式的郵件。

2.4.1、引入 freemarker 依賴包

首先,在pom.xml文件中,添加freemarker依賴包。

<!--freemarker 支持-->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>

???????2.4.2、編寫(xiě)郵件頁(yè)面模板

然后,在resources/templates目錄下,創(chuàng)建一個(gè)demo.ftl文件,示例如下!

<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<div>您好:${userName}</div>
<div>這是html文本內(nèi)容</div>
<img src="https://rescdn.qqmail.com/zh_CN/htmledition/images/logo/logo_0_0@2X1f1937.png" />
</body>
</html>

雖然采用 Spring Boot 提供的自動(dòng)配置屬性來(lái)實(shí)現(xiàn)郵件推送,可以極大的簡(jiǎn)化開(kāi)發(fā)過(guò)程。而實(shí)際開(kāi)發(fā)的時(shí)候,通常更推薦自定義一個(gè)郵件統(tǒng)一推送服務(wù),這樣更便于靈活的控制代碼實(shí)現(xiàn)以及排查相關(guān)問(wèn)題。

郵件統(tǒng)一發(fā)送服務(wù),示范如下。

@Component
public class MailPushService {
    private final Logger LOGGER = LoggerFactory.getLogger(MailPushService.class);
    @Value("${mail.host}")
    private String host;
    @Value("${mail.port}")
    private String port;
    @Value("${mail.protocol}")
    private String protocol;
    @Value("${mail.username}")
    private String username;
    @Value("${mail.password}")
    private String password;
    @Value("${mail.fromEmail}")
    private String fromEmail;
    @Value("${mail.fromPersonal}")
    private String fromPersonal;
    @Autowired
    private JavaMailSender mailSender;
    /**
     * 發(fā)送郵件(簡(jiǎn)單模式)
     * @param toEmail
     * @param subject
     * @param content
     */
    public void sendMail(String toEmail, String subject,String content)  {
        try {
            final Properties props = new Properties();
            //服務(wù)器
            props.put("mail.smtp.host", host);
            //端口
            props.put("mail.smtp.port", port);
            //協(xié)議
            props.setProperty("mail.transport.protocol", protocol);
            //用戶名
            props.put("mail.user", username);
            //密碼
            props.put("mail.password", password);
            //使用smtp身份驗(yàn)證
            props.put("mail.smtp.auth", "true");
            //開(kāi)啟安全協(xié)議
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(props.getProperty("mail.user"),
                            props.getProperty("mail.password"));
                }
            };
            Session session = Session.getDefaultInstance(props, authenticator);
            session.setDebug(true);
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress(fromEmail, MimeUtility.encodeText(fromPersonal)));
            mimeMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail));
            mimeMessage.setSubject(subject);
            mimeMessage.setContent(content, "text/html;charset=UTF-8");
            //保存信息
            mimeMessage.saveChanges();
            //發(fā)送消息
            Transport.send(mimeMessage);
            LOGGER.info("簡(jiǎn)單郵件已經(jīng)發(fā)送。");
        } catch (Exception e) {
            LOGGER.error("發(fā)送簡(jiǎn)單郵件時(shí)發(fā)生異常!", e);
        }
    }
}

代碼中相關(guān)自定義的全局參數(shù)配置如下:

???????
mail.host=smtp.exmail.qq.com
mail.port=465
mail.protocol=smtp
mail.username=xxx@qq.com
mail.password=xxxxxx
mail.fromEmail=xxxxxx@qq.com
mail.fromPersonal=發(fā)送者昵稱

???????2.4.4、測(cè)試服務(wù)的正確性

最后,編寫(xiě)一個(gè)單元測(cè)試來(lái)驗(yàn)證服務(wù)的正確性,示例如下:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {
    @Autowired
    private MailPushService mailPushService;
    @Test
    public void testSendHtmlMail() throws Exception {
        String sendHtml = buildHtmlContent("張三");
        mailPushService.sendMail("xxxxx@qq.com","簡(jiǎn)單標(biāo)題", sendHtml);
    }
    /**
     * 封裝html頁(yè)面
     * @return
     * @throws Exception
     */
    private static String buildHtmlContent(String userName) throws Exception {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        configuration.setDefaultEncoding(Charset.forName("UTF-8").name());
        configuration.setClassForTemplateLoading(MailTest.class, "/templates");
        // 獲取頁(yè)面模版
        Template template = configuration.getTemplate("demo.ftl");
        // 動(dòng)態(tài)變量替換
        Map<String,Object> map = new HashMap<>();
        map.put("userName", userName);
        String htmlStr = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
        return htmlStr;
    }
}

運(yùn)行單元測(cè)試之后,如果沒(méi)有報(bào)錯(cuò),接受者會(huì)收到這樣的一封郵件。

2.5、發(fā)送帶附件的郵件

某些業(yè)務(wù)場(chǎng)景,用戶希望發(fā)送的郵件中能帶上附件,比如上文中,在發(fā)送 HTML 格式的郵件時(shí),同時(shí)也帶上文件附件,這個(gè)時(shí)候如何實(shí)現(xiàn)呢?

2.5.1、編寫(xiě)帶附件的郵件發(fā)送

此時(shí)可以在郵件推送服務(wù)中,新增一個(gè)支持帶附件的方法,實(shí)現(xiàn)邏輯如下。

/**
 * 發(fā)送郵件(復(fù)雜模式)
 * @param toEmail    接受者郵箱
 * @param subject    主題
 * @param sendHtml   內(nèi)容
 * @param attachment 附件
 */
public void sendMail(String toEmail, String subject, String sendHtml, File attachment) {
    try {
        //設(shè)置了附件名過(guò)長(zhǎng)問(wèn)題
        System.setProperty("mail.mime.splitlongparameters", "false");
        final Properties props = new Properties();
        //服務(wù)器
        props.put("mail.smtp.host", host);
        //端口
        props.put("mail.smtp.port", port);
        //協(xié)議
        props.setProperty("mail.transport.protocol", protocol);
        //用戶名
        props.put("mail.user", username);
        //密碼
        props.put("mail.password", password);
        //使用smtp身份驗(yàn)證
        props.put("mail.smtp.auth", "true");
        //開(kāi)啟安全協(xié)議
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(props.getProperty("mail.user"),
                        props.getProperty("mail.password"));
            }
        };
        Session session = Session.getDefaultInstance(props, authenticator);
        session.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(session);
        // 發(fā)送者郵箱
        mimeMessage.setFrom(new InternetAddress(fromEmail, MimeUtility.encodeText(fromPersonal)));
        // 接受者郵箱
        mimeMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail));
        // 郵件主題
        mimeMessage.setSubject(subject);
        // 定義郵件內(nèi)容
        Multipart multipart = new MimeMultipart();
        // 添加郵件正文
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(sendHtml, "text/html;charset=UTF-8");
        multipart.addBodyPart(contentPart);
        // 添加附件
        if (attachment != null) {
            BodyPart attachmentBodyPart = new MimeBodyPart();
            // MimeUtility.encodeWord可以避免文件名亂碼
            FileDataSource fds=new FileDataSource(attachment);
            attachmentBodyPart.setDataHandler(new DataHandler(fds));
            attachmentBodyPart.setFileName(MimeUtility.encodeText(fds.getName()));
            multipart.addBodyPart(attachmentBodyPart);
        }
        // 將multipart對(duì)象放到message中
        mimeMessage.setContent(multipart);
        //保存信息
        mimeMessage.saveChanges();
        //發(fā)送消息
        Transport.send(mimeMessage);
        LOGGER.info("郵件已經(jīng)發(fā)送。");
    } catch (Exception e) {
        LOGGER.error("發(fā)送郵件時(shí)發(fā)生異常!", e);
    }
}

???????2.5.2、測(cè)試服務(wù)的正確性

最后,編寫(xiě)一個(gè)單元測(cè)試來(lái)驗(yàn)證服務(wù)的正確性,示例如下:

@Test
public void doSendHtmlEmail() throws Exception {
    // 獲取正文內(nèi)容
    String sendHtml = buildHtmlContent("張三");
    // 獲取附件
    File file = new File( "~/doc/Java開(kāi)發(fā)手冊(cè).pdf");
    // 發(fā)送郵件
    mailPushService.sendMail("xxxxx@qq.com","帶附件的郵件推送", sendHtml, file);
}

運(yùn)行單元測(cè)試之后,如果沒(méi)有報(bào)錯(cuò),接受者會(huì)收到這樣的一封郵件。

三、小結(jié)

最后總結(jié)一下,郵件自動(dòng)推送功能在實(shí)際的業(yè)務(wù)系統(tǒng)中應(yīng)用非常廣,在發(fā)送過(guò)程中也可能會(huì)因?yàn)榫W(wǎng)絡(luò)問(wèn)題出現(xiàn)各種失敗現(xiàn)象,因此推薦采用異步的方式來(lái)發(fā)送郵件,例如采用異步編程或者消息隊(duì)列來(lái)實(shí)現(xiàn),以便加快主流程的執(zhí)行速度。

想要獲取項(xiàng)目源代碼的小伙伴,可以訪問(wèn)如下地址獲?。?/p>

https://gitee.com/pzblogs/spring-boot-example-demo

四、參考資料

1.https://blog.csdn.net/qq_26383975/article/details/121957917

1.http://www.ityouknow.com/springboot/2017/05/06/spring-boot-mail.html

到此這篇關(guān)于SpringBoot+mail 輕松實(shí)現(xiàn)各類郵件自動(dòng)推送的文章就介紹到這了,更多相關(guān)SpringBoot郵件自動(dòng)推送內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論