SpringBoot中Mail的使用

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

    }

}

本文名称:SpringBoot中Mail的使用
本文路径:https://www.cdcxhl.com/article46/jophhg.html

成都网站建设公司_创新互联,为您提供定制开发静态网站建站公司网站排名ChatGPT品牌网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

微信小程序开发