1. 程式人生 > >springboot簡易使用郵件功能

springboot簡易使用郵件功能

寫在前面:本文中使用126郵箱進行測試

程式碼地址: https://github.com/Blankwhiter/email

一、註冊郵箱以及開啟POP3/SMTP/IMAP

讀者請自行註冊,以及自行開啟POP3/SMTP/IMAP。
在開啟POP3/SMTP/IMAP過程中,會要求輸入一個授權碼,請記住,接下來將使用該授權密碼進行郵件傳送。

二、編寫springboot配置檔案以及程式碼

1.引入email環境。
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.email</groupId> <artifactId>demo</artifactId> <version
>
0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>email-springboot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>
spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--引入email環境--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2.編寫配置檔案
application.yml

spring:
  mail:
    username: [email protected]
    password: XXXXX (第一步的授權密碼)
    host: smtp.126.com

#    qq郵箱需要多設定如下
#    properties:
#              mail:
#                smtp:
#                  ssl:
#                    enable: true

3.測試程式碼


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.internet.MimeMessage;
import java.io.File;


@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailSpringbootApplicationTests {


    @Autowired
    JavaMailSenderImpl javaMailSender;

    /**
     * 傳送簡單郵件
     */
    @Test
    public void testSimpleMail() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("這個是標題");
        simpleMailMessage.setText("這個是測試內容");
        simpleMailMessage.setFrom("[email protected]");
        simpleMailMessage.setTo("[email protected]");
        javaMailSender.send(simpleMailMessage);
    }

    /**
     * 傳送複雜郵件
     */
    @Test
    public void testMimeMail() throws Exception{
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //第二個引數 郵件是否要附件
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setSubject("it is also title ");
        //第二個引數 內容是否按html解析
        mimeMessageHelper.setText(" <b> it is a content </a>",true);
        mimeMessageHelper.setFrom("[email protected]");
        mimeMessageHelper.setTo("[email protected]");
        mimeMessageHelper.addAttachment("banner.png",new File("C:\\Users\\monkey\\Pictures\\banner.png"));

        javaMailSender.send(mimeMessage);
    }

}