这篇文章给大家分享的是有关基于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如何实现发送带附件的邮件”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。