1、自定义发送邮件添加附件功能实现
This commit is contained in:
parent
f14d5004a5
commit
c76a8639ba
|
|
@ -1,5 +1,6 @@
|
||||||
package com.ruoyi.auth.aop;
|
package com.ruoyi.auth.aop;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
import com.ruoyi.system.api.model.LoginUser;
|
import com.ruoyi.system.api.model.LoginUser;
|
||||||
import com.xjs.business.warning.RemoteMailFeign;
|
import com.xjs.business.warning.RemoteMailFeign;
|
||||||
import com.xjs.business.warning.domain.MailBean;
|
import com.xjs.business.warning.domain.MailBean;
|
||||||
|
|
@ -49,7 +50,8 @@ public class LoginAspect {
|
||||||
mailBean.setRecipient(loginUser.getSysUser().getEmail());
|
mailBean.setRecipient(loginUser.getSysUser().getEmail());
|
||||||
mailBean.setContent("<h3 style=\"color:red;\">" + loginUser.getSysUser().getNickName() + "上线啦</h3> " +
|
mailBean.setContent("<h3 style=\"color:red;\">" + loginUser.getSysUser().getNickName() + "上线啦</h3> " +
|
||||||
"<img src=\"" + loginUser.getSysUser().getAvatar() + "\" alt=\"头像\">" +
|
"<img src=\"" + loginUser.getSysUser().getAvatar() + "\" alt=\"头像\">" +
|
||||||
" <p>当前IP地址:" + loginUser.getSysUser().getLoginIp() + "</p>");
|
" <p><strong>当前IP地址:" + loginUser.getSysUser().getLoginIp() + "</strong></p>" +
|
||||||
|
"<p><strong>上线时间:"+ DateUtil.now() +"</strong></p>");
|
||||||
|
|
||||||
remoteMailFeign.sendMailForRPC(mailBean);
|
remoteMailFeign.sendMailForRPC(mailBean);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,22 @@
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
|
|
||||||
|
<el-col :span="24">
|
||||||
|
<el-form-item label="附件" prop="file">
|
||||||
|
<el-upload
|
||||||
|
ref="upload"
|
||||||
|
multiple
|
||||||
|
:limit="3"
|
||||||
|
action="#"
|
||||||
|
:before-upload="beforeUpload"
|
||||||
|
:before-remove="beforeRemove"
|
||||||
|
:http-request="addFile"
|
||||||
|
:file-list="fileList">
|
||||||
|
<el-button size="mini" type="info">添加附件</el-button>
|
||||||
|
</el-upload>
|
||||||
|
</el-form-item>
|
||||||
|
</el-col>
|
||||||
|
|
||||||
<!--md内容 -->
|
<!--md内容 -->
|
||||||
<el-col :span="24">
|
<el-col :span="24">
|
||||||
<el-form-item label="邮件内容" prop="content">
|
<el-form-item label="邮件内容" prop="content">
|
||||||
|
|
@ -70,8 +86,7 @@ export default {
|
||||||
recipient: undefined,
|
recipient: undefined,
|
||||||
recipientSuffix: undefined,
|
recipientSuffix: undefined,
|
||||||
content: undefined,
|
content: undefined,
|
||||||
|
file:[]
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
rules: {
|
rules: {
|
||||||
|
|
@ -94,6 +109,8 @@ export default {
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
|
fileList: [],
|
||||||
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -111,25 +128,50 @@ export default {
|
||||||
this.$refs['elForm'].validate(valid => {
|
this.$refs['elForm'].validate(valid => {
|
||||||
if (!valid) return
|
if (!valid) return
|
||||||
|
|
||||||
let data = {
|
let formData = new FormData();
|
||||||
subject: this.formData.subject,
|
let files = this.formData.file;
|
||||||
recipient: this.formData.recipient + this.formData.recipientSuffix,
|
for (let i = 0; i < files.length; i++) {
|
||||||
content: this.formData.content
|
formData.append("fileList", files[i]);
|
||||||
}
|
}
|
||||||
this.$modal.loading("请稍后...")
|
formData.append("subject", this.formData.subject);
|
||||||
sendMail(data).then(res => {
|
formData.append("content", this.formData.content);
|
||||||
|
formData.append("recipient", this.formData.recipient + this.formData.recipientSuffix);
|
||||||
|
|
||||||
|
this.$modal.loading("正在发送,请稍后...")
|
||||||
|
sendMail(formData).then(res => {
|
||||||
this.$modal.notifySuccess("发送成功")
|
this.$modal.notifySuccess("发送成功")
|
||||||
this.$modal.closeLoading()
|
this.$modal.closeLoading()
|
||||||
this.formData.subject=""
|
this.formData.subject = ""
|
||||||
this.formData.recipient=""
|
this.formData.recipient = ""
|
||||||
this.formData.recipientSuffix=""
|
this.formData.recipientSuffix = ""
|
||||||
}).catch(err =>{
|
}).catch(err => {
|
||||||
this.$modal.closeLoading()
|
this.$modal.closeLoading()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
addFile() {
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
//上传文件之前
|
||||||
|
beforeUpload(file) {
|
||||||
|
console.log(file)
|
||||||
|
this.formData.file.push(file)
|
||||||
|
},
|
||||||
|
|
||||||
|
//删除文件之前
|
||||||
|
beforeRemove(file) {
|
||||||
|
let fileList =this.formData.file
|
||||||
|
for (let i = 0; i < fileList.length; i++) {
|
||||||
|
if (fileList[i].uid===file.uid) {
|
||||||
|
fileList.splice(i,1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,18 @@
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<name>业务模块-预警模块</name>
|
<name>业务模块-预警模块</name>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>9</source>
|
||||||
|
<target>9</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
<artifactId>xjs-business-warning</artifactId>
|
<artifactId>xjs-business-warning</artifactId>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,10 +31,11 @@ public class MailController {
|
||||||
@PostMapping("send-mail")
|
@PostMapping("send-mail")
|
||||||
@ApiOperation("发送邮件")
|
@ApiOperation("发送邮件")
|
||||||
@Log(title = "发送邮件", businessType = BusinessType.INSERT)
|
@Log(title = "发送邮件", businessType = BusinessType.INSERT)
|
||||||
public AjaxResult sendMail(@RequestBody MailVo mailVo) {
|
public AjaxResult sendMail(MailVo mailVo) {
|
||||||
MailBean mailBean = new MailBean();
|
MailBean mailBean = new MailBean();
|
||||||
BeanUtils.copyProperties(mailVo, mailBean);
|
BeanUtils.copyProperties(mailVo, mailBean);
|
||||||
mailBean.setMailType(MailBean.MailType.HTML);
|
mailBean.setMailType(MailBean.MailType.ATTACHMENT);
|
||||||
|
|
||||||
mailService.sendMail(mailBean);
|
mailService.sendMail(mailBean);
|
||||||
return AjaxResult.success();
|
return AjaxResult.success();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.xjs.domain.mall;
|
package com.xjs.domain.mall;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
|
@ -38,6 +39,13 @@ public class MailBean implements Serializable {
|
||||||
*/
|
*/
|
||||||
private String absolutePath;
|
private String absolutePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件列表
|
||||||
|
*/
|
||||||
|
private MultipartFile[] fileList;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 邮件发送类型
|
* 邮件发送类型
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
package com.xjs.domain.mall;
|
package com.xjs.domain.mall;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -7,7 +10,7 @@ import java.io.Serializable;
|
||||||
* @author xiejs
|
* @author xiejs
|
||||||
* @since 2022-04-14
|
* @since 2022-04-14
|
||||||
*/
|
*/
|
||||||
|
@Data
|
||||||
public class MailVo implements Serializable {
|
public class MailVo implements Serializable {
|
||||||
private static final long serialVersionUID = -1L;
|
private static final long serialVersionUID = -1L;
|
||||||
|
|
||||||
|
|
@ -24,36 +27,9 @@ public class MailVo implements Serializable {
|
||||||
*/
|
*/
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String toString() {
|
* 文件列表
|
||||||
return "MailVo{" +
|
*/
|
||||||
"recipient='" + recipient + '\'' +
|
private MultipartFile[] fileList;
|
||||||
", subject='" + subject + '\'' +
|
|
||||||
", content='" + content + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getRecipient() {
|
|
||||||
return recipient;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRecipient(String recipient) {
|
|
||||||
this.recipient = recipient;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSubject() {
|
|
||||||
return subject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSubject(String subject) {
|
|
||||||
this.subject = subject;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getContent() {
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setContent(String content) {
|
|
||||||
this.content = content;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,13 @@ import com.xjs.domain.mall.MailBean;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
import org.springframework.core.io.FileSystemResource;
|
import org.springframework.core.io.FileSystemResource;
|
||||||
import org.springframework.mail.SimpleMailMessage;
|
import org.springframework.mail.SimpleMailMessage;
|
||||||
import org.springframework.mail.javamail.JavaMailSender;
|
import org.springframework.mail.javamail.JavaMailSender;
|
||||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.thymeleaf.TemplateEngine;
|
import org.thymeleaf.TemplateEngine;
|
||||||
import org.thymeleaf.context.Context;
|
import org.thymeleaf.context.Context;
|
||||||
|
|
||||||
|
|
@ -18,6 +20,9 @@ import javax.annotation.Resource;
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static com.xjs.consts.RedisConst.MAIL_STATUS;
|
import static com.xjs.consts.RedisConst.MAIL_STATUS;
|
||||||
|
|
@ -131,7 +136,7 @@ public class MailServer {
|
||||||
*
|
*
|
||||||
* @param mailBean 邮箱实体
|
* @param mailBean 邮箱实体
|
||||||
*/
|
*/
|
||||||
private void sendAttachmentMail(MailBean mailBean) throws MessagingException {
|
private void sendAttachmentMail(MailBean mailBean) throws MessagingException, IOException {
|
||||||
MimeMessage mimeMailMessage = null;
|
MimeMessage mimeMailMessage = null;
|
||||||
try {
|
try {
|
||||||
mimeMailMessage = javaMailSender.createMimeMessage();
|
mimeMailMessage = javaMailSender.createMimeMessage();
|
||||||
|
|
@ -140,15 +145,24 @@ public class MailServer {
|
||||||
mimeMessageHelper.setFrom(MAIL_SENDER);
|
mimeMessageHelper.setFrom(MAIL_SENDER);
|
||||||
mimeMessageHelper.setTo(mailBean.getRecipient());
|
mimeMessageHelper.setTo(mailBean.getRecipient());
|
||||||
mimeMessageHelper.setSubject(mailBean.getSubject());
|
mimeMessageHelper.setSubject(mailBean.getSubject());
|
||||||
mimeMessageHelper.setText(mailBean.getContent());
|
mimeMessageHelper.setText(mailBean.getContent(), true);
|
||||||
//文件路径 目前写死在代码中,之后可以当参数传过来,或者在MailBean中添加属性absolutePath
|
|
||||||
FileSystemResource file = new FileSystemResource(new File(mailBean.getAbsolutePath()));
|
//发送附件
|
||||||
//FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/email.png"));
|
if (mailBean.getFileList() != null && mailBean.getFileList().length > 0) {
|
||||||
String fileName = mailBean.getAbsolutePath().substring(mailBean.getAbsolutePath().lastIndexOf(File.separator));
|
for (MultipartFile multipartFile : mailBean.getFileList()) {
|
||||||
//添加附件,第一个参数表示添加到 Email 中附件的名称,第二个参数是图片资源
|
InputStream inputStream = null;
|
||||||
mimeMessageHelper.addAttachment(fileName, file);
|
try {
|
||||||
//多个附件
|
inputStream = multipartFile.getInputStream();
|
||||||
//mimeMessageHelper.addAttachment(fileName1, file1);
|
byte[] bytes = inputStream.readAllBytes();
|
||||||
|
ByteArrayResource bar = new ByteArrayResource(bytes);
|
||||||
|
mimeMessageHelper.addAttachment(Objects.requireNonNull(multipartFile.getOriginalFilename()), bar);
|
||||||
|
} finally {
|
||||||
|
if (inputStream != null) {
|
||||||
|
inputStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
javaMailSender.send(mimeMailMessage);
|
javaMailSender.send(mimeMailMessage);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue