spring boot 发送邮箱

添加pom依赖:

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

配置邮箱properties文件:

#mail
#发件箱主机地址
spring.mail.host=smtp.163.com
#用户名
spring.mail.username=17600000142@163.com
#密码或者授权码
spring.mail.password=XXXXXXX

写邮件配置类:

发送纯文本邮件:

@Component
public class MailUtil {
    //发送邮箱的类
    @Autowired
    private JavaMailSender javaMailSender;
    //@Value获取配置文件中的值
    @Value("${spring.mail.username}")
    private String from;
    //发送普通邮件
    public  void sender1(String to,String subject,String context){
        MimeMessage message=javaMailSender.createMimeMessage();
        MimeMessageHelper helper=null;
 
        try {
            helper=new MimeMessageHelper(message);
            //收件人邮箱
            helper.setTo(to);
            //发件人邮箱
            helper.setFrom(from);
            //发送主题
            helper.setSubject(subject);
            //发送内容,true可有可无,用于解析为html代码
            helper.setText(context,true);
            //把邮件发出去
            javaMailSender.send(message);
            
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

带附件邮件:

@Component
public class MailUtil {
    //发送邮箱的类
    @Autowired
    private JavaMailSender javaMailSender;
    //@Value获取配置文件中的值
    @Value("${spring.mail.username}")
    private String from;
    
    //发送带附件邮件
    public void sender2(String to,String subject,String context,String path) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage,true);
            FileSystemResource fileSystemResource = new FileSystemResource(new File(path));
            //.separator获取系统的分隔符/或者\
            //获取附件原文件名
            String fileName = path.substring(path.lastIndexOf(File.separator));
            //挂附件
            helper.addAttachment(fileName, fileSystemResource);
            //收件人邮箱
            helper.setTo(to);
            //发件人邮箱
            helper.setFrom(from);
            //邮件主题
            helper.setSubject(subject);
           //发送内容,true可有可无,用于解析为html代码
            helper.setText(context,true);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (java.lang.Exception exception) {
            exception.printStackTrace();
        }
    }
}

发送带网页模板的邮件:

需要在templates中新建一个html页面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 
你好: <span th:text="${username}"></span>
<span th:text="${text}"></span>
 
</body>
</html>

后台编写:

 @Autowired
    private TemplateEngine templateEngine;
 
    //发送模板邮件
    public void sender3(String to, String subject, String templateName, Map<String, Object> dataMap) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage);
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(subject);
            //解析页面模板->文本内容
            Context context = new Context();
            context.setVariables(dataMap);
            //process方法是用来解析模板生成文本的 templateName模板名字 context data数据
            String text = templateEngine.process(templateName,context);
            helper.setText(text,true);
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

控制层发送:

发送普通邮件:

 @RequestMapping("mail")
 @ResponseBody
 public  void  sender1(){
    String to="495381306@qq.com";
    String subject="刘银龙";
    String context="Hello SpringBoot";
    mailUtil.sender1(to,subject,context);
    System.out.println("发送邮件完成");
 
}

发送带附件的邮件:

    @RequestMapping("mail2")
    @ResponseBody
    public void sender2(){
        String path = "C:\\Users\\黯然\\Documents\\Tencent Files\\9123949\\FileRecv\\07 Spring Boot(Redis 邮件激活流程).doc";
        String to="495381306@qq.com";
        String subject="刘银龙";
        String context="Hello Word";
        mailUtil.sender2(to,subject,context,path);
        System.out.println("发送邮件完成");
 
    }

发送网页模板的图片:

@RequestMapping("mail3")
    @ResponseBody
    public String sender3(){
        String to="9123949@qq.com";
        String subject="刘银龙";
        String templateName = "hello";
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("username", "刘银龙");
        dataMap.put("text","啊啊啊啊");
        String context="这是一份测试的邮件,有附件";
        mailUtil.sender3(to,subject,templateName,dataMap);
        return "发送邮件完成";
    }
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇