springboot---发送邮件

Posted 橘子味儿的猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot---发送邮件相关的知识,希望对你有一定的参考价值。

1、pom.xml配置

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

        <!-- SpringBoot 发送邮件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
 </dependencies>

 

2、在application.properties中添加邮箱配置

spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
#发送者的邮箱密码
spring.mail.password=xxx
#端口
spring.mail.port=25
#协议
spring.mail.protocol=smtp
#发送者的邮箱账号
spring.mail.username=xxx@qq.com
server.port=8081

邮箱密码:

QQ邮箱 --> 邮箱设置 --> 账号

 

3、项目整体结构

 

4、业务层service接口

package org.zyu.springboot_email.service;

public interface EmailService {

    /**
     * 发送简单邮件
     * @param sendTo    接收人
     * @param title     邮件标题
     * @param content   邮件内容
     */
    void sendSimpleMail(String sendTo,String title,String content);

    /**
     * 发送带静态资源的邮件
     * @param sendTo    接收人
     * @param title     邮件标题
     * @param content   邮件内容
     * @param path      资源路径
     */
    void sendInlineMail(String sendTo,String title,String content,String path);

    /**
     * 发送带附件的邮件
     * @param sendTo    接收人
     * @param title     邮件标题
     * @param content   邮件内容
     * @param filePath  附件路径
     */
    void sendAttachmentsMail(String sendTo,String title,String content,String filePath);
}

 

 

5、serviceImpl实现service接口

package org.zyu.springboot_email.service;

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

@Service
public class EmailServiceImpl implements EmailService {


    @Autowired
    JavaMailSender mailSender;

    //发送人
    @Value("${spring.mail.username}")
    private String sendFrom;

    @Override
    public void sendSimpleMail(String sendTo, String title, String content) {
        SimpleMailMessage mainMessage = new SimpleMailMessage();
        mainMessage.setFrom(sendFrom);
        //接收者
        mainMessage.setTo(sendTo);
        //发送的标题
        mainMessage.setSubject(title);
        //发送的内容
        mainMessage.setText(content);
        mailSender.send(mainMessage);
    }

    @Override
    public void sendInlineMail(String sendTo, String title, String content,String path) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        try {
            FileSystemResource file = new FileSystemResource(new File(path));
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(sendFrom);
            helper.setTo(sendTo);
            helper.setSubject(title);
            helper.setText(content, true);
            helper.addInline("weixin", file);
        } catch (Exception e) {
            System.out.println("发送带静态资源的邮件失败");
        }

        mailSender.send(mimeMessage);
    }

    @Override
    public void sendAttachmentsMail(String sendTo, String title, String content, String filePath) {
        MimeMessage message=mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper=new MimeMessageHelper(message,true);
            helper.setFrom(sendFrom);
            helper.setTo(sendTo);
            helper.setSubject(title);
            helper.setText(content);
            FileSystemResource file=new FileSystemResource(new File(filePath));
            String fileName=filePath.substring(filePath.lastIndexOf(File.separator));
            //添加多个附件可以使用多条
            //helper.addAttachment(fileName,file);
            helper.addAttachment(fileName,file);
            mailSender.send(message);
            System.out.println("带附件的邮件发送成功");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("发送带附件的邮件失败");
        }
    }
}

 

 5、业务层Controller

package org.zyu.springboot_email.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zyu.springboot_email.service.EmailService;

import java.nio.file.Path;

@RestController
public class MailController {

    @Autowired
    private EmailService emailService;

    /**
     * 简单发送邮件
     * @return
     */
    @GetMapping("/send")
    public String send(){
        emailService.sendSimpleMail( "393488908@qq.com", "测试标题", "hello world");
        return "发送成功";
    }

    /**
     * 发送静态资源邮件
     * @return
     */
    @GetMapping("/sendInlineMail")
    public String sendInlineMail(){
        emailService.sendInlineMail("393488908@qq.com", "主题:嵌入静态资源", "<html><body><img src=\\"cid:weixin\\" ></body></html>","C:\\\\Users\\\\Administrator\\\\Desktop\\\\timg.jpg");
        return "发送成功";
    }

    /**
     * 发送带附件的邮件
     * @return
     */
    @GetMapping("/sendAttachmentsMail")
    public String sendAttachmentsMail(){
        emailService.sendAttachmentsMail("393488908@qq.com", "主题:携带附件邮件", "hello world", "D:\\\\谷歌下载\\\\注册补贴明细列表.xlsx");
        return "发送成功";
    }


}

 

以上是关于springboot---发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

25springboot发送邮件

25springboot发送邮件

补习系列(12)-springboot 与邮件发送

使用SpringBoot发送mail邮件

java springboot+maven发送邮件

SpringBoot定时发送邮件