调解系统后端服务

EmailUtil.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.ruoyi.common.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import javax.mail.*;
  4. import javax.mail.internet.MimeMessage;
  5. import java.io.IOException;
  6. @Slf4j
  7. public final class EmailUtil {
  8. private static final String multipart = "multipart/*";
  9. public static String getMessageId(Part part, Session session) throws Exception {
  10. if (!part.isMimeType(multipart)) {
  11. return "";
  12. }
  13. Multipart multipart = (Multipart) part.getContent();
  14. for (int i = 0; i < multipart.getCount(); i++) {
  15. BodyPart bodyPart = multipart.getBodyPart(i);
  16. if(bodyPart.getContentType().contains("Message/Rfc822")){
  17. MimeMessage mimeMessage = new MimeMessage(session, bodyPart.getInputStream());
  18. if(mimeMessage.getSubject()!=null&&mimeMessage.getSubject().contains("调解书")){
  19. String[] split = mimeMessage.getSubject().split("调解书");
  20. if(split.length>0){
  21. return split[0];
  22. }
  23. }
  24. }
  25. } return "";
  26. }
  27. public static boolean isContainAttachment(Part part) {
  28. boolean attachFlag = false;
  29. try {
  30. if (part.isMimeType(multipart)) {
  31. Multipart mp = (Multipart) part.getContent();
  32. for (int i = 0; i < mp.getCount(); i++) {
  33. BodyPart mpart = mp.getBodyPart(i);
  34. String disposition = mpart.getDisposition();
  35. if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)) || (disposition.equals(Part.INLINE))))
  36. attachFlag = true;
  37. else if (mpart.isMimeType(multipart)) {
  38. attachFlag = isContainAttachment((Part) mpart);
  39. } else {
  40. String contype = mpart.getContentType();
  41. if (contype.toLowerCase().contains("application"))
  42. attachFlag = true;
  43. if (contype.toLowerCase().contains("name"))
  44. attachFlag = true;
  45. }
  46. }
  47. } else if (part.isMimeType("message/rfc822")) {
  48. attachFlag = isContainAttachment((Part) part.getContent());
  49. }
  50. } catch (MessagingException | IOException e) {
  51. e.printStackTrace();
  52. }
  53. return attachFlag;
  54. }
  55. }