[SpringBoot] - 发送带附件的邮件
<!--发送email依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
service
package com.ykmimi.job.service; import org.springframework.stereotype.Service; import java.util.Map; @Service
public interface MailService { /**
* TODO 发送带附件的邮件 , 需要进行重载方法
*/
Map<String, Object> sendAttachmentsMail(String to, String subject, String content, String filePath);
}
service实现
package com.ykmimi.job.service.impl; import com.ykmimi.job.service.MailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils; import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map; @Service
public class MailServiceImpl implements MailService { @Resource
private JavaMailSender mailSender;
//发送者
@Value("${mail.fromMail.addr}")
private String from; //TODO 设置发送邮件重载方式 @Override
public Map<String, Object> sendAttachmentsMail(String to, String subject, String content, String filePath) { System.out.println("in sendAttachmentsMail");
System.out.println(filePath);
MimeMessage message = mailSender.createMimeMessage();
Map<String, Object> map = new HashMap<>(); 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));
// 发送附件
File file = new File(filePath);
file = ResourceUtils.getFile(file.getAbsolutePath());
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
// String fileName = filePath.substring(filePath.lastIndexOf("/"));
// String fileName = "附件";
System.out.println(fileName);
//添加多个附件可以使用多条 helper.addAttachment(fileName,file);
//helper.addAttachment(fileName,file);
helper.addAttachment(fileName, file);
mailSender.send(message);
map.put("code", 1);
map.put("message", "发送成功");
return map;
} catch (MessagingException e) {
e.printStackTrace();
map.put("code",0);
map.put("message","发送失败");
return map;
} catch (FileNotFoundException e) {
e.printStackTrace();
map.put("code",-1);
map.put("message","没有文件");
return map;
} finally {
} } }
或将邮件内容及邮件地址等封装为EmailContent的bean实体
通过Controll而接受.
下面是我的邮件主机配置,大家可以拿去用
##上传文件限制大小
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
##发送email设置
spring.application.name=spring-boot-mail
spring.mail.host=smtp.126.com
spring.mail.username=hostinbj@126.com
spring.mail.password=1314520jy
spring.mail.default-encoding=UTF-8
##邮件主机地址
mail.fromMail.addr=hostinbj@126.com spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
#邮件端口
spring.mail.properties.mail.smtp.socketFactory.port=465 spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
[SpringBoot] - 发送带附件的邮件的更多相关文章
- java发送带附件的邮件
/** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...
- C#发送带附件的邮件的代码
如下的代码是关于C#发送带附件的邮件的代码. MailMessage m = new MailMessage();m.Subject = "File attachment!";m. ...
- 利用Python+163邮箱授权码发送带附件的邮件
背景 前段时间写了个自动爬虫的脚本,定时在阿里云服务器上执行,会从某个网站上爬取链接保存到txt文本中,但是脚本不够完善,我需要爬虫完毕之后通过邮件把附件给我发送过来,之前写过一个<利用Pyth ...
- 使用JavaMail发送带附件的邮件
所需jar包 链接:http://pan.baidu.com/s/1dFo4cDz 密码:akap 工具类: package com.javamail.utils; import java.util. ...
- 接口测试基础——第2篇smtplib发送带附件的邮件
我先给大家补充一个用QQ发送纯文本电子邮件的代码,用QQ的朋友可以参考一下: # coding=utf-8 import smtplib from email.mime.text import MIM ...
- spring boot:发送带附件的邮件和html内容的邮件(以163.com邮箱为例/spring boot 2.3.2)
一,网站哪些情况下需要发送电子邮件? 作为一个电商网站,以下情况需要发邮件通知用户: 注册成功的信息 用邮箱接收验证码 找回密码时发链接 发送推广邮件 下单成功后的订单通知 给商户的对账单邮件 说明: ...
- python 发送带附件的邮件
特别注意的地方:filespart.add_header("Content-Disposition","attachment",filename=file_na ...
- Python发送带附件的邮件
看别人的博客就不要在往别人的邮箱里发东西了行不行, 有点素质可以吗!!! 写出来分享还不知道珍惜!!!!! #-*-encoding:utf-8 -*- import os import smtpli ...
- 【Mail】JavaMail发送带附件的邮件(二)
上一篇讲了使用JavaMail发送普通邮件([Mail]JavaMail介绍及发送邮件(一)),本例讲发送复杂的邮件(带有附件的邮件) 生成一封复杂的邮件 新建一个JavaWeb的Maven工程,引入 ...
随机推荐
- 【Pyton】【小甲鱼】类和对象
一.类 定义一个类,例子如下: class Turtle: #定义一个名为Turtle的类,Python中类型约定以大写字母开头 #属性 color='green' weight=10 legs=4 ...
- 纯css打造凹进与突出效果
1.凹进效果 background:#f2f2f2 center repeat; border-bottom: 1px solid #e9e9e9;border-top: 1px solid # ...
- java项目连接jdbc报错:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server
java项目连接jdbc报错:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not creat ...
- Win10+vs2012+cuda8.0的安装与配置
安装环境说明:NVDIA GeForce 930M.Intel(R) HD Graphics 520 显卡和cuda需要兼容匹配,我一开始下载的cuda6.5无法安装,所以又重新下了比较新的cuda8 ...
- [vue]vue双向绑定$on $emit sync-模态框
双向绑定实现($on $emit) 关于父子之间数据更新同步, 如果单向绑定, 子修改了,父却没有修改, 这种一般不符合规范 正常更新数据的套路是: 1. 子通知父更新数据 2. 子自动刷新获取最新数 ...
- pandas中的axis=0,axis=1,傻傻分不清楚
简单的来记就是axis=0代表往跨行(down),而axis=1代表跨列(across) 轴用来为超过一维的数组定义的属性,二维数据拥有两个轴: 第0轴沿着行的垂直往下,第1轴沿着列的方向水平延伸. ...
- AdaBoost Classifier和Regressor
Adaboost原理传送门 AdaBoost在我看理论课程的时候,以分类为例子来讲解的,谁知道sklearn里面基本上都有classifier和regressor两种.这个倒是我没想到的!!! fro ...
- zookeeper简单实战
一.安装(单机模式.集群模式.伪集群模式) 1:安装JDK 2:解压zk压缩包 3:在conf目录下创建zoo.cfg配置文件. 设置超时时间,快照目录,事务日志文件目录,对外端口,服务IP 4:启动 ...
- 全文搜索引擎ElasticSearch学习记录:mac下安装
最近开发组培训了ElasticSearch,准备开展新项目,我也去凑了下热闹,下面把学习过程记录一下. 一.安装 1.环境需要jdk1.8; 2.下载:http://www.elastic.co/do ...
- 如何制作Windows镜像
1.在https://msdn.itellyou.cn/网站中下载(使用迅雷)Windows2003R2 中文版ISO 2.使用qemu-img create命令创建一个空的 后缀为.img的文件 q ...