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>

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

  • 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

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

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

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


}

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";
    }
}

请求接口:

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

收破烂的小熊猫~

你的鼓励将是我创造最大的东西~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值