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