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

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

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

public EmailContentDTO WriteAndReadCalendarFiles(Message message) {
        try {
            LOGGER.info("Start function WriteAndReadCalendarFiles");

            // create calendar entry
            Map<String, String> calendarEntry = null;

            // get as multipart object
            Object content = emailFolder.getMessage(message.getMessageNumber()).getContent();

            Multipart multipart = (Multipart) content;

            Address[] recipients = message.getAllRecipients();
            LOGGER.info("multipart.getCount() : " + multipart.getCount());
            // get multipart one by one
            for (int k = 0; k < multipart.getCount(); k++) {

                // write ICS file
                Part part = multipart.getBodyPart(k);
//                LOGGER.info("Attachment properties: Disposition:" + part.getDisposition()+", Description: "+ part.getDescription()+" Content-Type: "+part.getContentType() + "File name:" + part.getFileName());
                if ((part.getFileName() != null && part.getFileName().endsWith(".ics")) || part.getContentType().contains("text/calendar;")) {
//                    LOGGER.debug("File name:" + part.getFileName());
                    String disposition = part.getDisposition();
                    if (disposition == null) disposition = "attachment";
                    String fileUploadDir = environment.getRequiredProperty("icsFileUploadDirectory");
                    //make directory
                    File uploadDirectory = new File(fileUploadDir);
                    if (!uploadDirectory.exists()) {
                        uploadDirectory.mkdirs();
                    }
                    String fileName = "invite.ics";
                    if (disposition.contains("attachment")) {
                        LOGGER.info("start write ics file");
                        writeICSFile("invite.ics", fileUploadDir, part.getInputStream());
                    }

                    // read ICS file
                    FileInputStream fileInputStream = new FileInputStream(fileUploadDir + fileName);

                    // create calendar object
                    CalendarBuilder builder = new CalendarBuilder();
                    Calendar calendar = builder.build(fileInputStream);

                    // read calender object and retrieve data
                    for (Iterator i = calendar.getComponents().iterator(); i.hasNext(); ) {
                        Component component = (Component) i.next();
                        if (component.getName().equalsIgnoreCase("VEVENT")) {
                            calendarEntry = new HashMap<>();
                            for (Iterator j = component.getProperties().iterator(); j.hasNext(); ) {
                                net.fortuna.ical4j.model.Property property = (Property) j.next();
                                calendarEntry.put(property.getName(), property.getValue());
                                //get time zome
                                if (property.getName().equals("DTSTART")) {
                                    Parameter tzid = property.getParameter("TZID");
                                    if (tzid != null) {
                                        calendarEntry.put("TZID", tzid.getValue());
                                    }
                                }
                            }

                            // get ATTENDEES
                            ArrayList<String> attendees = new ArrayList<>();

                            //format address to tags email format and add to the list
                            for (Address address : recipients) {
                                String nonFormatAddress = address.toString();
                                if (nonFormatAddress.contains("<")) {
                                    Pattern pattern = Pattern.compile("<(.*?)>", Pattern.DOTALL);
                                    Matcher matcher = pattern.matcher(nonFormatAddress);
                                    if (matcher.find()) {
                                        attendees.add(matcher.group(1));
                                    }
                                } else {
                                    attendees.add(address.toString());
                                }
                            }

                            DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
                            Date startTime = dateFormat.parse(calendarEntry.get("DTSTART").split("z")[0]);
                            Date endTime = dateFormat.parse(calendarEntry.get("DTEND").split("z")[0]);

                            if (calendarEntry.containsKey("TZID")) {
                                if (calendarEntry.get("TZID").toLowerCase().contains("sri lanka")) {
                                    startTime = dateGenerator.changeHoursOfDate(startTime, -330);
                                    endTime = dateGenerator.changeHoursOfDate(endTime, -330);
                                }
                            }

                            // Create EmailContentDTO object
                            EmailContentDTO emailContentDTO = new EmailContentDTO(calendarEntry.get("SUMMARY"),
                                    calendarEntry.get("ORGANIZER").split(":")[1],
                                    calendarEntry.get("UID"), attendees,
                                    (calendarEntry.get("LOCATION") != null) ? calendarEntry.get("LOCATION") : "",
                                    (calendarEntry.get("DESCRIPTION") != null) ? calendarEntry.get("DESCRIPTION").split(":")[0] : "",
                                    startTime, endTime, Integer.parseInt(calendarEntry.get("SEQUENCE")));
                            return emailContentDTO;
                        }
                    }
                }
            }
            return null;
        } catch (Exception e) {
            LOGGER.error("Function WriteAndReadCalendarFiles : " + e.getMessage(), e);
            e.printStackTrace();
            return null;
        }
    }

暂无答案!

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

相关问题