博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot 发送,简单,html格式,带本地附件,带远程附件邮件详解
阅读量:1824 次
发布时间:2019-04-25

本文共 4230 字,大约阅读时间需要 14 分钟。

1、导入相应的jar包

org.springframework.boot
spring-boot-starter-mail

2、配置参数

mail:    host: smtp.qq.com #发送邮件服务器    username: xxx@qq.com #发送邮件的邮箱地址    password:  xxxx #客户端授权码,不是邮箱密码,这个在qq邮箱设置里面自动生成的    properties.mail.smtp.port: 465 #端口号465或587    from: xxx@qq.com # 发送邮件的地址,和上面username一致可以任意    properties.mail.smtp.starttls.enable: true    properties.mail.smtp.starttls.required: true    properties.mail.smtp.ssl.enable: true    default-encoding: utf-8

3、检查相应的邮件服务器是否开启服务

4、编写相应的工具

package top.cfl.cflwork.util;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.Component;import javax.activation.FileDataSource;import javax.activation.URLDataSource;import javax.mail.internet.MimeMessage;import java.io.File;import java.net.URL;/** * @Author: fly 275300091 * @Description: 邮件工具 * @Date: Create in 2019/08/24 */@Componentpublic class MailUtil {    /**     * Spring Boot 提供了一个发送邮件的简单抽象,使用的是下面这个接口,这里直接注入即可使用     */    @Autowired    private JavaMailSender mailSender;    /**     * 配置文件中我的qq邮箱     */    @Value("${spring.mail.from}")    private String from;    /**     * 简单文本邮件     * @param to 收件人     * @param subject 主题     * @param content 内容     */    public void sendSimpleMail(String to, String subject, String content) {        try {            //创建SimpleMailMessage对象            SimpleMailMessage message = new SimpleMailMessage();            //邮件发送人            message.setFrom(from);            //邮件接收人            message.setTo(to);            //邮件主题            message.setSubject(subject);            //邮件内容            message.setText(content);            //发送邮件            mailSender.send(message);        }catch (Exception e){            e.printStackTrace();        }    }    /**     * html邮件     * @param to 收件人     * @param subject 主题     * @param content 内容     */    public void sendHtmlMail(String to, String subject, String content) {        //获取MimeMessage对象        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper messageHelper;        try {            messageHelper = new MimeMessageHelper(message, true);            //邮件发送人            messageHelper.setFrom(from);            //邮件接收人            messageHelper.setTo(to);            //邮件主题            message.setSubject(subject);            //邮件内容,html格式            messageHelper.setText(content, true);            //发送            mailSender.send(message);            //日志信息            System.out.println("邮件已经发送。");        } catch (Exception e) {            e.printStackTrace();            System.err.println("发送邮件时发生异常!");        }    }    /**     * 带附件的邮件     * @param to 收件人     * @param subject 主题     * @param content 内容     * @param filePath 附件     */    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {        MimeMessage message = mailSender.createMimeMessage();        try {            MimeMessageHelper helper = new MimeMessageHelper(message, true);            helper.setFrom(from);            helper.setTo(to);            helper.setSubject(subject);            helper.setText(content, true);            //本地或项目资源            //FileSystemResource file = new FileSystemResource(new File(filePath));            //FileDataSource fds = new FileDataSource(filePath);            //远程资源            URLDataSource uds=new URLDataSource(new URL(filePath));            helper.addAttachment(uds.getName(), uds);            mailSender.send(message);            //日志信息            System.out.println("邮件已经发送成功,带有附件。");        } catch (Exception e) {            System.err.println("发送邮件时发生异常!");        }    }}

5、测试

public static void main(String[] args) {        MailUtil mailUtil = new MailUtil();//        mailUtil.sendSimpleMail("xxx@163.com","主题:简单邮件测试","内容:简单邮件测试");        mailUtil.sendAttachmentsMail("xxx@163.com","主题:我是带有附件的,带有html格式","

内容:我是带有附件的

","http://www.xxx.com/upload/avatar//2019/0623/6b50262a7be6442d.jpg"); }

 

转载地址:http://xyskf.baihongyu.com/

你可能感兴趣的文章
LeetCode 342. 4的幂
查看>>
El表达式
查看>>
springboot banner打印,控制台springboot图案怎么来的
查看>>
linux shell內建命令区分--type
查看>>
java--打印当前项目加载的jar包--getResources
查看>>
mybatis 学习记录(3)—— 动态 sql
查看>>
mybatis 学习记录(4.1)—— 级联查询(无 association 和 collection)
查看>>
面试官:说说快速失败和安全失败是什么
查看>>
Java的final和static区别
查看>>
建立索引的好处
查看>>
java如何对ArrayList中对象按照该对象某属性排序
查看>>
今天碰到IE的一个问题, 两个IFRAME的问题
查看>>
js实现列表滚动
查看>>
WindowXP下PHP5开发环境配置 (转载)
查看>>
用java调用webservice接口
查看>>
jquery 横向柱形图
查看>>
log4j.xml输出日志调试过程
查看>>
<param name="wmode" value="transparent">
查看>>
myeclipse集成ant
查看>>
MySQL中的配置参数interactive_timeout和wait_timeout(可能导致过多sleep进程的两个参数)
查看>>