当前位置:   article > 正文

springboot整合java mail_springboot整合javamial

springboot整合javamial

springboot整合java mail

1.准备工作

登录进qq邮箱,进入设置

接下来打开

打开设置:

记录下授权码,有用的

2.进行开发

2.1环境搭建

pom.xml

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

test—>用于单元测试

springbootmailstater—>用于邮件发送服务

applicaiton.properties

server.port=8080

#mail
spring.mail.host=smtp.qq.com
spring.mail.port=465
spring.mail.username=859240195@qq.com
spring.mail.protocol=smtp
spring.mail.password=你的授权码
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

mail的基本配置,没什么好说的,把授权码放在password那里,就可以了

在这里port可以设置为465或者587,都可以

2.2 发送简单邮件

package top.juntech.springbootmail.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @author juntech
 * @version ${version}
 * @date 2019/12/13 13:40
 * @ClassName 类名
 * @Descripe 发送简单邮件
 */
@Service
public class MailService {
    @Autowired
    JavaMailSender javaMailSender;

    public void sendSimpleMail(String from,String to,String cc,String subject,String content){
        /*
        * 邮件信息
        * */
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setCc(cc);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        /*
        * 发送邮件
        * */
        javaMailSender.send(simpleMailMessage);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

代码解释:

​ javamailsender是springboot在mailsenderpropertiesConfiguration类中配置好的

​ sendSimplemail的5个参数分别是:发送者,接收者,抄送这,邮件主题,邮件内容

​ 简单邮件可以直接构建一个simplemailmessage对象进行配置,配置完成后通过javamailsender发送出去

2.3单元测试

package top.juntech.springbootmail;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import top.juntech.springbootmail.service.MailService;

@SpringBootTest
class SpringbootMailApplicationTests {
    @Autowired
    private MailService mailService;

    @Test
    void contextLoads() {
        mailService.sendSimpleMail(
                "859240195@qq.com",
                "zhoujay1997@gmail.com",
                "859240195@qq.com",
                "测试邮件主题",
                "测试邮件内容"
        );
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

运行单元测试,打开控制台,打印日志

RCPT TO:<zhoujay1997@gmail.com>
250 Ok
RCPT TO:<859240195@qq.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   zhoujay1997@gmail.com
DEBUG SMTP:   859240195@qq.com
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Fri, 13 Dec 2019 14:03:21 +0800 (CST)
From: 859240195@qq.com
To: zhoujay1997@gmail.com
Cc: 859240195@qq.com
Message-ID: <66845334.0.1576217001699@Ryan>
Subject: =?UTF-8?B?5rWL6K+V6YKu5Lu25Li76aKY?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64

5rWL6K+V6YKu5Lu25YaF5a65
.
250 Ok: queued as 
DEBUG SMTP: message successfully delivered to mail server
QUIT
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在查看邮箱:

2.4发送带附件的邮件

    /*
    * 发送带附件的邮件
    * */
    public void sendAttacheFileMail(String from, String to, String cc, String subject, String content, File file){
        try{
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper();
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setText(content);
            mimeMessageHelper.setCc(cc);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.addAttachment(file.getName(),file);
            javaMailSender.send(mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

加上这段代码就可以发送带附件的邮件 了

更多详情

图片不清楚或者被屏蔽以及更多详情请访问:传送门

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/95419
推荐阅读
相关标签
  

闽ICP备14008679号