<!--发送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] - 发送带附件的邮件的更多相关文章

  1. java发送带附件的邮件

    /** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...

  2. C#发送带附件的邮件的代码

    如下的代码是关于C#发送带附件的邮件的代码. MailMessage m = new MailMessage();m.Subject = "File attachment!";m. ...

  3. 利用Python+163邮箱授权码发送带附件的邮件

    背景 前段时间写了个自动爬虫的脚本,定时在阿里云服务器上执行,会从某个网站上爬取链接保存到txt文本中,但是脚本不够完善,我需要爬虫完毕之后通过邮件把附件给我发送过来,之前写过一个<利用Pyth ...

  4. 使用JavaMail发送带附件的邮件

    所需jar包 链接:http://pan.baidu.com/s/1dFo4cDz 密码:akap 工具类: package com.javamail.utils; import java.util. ...

  5. 接口测试基础——第2篇smtplib发送带附件的邮件

    我先给大家补充一个用QQ发送纯文本电子邮件的代码,用QQ的朋友可以参考一下: # coding=utf-8 import smtplib from email.mime.text import MIM ...

  6. spring boot:发送带附件的邮件和html内容的邮件(以163.com邮箱为例/spring boot 2.3.2)

    一,网站哪些情况下需要发送电子邮件? 作为一个电商网站,以下情况需要发邮件通知用户: 注册成功的信息 用邮箱接收验证码 找回密码时发链接 发送推广邮件 下单成功后的订单通知 给商户的对账单邮件 说明: ...

  7. python 发送带附件的邮件

    特别注意的地方:filespart.add_header("Content-Disposition","attachment",filename=file_na ...

  8. Python发送带附件的邮件

    看别人的博客就不要在往别人的邮箱里发东西了行不行, 有点素质可以吗!!! 写出来分享还不知道珍惜!!!!! #-*-encoding:utf-8 -*- import os import smtpli ...

  9. 【Mail】JavaMail发送带附件的邮件(二)

    上一篇讲了使用JavaMail发送普通邮件([Mail]JavaMail介绍及发送邮件(一)),本例讲发送复杂的邮件(带有附件的邮件) 生成一封复杂的邮件 新建一个JavaWeb的Maven工程,引入 ...

随机推荐

  1. SpringBoot @Transactional声明事务无效问题

    查看系统支持的存储引擎:show engines; 查看表使用的引擎:show table status from db_name where name='table_name'; 修改表引擎方法:  ...

  2. java客户端调用ftp上传下载文件

    1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...

  3. R实现的最小二乘lsfit函数学习

    1.源码 function (x, y, wt = NULL, intercept = TRUE, tolerance = 1e-, yname = NULL) { x <- as.matrix ...

  4. VB.net 与线程

    Imports System.Threading Imports System Public Class Form1 Dim th1, th2 As Thread Public Sub Method1 ...

  5. DNS解析原理和流程

    DNS解析原理和流程   DNS解析其实就是将IP地址(202.96.134.133)变成域名(www.xxxxx.com)   网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址的,所 ...

  6. unity3d-绘制贴图

    准备贴图 在屏幕在绘制一张静态贴图,需要用到GUI.DrawTexture()方法, 该方法可以设定图片的显示位置.缩放比例和渲染混合等 /* Rect position:表示图片的绘制区域 * Te ...

  7. 利用lodop打印控件轻松实现批量打印 (转载http://www.thinkphp.cn/topic/13085.html)

    最近在做一个打印程序,要实现批量打印功能,在网上找了很多天,也在tp官网咨询大牛们,对大家的的提议我一一进行了研究,总结如下: 要实现批量打印可以有两个办法: 一是利用专业的报表程序,能实现十分复杂的 ...

  8. mysql的转储SQL文件

    1.转储数据库的SQL文件,有两个选择,一是转储结构:另一种是转储数据与结构: 2.以上两种转储都不会将事件(定时器)转储,所以特别注意这个,否则以为将数据库备份完了,其实漏掉了所有的事件代码,所以需 ...

  9. 014-配置SSH免密钥登录

    问题:client端需要免密钥登录服务器server如何配置?1.前提:客户端已安装openssh-client;服务端已安装openssh-server;服务器端22号端口已经打开2.需要密钥登录时 ...

  10. ModelSim使用$display查看变量值和输出信息

    打开ModelSim,新建工程->新建Verilog文件demo.v 输入文件内容 module demo(); reg[3:0] a,b; initial begin $display(&qu ...