博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[SpringBoot] - 发送带附件的邮件
阅读量:7058 次
发布时间:2019-06-28

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

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

service

package com.ykmimi.job.service;import org.springframework.stereotype.Service;import java.util.Map;@Servicepublic interface MailService {    /**     * TODO 发送带附件的邮件 , 需要进行重载方法     */    Map
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;@Servicepublic class MailServiceImpl implements MailService {    @Resource    private JavaMailSender mailSender;    //发送者    @Value("${mail.fromMail.addr}")    private String from;    //TODO 设置发送邮件重载方式    @Override    public Map
sendAttachmentsMail(String to, String subject, String content, String filePath) { System.out.println("in sendAttachmentsMail"); System.out.println(filePath); MimeMessage message = mailSender.createMimeMessage(); Map
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=10MBspring.servlet.multipart.max-request-size=10MB##发送email设置spring.application.name=spring-boot-mailspring.mail.host=smtp.126.comspring.mail.username=hostinbj@126.comspring.mail.password=1314520jyspring.mail.default-encoding=UTF-8##邮件主机地址mail.fromMail.addr=hostinbj@126.comspring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory#邮件端口spring.mail.properties.mail.smtp.socketFactory.port=465spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=true

 

转载于:https://www.cnblogs.com/ukzq/p/10201590.html

你可能感兴趣的文章
c++编程命名规范
查看>>
时间戳格式化
查看>>
背景建模技术(六):帧处理(FrameProcessor)模块
查看>>
抖音无水印视频解析php源码
查看>>
AngularJs自定义指令详解(5) - link
查看>>
docker的安装,升级,与删除(最新版)
查看>>
让ul li 或者table 进行循环往上滚屏
查看>>
给那次面试一个响亮的回忆
查看>>
org.springframework:spring-cloud-starter-netflix-eureka-client:unknown 的解决办法
查看>>
C#集合类使用范例
查看>>
js操作DOM在父元素中的结尾添加子节点注意
查看>>
【转】Hadoop是什么
查看>>
【转载】Java程序设计入门 (一)
查看>>
【转】Verilog阻塞与非阻塞赋值使用要点
查看>>
数据结构及算法基础--归并排序(Merge Sort)
查看>>
Fixed: My Generation Drivers Dropdown List Empty
查看>>
redhat 6.4 yum 本地配置简记
查看>>
一些android开发实用性网站记录
查看>>
常用网页代码
查看>>
卡漫绘图
查看>>