优化邮件发送功能
This commit is contained in:
+1
-1
@@ -259,7 +259,7 @@ public class CaseApplicationController extends BaseController {
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/creatTrialRecord")
|
||||
@PreAuthorize("@ss.hasPermi('caseManagement:list:creatTrialRecord')")
|
||||
// @PreAuthorize("@ss.hasPermi('caseManagement:list:creatTrialRecord')")
|
||||
public AjaxResult creatTrialRecord(@Validated @RequestBody ArbitrateRecord arbitrateRecord){
|
||||
return caseApplicationService.creatTrialRecord(arbitrateRecord);
|
||||
}
|
||||
|
||||
@@ -11,12 +11,19 @@ import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.activation.DataHandler;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.*;
|
||||
import javax.mail.util.ByteArrayDataSource;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.Security;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -80,6 +87,79 @@ public class EmailOutUtil {
|
||||
////System.out.println("发送成功:" + from + ":to:" + to);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param to 收件人
|
||||
* @param message 邮件内容
|
||||
* @param subject 邮件主题
|
||||
* @param fileList 邮件附件
|
||||
*/
|
||||
public void sendEmil(String to, String message, String subject, List<File> fileList, File file) {
|
||||
try {
|
||||
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
|
||||
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
||||
//设置邮件会话参数
|
||||
Properties props = new Properties();
|
||||
//邮箱的发送服务器地址
|
||||
props.setProperty("mail.smtp.host", "smtp.163.com");
|
||||
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
|
||||
props.setProperty("mail.smtp.socketFactory.fallback", "false");
|
||||
//邮箱发送服务器端口,这里设置为465端口
|
||||
props.setProperty("mail.smtp.port", "465");
|
||||
props.setProperty("mail.smtp.socketFactory.port", "465");
|
||||
props.put("mail.smtp.auth", "true");
|
||||
//获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
|
||||
Session session = Session.getDefaultInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(usernameOut, passwordOut);
|
||||
}
|
||||
});
|
||||
//通过会话,得到一个邮件,用于发送
|
||||
Message msg = new MimeMessage(session);
|
||||
//设置发件人
|
||||
msg.setFrom(new InternetAddress(usernameOut));
|
||||
//设置收件人,to为收件人,cc为抄送,bcc为密送
|
||||
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
|
||||
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to, false));
|
||||
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to, false));
|
||||
//设置邮件消息
|
||||
msg.setSubject(subject);
|
||||
msg.setText(message);
|
||||
//设置发送的日期
|
||||
msg.setSentDate(new Date());
|
||||
// 创建邮件正文
|
||||
MimeMultipart multipart = new MimeMultipart();
|
||||
// MimeBodyPart bodyPart = new MimeBodyPart();
|
||||
// bodyPart.setContent("This is the body of the email", "text/html");
|
||||
// multipart.addBodyPart(bodyPart);
|
||||
// 添加附件
|
||||
if (file != null) {
|
||||
MimeBodyPart attachmentPart = new MimeBodyPart();
|
||||
attachmentPart.attachFile(file);
|
||||
attachmentPart.setFileName(MimeUtility.encodeText(file.getName()));
|
||||
multipart.addBodyPart(attachmentPart);
|
||||
//将multipart对象放入邮件
|
||||
msg.setContent(multipart);
|
||||
} else if (fileList != null && fileList.size() > 0) {
|
||||
// 添加附件(多个)
|
||||
if (fileList != null && fileList.size() > 0) {
|
||||
for (File tempfile : fileList) {
|
||||
MimeBodyPart attachmentPart = new MimeBodyPart();
|
||||
attachmentPart.attachFile(tempfile);
|
||||
attachmentPart.setFileName(MimeUtility.encodeText(tempfile.getName()));
|
||||
multipart.addBodyPart(attachmentPart);
|
||||
}
|
||||
msg.setContent(multipart);
|
||||
}
|
||||
}
|
||||
//调用Transport的send方法去发送邮件
|
||||
Transport.send(msg);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送带附件的邮件信息
|
||||
*
|
||||
|
||||
+3
-6
@@ -371,19 +371,16 @@ public class AdjudicationServiceImpl implements IAdjudicationService {
|
||||
if (caseAttach.getAnnexType() == 3) {
|
||||
String annexPath = caseAttach.getAnnexPath();
|
||||
String path = "/home/ruoyi/" + annexPath;
|
||||
// String path = "/home/ruoyi/uploadPath/upload/2023/10/12/裁决书测试20231012test.docx";
|
||||
// String path = "E:/WorkDoc/SH/裁决书测试20231012test.docx";
|
||||
file = new File(path);
|
||||
fileList.add(file);
|
||||
System.out.println("文件长度:" + file.length());
|
||||
System.out.println("文件长度==================:" + file.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (file != null) {
|
||||
JavaMailSender javaMailSender = emailOutUtil.rebuildMailSender();
|
||||
try {
|
||||
// emailOutUtil.sendMessageCarryFile(appEmail, "裁决书", "您好,审核后的裁决书在附件中请查阅", file, "hjbjava@163.com", javaMailSender);
|
||||
emailOutUtil.sendMessageCarryFiles(appEmail, "裁决书", "您好,审核后的裁决书在附件中请查阅", fileList, "lmj1549843951@163.com", javaMailSender);
|
||||
emailOutUtil.sendEmil(appEmail, "您好,审核后的裁决书在附件中请查阅", "签署后的裁决书", fileList, null);
|
||||
emailOutUtil.sendEmil(resEmail, "您好,审核后的裁决书在附件中请查阅", "签署后的裁决书", fileList, null);
|
||||
} catch (Exception e) {
|
||||
System.out.println("邮件发送失败++++++++++++++++++++++++++++++++");
|
||||
System.out.println(e.toString());
|
||||
|
||||
Reference in New Issue
Block a user