当前位置:   article > 正文

Springboot之整合mail_springboot 整合mail

springboot 整合mail

Springboot之整合mail

前言

Spring Email抽象的核心是MailSender接口,MailSender的实现能够将想要发送的邮件通过邮件服务器发送到用户邮箱,实现发送邮箱的功能。

发送邮件
MailSender接口
邮件服务器
用户邮箱

Spring 自带了一个 MailSender 的实现 JavaMailSenderImpl,它会使用 JavaMail API 来发送 Email。Spring 或 SpringBoot 应用在发送 Email 之前,我们必须要 JavaMailSenderImpl 装配为 Spring应用上下文的一个 bean。

配置

pom配置

   <!--引入mail依赖-->
 <dependency>
	 <groupId>org.springframework.boot</groupId>
	 <artifactId>spring-boot-starter-mail</artifactId>
 </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

application.properties文件配置

  • qq邮箱
# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.qq.com
spring.mail.username=用户qq邮箱
#QQ邮箱的授权码
spring.mail.password=授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 163邮箱
spring.mail.host=smtp.163.com
spring.mail.username=用户163邮箱
spring.mail.password=邮箱密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

host 属性默认是 JavaMail 会话的主机;port 端口默认监听标准的 SMTP 端口25;如果邮件服务器需要认证的,还需要设置 userrname 和 password。
通常username是邮箱号,password为邮箱的授权码。授权码可以登录邮箱获取。
设置->账户->POP3/SMTP服务:开启服务后会获得的授权码.

EmailConfig实体类

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

/**
 * @Author youjp
 * @Description //TODO
 * @throw
 **/
@Component
public class EmailConfig {

    @Value("${spring.mail.username}")
    private String  emailForm;

    public String getEmailForm() {
        return emailForm;
    }

    public void setEmailForm(String emailForm) {
        this.emailForm = emailForm;
    }
}

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

EmailService

package com.example.email.com.example.email.demo;

/**
 * @Param
 * @return
 * @Author youjp
 * @Description //TODO
 * @throw
 **/
public interface EmailService {

    //发送简单的邮件
    void sendMailSimpleMail(String sendTo,String title,String content);

    //发送html的邮件
    void sendHtmlMail(String sendTo,String title,String content);

    //发送带附件的邮件
    void sendAttachmentMail(String sendTo, String title, String content);
}

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

EmailService实现类

package com.example.email.com.example.email.demo;

import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Service;

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

/**
 * @Param
 * @return
 * @Author youjp
 * @Description //TODO
 * @throw
 **/
@Service
public class EmailServiceImpl implements EmailService{

    @Autowired
    private  EmailConfig emailConfig;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * @Param [sendTo, title, content]
     * @return void
     * @Author youjp
     * @Description //TODO 发送简单邮件
     * @throw
     **/
    @Override
    public void sendMailSimpleMail(String sendTo, String title, String content) {
        //简单邮件的发送
        SimpleMailMessage message= new SimpleMailMessage();
        message.setFrom(emailConfig.getEmailForm());
        message.setTo(sendTo);
        message.setSubject(title);
        message.setText(content);
        mailSender.send(message);
    }

    /**
     * @Param [sendTo, title, content]
     * @return void
     * @Author youjp
     * @Description //TODO  发送一个带html格式的邮件
     * @throw
     **/
    @Override
    public void sendHtmlMail(String sendTo, String title, String content) {
        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(emailConfig.getEmailForm());
            mimeMessageHelper.setTo(sendTo);
            mimeMessageHelper.setSubject(title);
            StringBuilder sb = new StringBuilder();
            sb.append("<h1>SpirngBoot测试邮件HTML</h1>")
                    .append("\"<p style='color:#F00'>"+content+"</p>")
                    .append("<p style='text-align:right'>右对齐</p>");
            mimeMessageHelper.setText(sb.toString(), true);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Param [sendTo, title, content]
     * @return void
     * @Author youjp
     * @Description //TODO 发送带附件格式的邮件
     * @throw
     **/
    @Override
    public void sendAttachmentMail(String sendTo, String title, String content) {
        MimeMessage mimeMailMessage = null;
        try {
            mimeMailMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(emailConfig.getEmailForm());
            mimeMessageHelper.setTo(sendTo);
            mimeMessageHelper.setSubject(title);
            mimeMessageHelper.setText(content);
            //文件路径
            FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/a2.jpeg"));
            mimeMessageHelper.addAttachment("mail.png", file);

           mailSender.send(mimeMailMessage);
        } catch (Exception e) {
         e.printStackTrace();
        }
    }


}

  • 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
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103

EmailController 控制层

package com.example.email.com.example.email.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class EmailController {

    @Autowired
    private EmailService emailService;

    @RequestMapping("/simple")
    @ResponseBody
    public String sendSimpleEmail(String sendTo) {
        emailService.sendMailSimpleMail(sendTo, "发送简单邮件", "你好");

        return "success";
    }

    @RequestMapping("/sendHtmlMail")
    @ResponseBody
    public String sendHtmlMail(String sendTo) {
        emailService.sendHtmlMail(sendTo,"发送一个带html格式的邮件", "这是一封带html格式的邮件");
        return "success";
    }

    @RequestMapping("/sendAttachMail")
    @ResponseBody
    public String sendAttachMail(String sendTo) {
        emailService.sendAttachmentMail(sendTo,"发送带附件格式的邮件", "这是一封发送带附件格式的邮件");
        return "success";
    }
}
  • 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

请求接口:

请求发送带附件格式的邮件
在这里插入图片描述
在这里插入图片描述
其他请求方式一样。

有兴趣的老爷,可以关注我的公众号【一起收破烂】,回复【006】获取2021最新java面试资料以及简历模型120套哦~
在这里插入图片描述

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

闽ICP备14008679号