基于SpringBoot实现发送带附件的邮件
这篇文章主要介绍了基于SpringBoot实现发送带附件的邮件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
org.springframework.boot spring-boot-starter-mail
service
packagecom.ykmimi.job.service;
importorg.springframework.stereotype.Service;
importjava.util.Map;
@Service
publicinterfaceMailService{
/**
*TODO发送带附件的邮件,需要进行重载方法
*/
MapsendAttachmentsMail(Stringto,Stringsubject,Stringcontent,StringfilePath);
}
service实现
packagecom.ykmimi.job.service.impl;
importcom.ykmimi.job.service.MailService;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.mail.javamail.JavaMailSender;
importorg.springframework.mail.javamail.MimeMessageHelper;
importorg.springframework.stereotype.Service;
importorg.springframework.util.ResourceUtils;
importjavax.annotation.Resource;
importjavax.mail.MessagingException;
importjavax.mail.internet.MimeMessage;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.HashMap;
importjava.util.Map;
@Service
publicclassMailServiceImplimplementsMailService{
@Resource
privateJavaMailSendermailSender;
//发送者
@Value("${mail.fromMail.addr}")
privateStringfrom;
//TODO设置发送邮件重载方式
@Override
publicMapsendAttachmentsMail(Stringto,Stringsubject,Stringcontent,StringfilePath){
System.out.println("insendAttachmentsMail");
System.out.println(filePath);
MimeMessagemessage=mailSender.createMimeMessage();
Mapmap=newHashMap<>();
try{
MimeMessageHelperhelper=newMimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
//FileSystemResourcefile=newFileSystemResource(newFile(filePath));
//发送附件
Filefile=newFile(filePath);
file=ResourceUtils.getFile(file.getAbsolutePath());
StringfileName=filePath.substring(filePath.lastIndexOf(File.separator));
//StringfileName=filePath.substring(filePath.lastIndexOf("/"));
//StringfileName="附件";
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","发送成功");
returnmap;
}catch(MessagingExceptione){
e.printStackTrace();
map.put("code",0);
map.put("message","发送失败");
returnmap;
}catch(FileNotFoundExceptione){
e.printStackTrace();
map.put("code",-1);
map.put("message","没有文件");
returnmap;
}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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。