SpringBoot-19-之发送邮件

零、准备工作
<!--发送邮件的依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring:
    mail:
      host: smtp.126.com
      username: toly1994@126.com
      password: 你的密码--注意不是登陆密码,见下图:
      default-encoding: utf-8
点设置,会有让设置pop3密码的地方
9414344-0c8b97f7ad4edb13.png
关于密码问题.png

一、发送简单邮件
toly1994.com.toly_email.service.MailService
@Service
public class MailService {
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mJavaMailSender;

    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo(to);
        msg.setSubject(subject);
        msg.setText(content);
        msg.setFrom(from);

        mJavaMailSender.send(msg);
    }
}
toly1994.com.toly_email.controller.MailController
@RestController
public class MailController {

    @Autowired
    private MailService mMailService;

    @GetMapping("/send")
    public String send() {
        mMailService.sendSimpleMail("1981462002@qq.com","你好","张风捷特烈");
        return "send";
    }

}
9414344-106cdaf09e353451.png
发送邮箱.png

二、发送Html邮件
toly1994.com.toly_email.service.MailService
    public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
        MimeMessage msg = mJavaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        helper.setFrom(from);

        mJavaMailSender.send(msg);

    }
toly1994.com.toly_email.controller.MailController
    @GetMapping("/sendHtml")
    public String sendHtml() {
        String html =
                "<html\n" +
                "<body>\n" +
                "<h3> Hello Toly</h3>\n" +
                "</body>\n" +
                "</html>";

        try {
            mMailService.sendHtmlMail("1981462002@qq.com", "你好", html);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "send";
    }
9414344-4d8a946ad7a12958.png
发送html邮件.png

三、发送附件

推荐压缩一下成zip再发送。

toly1994.com.toly_email.service.MailService
/**
 *
 * @param to 收信人
 * @param subject 主题
 * @param content 内容
 * @param paths 路径
 * @throws MessagingException
 */
public void sendSubFileMail(String to, String subject, String content, String... paths) throws MessagingException {
    MimeMessage msg = mJavaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(msg, true);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content, true);
    helper.setFrom(from);
    for (String path : paths) {
        FileSystemResource fsr = new FileSystemResource(new File(path));
        String filename = fsr.getFilename();
        System.out.println(filename);
        helper.addAttachment(filename, fsr);
    }
    mJavaMailSender.send(msg);
}
toly1994.com.toly_email.controller.MailController
@GetMapping("/sendSubFile")
public String sendSubFile() {
    String path = "F:\\SpringBootFiles\\file\\springboot.zip";
    String path2 = "F:\\SpringBootFiles\\file\\MySQL.zip";
    try {
        mMailService.sendSubFileMail("1981462002@qq.com","你好","张风捷特烈",path,path2);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    return "send";
}
9414344-d2d7c735f3e10bd6.png
发送附件.png

四、发送图片
toly1994.com.toly_email.service.MailService
    /**
     * 发送带图片的邮件
     * @param to
     * @param subject
     * @param content
     * @param resPath
     * @param resId
     * @throws MessagingException
     */
    public void sendImgMail(String to, String subject, String content,
                            String resPath,String resId) throws MessagingException {
        MimeMessage msg = mJavaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        helper.setFrom(from);

        FileSystemResource fsr = new FileSystemResource(new File(resPath));
        helper.addInline(resId, fsr);

        mJavaMailSender.send(msg);
    }
toly1994.com.toly_email.controller.MailController
    @GetMapping("/sendImgMail")
    public String sendImgMail() {
        String imgPath = "E:\\Photo\\picpic\\Android\\timg.jpg";
        String resId = "001";

        String content =
                "<html><body> 图片邮件:<img width='300' src=\'cid:"+resId+"\'></img></body></html>";
        try {
            mMailService.sendImgMail("1981462002@qq.com","发一张图片给你",content,imgPath,resId);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "send";
    }
9414344-754340421603af09.png
发送图片.png

五、发送模板邮件:使用thymeleaf,不熟悉的小伙伴可移驾到这篇:模板引擎--thymeleaf
toly1994.com.toly_email.controller.MailController
    @Autowired
    private TemplateEngine mTemplateEngine;

    @GetMapping("/sendTempMail")
    public String sendTempMail() {
        Context context = new Context();
        context.setVariable("id", "99531e7006e0");
        String emailTemp = mTemplateEngine.process("emailTemp", context);
        try {
            mMailService.sendHtmlMail("1981462002@qq.com","模板邮件",emailTemp);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "OK";
    }

模板:templates/emailTemp.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>邮件模板</title>
</head>
<body>
您好,感谢您的注册!点击下面的链接完成注册,感谢您的支持 <br>

<a href="#" th:href="@{https://www.jianshu.com/p/{id}(id=${id})}">
https://www.jianshu.com/p/99531e7006e0</a>
</body>
</html>
9414344-0744ab42b848f9ae.png
模板邮件.png

后记、
1.声明:

[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力

2.连接传送门:

更多Spring文章,欢迎访问
我的github地址:欢迎star
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

3.联系我

QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328

4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
9414344-c474349cd3bd4b82.jpg
公众号.jpg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值