SpringBoot中Mail的使用

发布时间:2020-07-28 17:11:07 作者:素颜猪
来源:网络 阅读:639

1.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>activiti.demo</groupId>

    <artifactId>activiti-demo</artifactId>

    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>


    <name>activiti-demo</name>

    <description>spring-activiti-demo</description>


    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.6.RELEASE</version>

        <relativePath/>

    </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-mail</artifactId>

        </dependency>


        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-devtools</artifactId>

            <optional>true</optional>

        </dependency>


        <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>

    </dependencies>


    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

                <configuration>

                    <fork>true</fork>

                </configuration>

            </plugin>

        </plugins>

    </build>

</project>

2.属性文件配置如下

# 端口号

server.port=8082

spring.mail.host=smtp.163.com

spring.mail.username=xx@163.com

spring.mail.password=xxx

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true

3.测试类如下

package com.springboot.demo.controller;


import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.core.io.FileSystemResource;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.MimeMessageHelper;

import org.springframework.test.context.junit4.SpringRunner;


import javax.mail.internet.MimeMessage;

import java.io.File;


/**

 * @ClassName LeaveControllerTest

 * @Description TODO

 * @Author yunshuodeng

 * @Date 2019-05-06 11:03

 * @Version 1.0

 **/

@RunWith(SpringRunner.class)

@SpringBootTest

public class EmailSendTest {


    @Autowired

    private JavaMailSender javaMailSender;


    @Value("${spring.mail.username}")

    private String senderUserName;


    /**

     * 发送带静态资源的邮件-单元测试

     */

    @Test

    public void sendInlineMail() throws Exception{

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);

        mimeMessageHelper.setFrom(senderUserName);

        mimeMessageHelper.setTo("691258810@qq.com");

        mimeMessageHelper.setSubject("发送带静态资源邮件");

        mimeMessageHelper.setText("<html><body>带静态资源的邮件内容 图片:<img src='cid:picture' /></body></html>",true);

        FileSystemResource fileSystemResource = new FileSystemResource(new File("/Users/yunshuodeng/Downloads/QQ20190507-102734.png"));

        mimeMessageHelper.addInline("picture",fileSystemResource);

        javaMailSender.send(mimeMessage);

    }


    /**

     * 发送带附件的邮件-单元测试

     */

    @Test

    public void sendAttachmentsMail() throws Exception{

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);

        mimeMessageHelper.setFrom(senderUserName);

        mimeMessageHelper.setTo("691258810@qq.com");

        mimeMessageHelper.setSubject("发送带附件邮件");

        mimeMessageHelper.setText("这是一个有附件的邮件");

        // 附件

        FileSystemResource fileSystemResource = new FileSystemResource(new File("/Users/yunshuodeng/Downloads/leavedemo.bpmn20.xml"));

        mimeMessageHelper.addAttachment("leavedemo.bpmn20.xml",fileSystemResource);

        javaMailSender.send(mimeMessage);

    }


    /**

     * 发送html邮件-单元测试

     */

    @Test

    public void sendHtmlMail() throws Exception{

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);

        mimeMessageHelper.setFrom(senderUserName);

        mimeMessageHelper.setTo("2247053450@qq.com");

        mimeMessageHelper.setSubject("发送html邮件");

        StringBuffer stringBuffer = new StringBuffer();

        stringBuffer.append("<h2>大标题</h2>");

        stringBuffer.append("<h3>二标题</h3>");

        stringBuffer.append("<h4>三标题</h4>");

        mimeMessageHelper.setText(stringBuffer.toString(),true);

        javaMailSender.send(mimeMessage);

    }


    /**

     * 发送简单文本邮件-单元测试

     */

    @Test

    public void sendSimpleTextMail() throws Exception{

        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        // 发送人

        simpleMailMessage.setFrom(senderUserName);

        // 接收人

        simpleMailMessage.setTo("691258810@qq.com");

        // 标题

        simpleMailMessage.setSubject("发送简单文本邮件");

        // 内容

        simpleMailMessage.setText("这是文本描述");

        javaMailSender.send(simpleMailMessage);

    }

}



推荐阅读:
  1. 关于php中使用mail()不能发邮件的问题
  2. SpringBoot导入mail依赖报错问题解决

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot mail

上一篇:sqlserver阻止保存要求重新建立表的更改

下一篇:PMP项目管理软件有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》