diff --git a/boot-service/src/main/java/com/xwbing/demo/FileDemo.java b/boot-service/src/main/java/com/xwbing/demo/FileDemo.java
index 4676897..70ce940 100644
--- a/boot-service/src/main/java/com/xwbing/demo/FileDemo.java
+++ b/boot-service/src/main/java/com/xwbing/demo/FileDemo.java
@@ -63,7 +63,7 @@ public class FileDemo {
* java resources 编译后在WEB-INF/classes下 同级
*/
String tomcatHome = System.getProperty("catalina.home");//服务器tomcat路径
- ClassPathResource pic = new ClassPathResource("file");//获取classes下file文件夹路径
+ ClassPathResource pic = new ClassPathResource("pic");//获取classes下file文件夹路径
String absolutePath = pic.getFile().getAbsolutePath();
//配置文件
String filePath = IODemo.class.getResource("/redis.properties").getPath();
diff --git a/boot-service/src/main/java/com/xwbing/demo/FileUploadDemo.java b/boot-service/src/main/java/com/xwbing/demo/FileUploadDemo.java
index 932b5bc..5ac864d 100644
--- a/boot-service/src/main/java/com/xwbing/demo/FileUploadDemo.java
+++ b/boot-service/src/main/java/com/xwbing/demo/FileUploadDemo.java
@@ -59,7 +59,7 @@ public class FileUploadDemo {
public void fileUpload(CommonsMultipartFile multipartFile) {
if (!multipartFile.isEmpty()) {
String name = multipartFile.getOriginalFilename();//原始名字
- ClassPathResource file = new ClassPathResource("file");
+ ClassPathResource file = new ClassPathResource("pic");
try {
String path = file.getFile().getAbsolutePath();
File targetFile = new File(path+ File.separator+name);
@@ -74,7 +74,7 @@ public class FileUploadDemo {
}
public static void main(String[] args) throws Exception{
- ClassPathResource file = new ClassPathResource("file");
+ ClassPathResource file = new ClassPathResource("pic");
String path = file.getFile().getAbsolutePath();
File targetFile = new File(path+ File.separator+"test.txt");
if(!targetFile.exists()){
diff --git a/boot-service/src/main/java/com/xwbing/service/rest/MailService.java b/boot-service/src/main/java/com/xwbing/service/rest/MailService.java
index c21ec4b..b039935 100644
--- a/boot-service/src/main/java/com/xwbing/service/rest/MailService.java
+++ b/boot-service/src/main/java/com/xwbing/service/rest/MailService.java
@@ -27,6 +27,7 @@ public class MailService {
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
+ private MimeMessageHelper helper = null;
/**
* 发送纯文本的简单邮件
@@ -44,14 +45,12 @@ public class MailService {
message.setText(content);
try {
mailSender.send(message);
- } catch (MailException e) {
- restMessage.setSuccess(false);
- restMessage.setMessage("发送邮件失败");
+ restMessage.setSuccess(true);
+ restMessage.setMessage("纯文本邮件已经发送");
return restMessage;
+ } catch (MailException e) {
+ throw new BusinessException("发送纯文本邮件异常");
}
- restMessage.setSuccess(true);
- restMessage.setMessage("纯文本邮件已经发送");
- return restMessage;
}
/**
@@ -64,45 +63,36 @@ public class MailService {
public RestMessage sendHtmlMail(String to, String subject, String content) {
RestMessage restMessage = new RestMessage();
MimeMessage message = mailSender.createMimeMessage();
- try {
- //true表示需要创建一个multipart message
- MimeMessageHelper helper = new MimeMessageHelper(message, true);
- helper.setFrom(from);
- helper.setTo(to);
- helper.setSubject(subject);
- helper.setText(content, true);//启用html
- mailSender.send(message);
- restMessage.setSuccess(true);
- restMessage.setMessage("html邮件已经发送!");
- return restMessage;
- } catch (MessagingException e) {
- throw new BusinessException("发送html邮件时发生异常");
- }
+ //true表示需要创建一个multipart message
+ getHelper(message, to, subject, content);
+ mailSender.send(message);
+ restMessage.setSuccess(true);
+ restMessage.setMessage("html邮件已经发送!");
+ return restMessage;
}
/**
* 发送带附件的邮件
*
- * @param to 收件人
- * @param subject 主题
- * @param content 文本内容
- * @param rscPath 附件文件路径
+ * @param to 收件人
+ * @param subject 主题
+ * @param content 文本内容
+ * @param rscPaths 附件文件路径
*/
- public RestMessage sendAttachmentsMail(String to, String subject, String content, String rscPath) {
+ public RestMessage sendAttachmentsMail(String to, String subject, String content, String... rscPaths) {
RestMessage restMessage = new RestMessage();
MimeMessage message = mailSender.createMimeMessage();
+ MimeMessageHelper helper = getHelper(message, to, subject, content);
try {
- //true表示需要创建一个multipart message
- MimeMessageHelper helper = new MimeMessageHelper(message, true);
- helper.setFrom(from);
- helper.setTo(to);
- helper.setSubject(subject);
- helper.setText(content, true);
- //添加附件1
- FileSystemResource file = new FileSystemResource(new File(rscPath));
- String fileName = rscPath.substring(rscPath.lastIndexOf(File.separator) + 1);
- helper.addAttachment(fileName, file);
- // 可以继续添加附件。。。。。
+ //添加附件
+ if (rscPaths != null && rscPaths.length > 0) {
+ FileSystemResource file;
+ for (String rscPath : rscPaths) {
+ file = new FileSystemResource(new File(rscPath));
+ String fileName = rscPath.substring(rscPath.lastIndexOf(File.separator) + 1);
+ helper.addAttachment(fileName, file);
+ }
+ }
mailSender.send(message);//发送邮件
restMessage.setSuccess(true);
restMessage.setMessage("带附件的邮件已经发送!");
@@ -124,13 +114,8 @@ public class MailService {
public RestMessage sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
RestMessage restMessage = new RestMessage();
MimeMessage message = mailSender.createMimeMessage();
+ MimeMessageHelper helper = getHelper(message, to, subject, content);
try {
- //true表示需要创建一个multipart message
- MimeMessageHelper helper = new MimeMessageHelper(message, true);
- helper.setFrom(from);
- helper.setTo(to);
- helper.setSubject(subject);
- helper.setText(content, true);
//添加多个图片可以使用多条 和 helper.addInline(rscId, res) 来实现
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
@@ -142,4 +127,25 @@ public class MailService {
throw new BusinessException("发送嵌入静态资源的邮件时发生异常!");
}
}
+
+ /**
+ * 获取邮件助手
+ *
+ * @param message
+ * @return
+ */
+ private MimeMessageHelper getHelper(MimeMessage message, String to, String subject, String content) {
+ if (helper == null) {
+ try {
+ helper = new MimeMessageHelper(message, true);
+ helper.setFrom(from);
+ helper.setTo(to);
+ helper.setSubject(subject);
+ helper.setText(content, true);//启用html
+ } catch (MessagingException e) {
+ throw new BusinessException("获取邮件助手失败");
+ }
+ }
+ return helper;
+ }
}
diff --git a/boot-service/src/main/java/com/xwbing/service/rest/QRCodeZipService.java b/boot-service/src/main/java/com/xwbing/service/rest/QRCodeZipService.java
index 7af6b7a..5aa516c 100644
--- a/boot-service/src/main/java/com/xwbing/service/rest/QRCodeZipService.java
+++ b/boot-service/src/main/java/com/xwbing/service/rest/QRCodeZipService.java
@@ -103,7 +103,7 @@ public class QRCodeZipService {
* @return
*/
public String getPath() {
- ClassPathResource pic = new ClassPathResource("file");
+ ClassPathResource pic = new ClassPathResource("pic");
String absolutePath;
try {
absolutePath = pic.getFile().getAbsolutePath();
diff --git a/boot-service/src/main/java/com/xwbing/util/QRCodeUtil.java b/boot-service/src/main/java/com/xwbing/util/QRCodeUtil.java
index 853e664..9d61197 100644
--- a/boot-service/src/main/java/com/xwbing/util/QRCodeUtil.java
+++ b/boot-service/src/main/java/com/xwbing/util/QRCodeUtil.java
@@ -303,12 +303,12 @@ public class QRCodeUtil {
// String tomcatHome = System.getProperty("catalina.home");
// String name="QRcode";
// File output=new File(tomcatHome+File.separator+"file"+File.separator+name+".png");
- ClassPathResource file = new ClassPathResource("file");
+ ClassPathResource file = new ClassPathResource("pic");
String path = file.getFile().getAbsolutePath();
File image = new File(path+File.separator+"logo.png");
FileInputStream logo=new FileInputStream(image);
File out = new File(path+File.separator+"QRCode.png");
- createCodeLogo(out,"java生成的二維碼",logo,"");
+ createCodeLogo(out,"java生成的二維碼",logo,"logo");
}
}
diff --git a/boot-service/src/main/resources/file/QRCode.png b/boot-service/src/main/resources/pic/QRCode.png
similarity index 100%
rename from boot-service/src/main/resources/file/QRCode.png
rename to boot-service/src/main/resources/pic/QRCode.png
diff --git a/boot-service/src/main/resources/file/logo.png b/boot-service/src/main/resources/pic/logo.png
similarity index 100%
rename from boot-service/src/main/resources/file/logo.png
rename to boot-service/src/main/resources/pic/logo.png
diff --git a/boot-web/src/main/java/com/xwbing/controller/rest/MailTestControl.java b/boot-web/src/main/java/com/xwbing/controller/rest/MailTestControl.java
index 1e836cd..543fa84 100644
--- a/boot-web/src/main/java/com/xwbing/controller/rest/MailTestControl.java
+++ b/boot-web/src/main/java/com/xwbing/controller/rest/MailTestControl.java
@@ -7,11 +7,13 @@ import com.xwbing.util.JsonResult;
import com.xwbing.util.RestMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
+import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
+import java.io.IOException;
/**
* 项目名称: sb-boot-module-demo
@@ -21,7 +23,7 @@ import javax.annotation.Resource;
*/
@Api(tags = "mailApi", description = "邮件测试相关接口")
@RestController
-@RequestMapping("/mail/")
+@RequestMapping("/mailtest/")
public class MailTestControl {
@Resource
private MailService mailService;
@@ -50,9 +52,12 @@ public class MailTestControl {
@LogInfo("发送带附件邮件")
@GetMapping("sendAttachmentsMail")
@ApiOperation(value = "发送带附件邮件")
- public JSONObject sendAttachmentsMail() {
- String path = "C:\\Users\\admin\\Desktop\\qq.txt";
- RestMessage restMessage = mailService.sendAttachmentsMail("786461501@qq.com", "附件测试邮件", "收到一个带附件邮件", path);
+ public JSONObject sendAttachmentsMail() throws IOException {
+ ClassPathResource file = new ClassPathResource("file");
+ String path = file.getFile().getAbsolutePath();
+ String path1 = path + "\\QRCode.png";
+ String path2 = path + "\\QRCode.png";
+ RestMessage restMessage = mailService.sendAttachmentsMail("786461501@qq.com", "附件测试邮件", "收到一个带附件邮件", path1, path2);
return JsonResult.toJSONObj(restMessage);
}
@@ -62,7 +67,7 @@ public class MailTestControl {
public JSONObject sendInlineResourceMail() {
String rscId = "file";
String content = "