springboot 发送,简单,html格式,带本地附件,带远程附件邮件详解
发布日期:2021-06-28 18:30:42
浏览次数:3
分类:技术文章
本文共 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"); }
转载地址:https://blog.csdn.net/xljx_1/article/details/100057073 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月06日 14时09分43秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
集合篇-----ArrayList与LinkedList之间的那些小事
2019-04-29
Linux系统的优点小结
2019-04-29
15个Google面试题,看看自己能答对几个,看你是否真的聪明。。。
2019-04-29
Clone使用方法详解
2019-04-29
Java clone() 浅克隆与深度克隆
2019-04-29
Java中对象与引用
2019-04-29
JDK 1.7 Integer.parseInt 源码解析
2019-04-29
Java单例模式
2019-04-29
三种方法拆分字符串
2019-04-29
贪心算法基本思想和典型例题
2019-04-29
坑爹的小学数学题
2019-04-29
快速找出一个数组中的两个数字,让这两个数字之和等于一个给定的值
2019-04-29
[经典排序算法][集锦]
2019-04-29
无处不在的二分查找
2019-04-29
Java集合框架List,Map,Set等全面介绍
2019-04-29
Java 泛型(二) 泛型之中的通配符(Wildcards)使用
2019-04-29
7-36 复数四则运算 (15 分)
2019-04-29
L1-002 打印沙漏 (20 分)
2019-04-29
L1-003 个位数统计 (15 分)
2019-04-29
L1-005 考试座位号 (15 分)
2019-04-29