想读取邮箱中的.ics文件-日历调度

2ekbmq32  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(525)

我想阅读邮件收件箱并收集预定事件。我可以通过阅读附件中的.ics文件来完成。outlook可以进行日历调度并向其添加另一个附件。在这种特殊情况下,我无法将.ics文件作为附件读取。但也有一部分是多部分/备选方案。我不能把它当作可读的。

  1. public EmailContentDTO WriteAndReadCalendarFiles(Message message) {
  2. try {
  3. LOGGER.info("Start function WriteAndReadCalendarFiles");
  4. // create calendar entry
  5. Map<String, String> calendarEntry = null;
  6. // get as multipart object
  7. Object content = emailFolder.getMessage(message.getMessageNumber()).getContent();
  8. Multipart multipart = (Multipart) content;
  9. Address[] recipients = message.getAllRecipients();
  10. LOGGER.info("multipart.getCount() : " + multipart.getCount());
  11. // get multipart one by one
  12. for (int k = 0; k < multipart.getCount(); k++) {
  13. // write ICS file
  14. Part part = multipart.getBodyPart(k);
  15. // LOGGER.info("Attachment properties: Disposition:" + part.getDisposition()+", Description: "+ part.getDescription()+" Content-Type: "+part.getContentType() + "File name:" + part.getFileName());
  16. if ((part.getFileName() != null && part.getFileName().endsWith(".ics")) || part.getContentType().contains("text/calendar;")) {
  17. // LOGGER.debug("File name:" + part.getFileName());
  18. String disposition = part.getDisposition();
  19. if (disposition == null) disposition = "attachment";
  20. String fileUploadDir = environment.getRequiredProperty("icsFileUploadDirectory");
  21. //make directory
  22. File uploadDirectory = new File(fileUploadDir);
  23. if (!uploadDirectory.exists()) {
  24. uploadDirectory.mkdirs();
  25. }
  26. String fileName = "invite.ics";
  27. if (disposition.contains("attachment")) {
  28. LOGGER.info("start write ics file");
  29. writeICSFile("invite.ics", fileUploadDir, part.getInputStream());
  30. }
  31. // read ICS file
  32. FileInputStream fileInputStream = new FileInputStream(fileUploadDir + fileName);
  33. // create calendar object
  34. CalendarBuilder builder = new CalendarBuilder();
  35. Calendar calendar = builder.build(fileInputStream);
  36. // read calender object and retrieve data
  37. for (Iterator i = calendar.getComponents().iterator(); i.hasNext(); ) {
  38. Component component = (Component) i.next();
  39. if (component.getName().equalsIgnoreCase("VEVENT")) {
  40. calendarEntry = new HashMap<>();
  41. for (Iterator j = component.getProperties().iterator(); j.hasNext(); ) {
  42. net.fortuna.ical4j.model.Property property = (Property) j.next();
  43. calendarEntry.put(property.getName(), property.getValue());
  44. //get time zome
  45. if (property.getName().equals("DTSTART")) {
  46. Parameter tzid = property.getParameter("TZID");
  47. if (tzid != null) {
  48. calendarEntry.put("TZID", tzid.getValue());
  49. }
  50. }
  51. }
  52. // get ATTENDEES
  53. ArrayList<String> attendees = new ArrayList<>();
  54. //format address to tags email format and add to the list
  55. for (Address address : recipients) {
  56. String nonFormatAddress = address.toString();
  57. if (nonFormatAddress.contains("<")) {
  58. Pattern pattern = Pattern.compile("<(.*?)>", Pattern.DOTALL);
  59. Matcher matcher = pattern.matcher(nonFormatAddress);
  60. if (matcher.find()) {
  61. attendees.add(matcher.group(1));
  62. }
  63. } else {
  64. attendees.add(address.toString());
  65. }
  66. }
  67. DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
  68. Date startTime = dateFormat.parse(calendarEntry.get("DTSTART").split("z")[0]);
  69. Date endTime = dateFormat.parse(calendarEntry.get("DTEND").split("z")[0]);
  70. if (calendarEntry.containsKey("TZID")) {
  71. if (calendarEntry.get("TZID").toLowerCase().contains("sri lanka")) {
  72. startTime = dateGenerator.changeHoursOfDate(startTime, -330);
  73. endTime = dateGenerator.changeHoursOfDate(endTime, -330);
  74. }
  75. }
  76. // Create EmailContentDTO object
  77. EmailContentDTO emailContentDTO = new EmailContentDTO(calendarEntry.get("SUMMARY"),
  78. calendarEntry.get("ORGANIZER").split(":")[1],
  79. calendarEntry.get("UID"), attendees,
  80. (calendarEntry.get("LOCATION") != null) ? calendarEntry.get("LOCATION") : "",
  81. (calendarEntry.get("DESCRIPTION") != null) ? calendarEntry.get("DESCRIPTION").split(":")[0] : "",
  82. startTime, endTime, Integer.parseInt(calendarEntry.get("SEQUENCE")));
  83. return emailContentDTO;
  84. }
  85. }
  86. }
  87. }
  88. return null;
  89. } catch (Exception e) {
  90. LOGGER.error("Function WriteAndReadCalendarFiles : " + e.getMessage(), e);
  91. e.printStackTrace();
  92. return null;
  93. }
  94. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题