public MimeMessage getMessage(String emailSetId, String mailId) { String hql = "from EmailUserSetting emailset where emailset.id = '" + Integer.valueOf(emailSetId) + "'"; EmailUserSetting emailUserSetting = emailUserSettingDao.findEmailUserSetting(hql); Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); Store store = null; Message message[] = null; try { store = session.getStore("pop3"); store.connect( emailUserSetting.getPopService(), emailUserSetting.getEmailAddress(), emailUserSetting.getPassword()); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); message = folder.getMessages(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } // System.out.println("Messages's length: "+message.length); MimeMessage pmm = null; for (int i = 0; i < message.length; i++) { pmm = (MimeMessage) message[i]; String eId = null; try { eId = pmm.getMessageID(); } catch (MessagingException e) { e.printStackTrace(); } if (eId != null && eId.equals(mailId)) { return pmm; } } return null; }
public String getMessages(String userId, String emailSetId) { String hql = "from EmailUserSetting emailset where emailset.id = '" + Integer.valueOf(emailSetId) + "'"; EmailUserSetting emailUserSetting = emailUserSettingDao.findEmailUserSetting(hql); Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); Store store = null; Message message[] = null; try { store = session.getStore("pop3"); store.connect( emailUserSetting.getPopService(), emailUserSetting.getEmailAddress(), emailUserSetting.getPassword()); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); message = folder.getMessages(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } System.out.println("Messages's length: " + message.length); PraseMimeMessage pmm = null; String jsonString = "{'mails':["; for (int i = 0; i < message.length; i++) { pmm = new PraseMimeMessage((MimeMessage) message[i]); try { String mailString = "{"; mailString += "'mailId' : '" + pmm.getMessageId() + "',"; mailString += "'theme':'" + pmm.getSubject() + "',"; mailString += "'sender':'" + pmm.getFrom() + "',"; pmm.setDateFormat("yy年MM月dd日 HH:mm:ss"); mailString += "'time':'" + pmm.getSentDate() + "',"; mailString += "'files':'"; if (pmm.isContainAttach((Part) message[i])) { List<String> files = pmm.getFilenames(); for (int j = 0; j < files.size(); j++) { mailString += files.get(j) + ";"; } mailString += "',"; } else { mailString += "',"; } pmm.getMailContent((Part) message[i]); String content = pmm.getBodyText(); content = content.replaceAll("\r|\n", ""); mailString += "'content':'" + content + "'"; mailString += "},"; jsonString += mailString; } catch (Exception e) { e.printStackTrace(); } } jsonString = jsonString.substring(0, jsonString.length() - 1); jsonString += "]}"; System.out.println(jsonString); return jsonString; }