// pushes a message/attachment to firebase private static void pushToFirebase(List<FirebaseMessage> messagesToPush) { String firebaseUrl = "https://fiery-heat-7720.firebaseio.com/"; final AtomicBoolean done = new AtomicBoolean(false); Firebase ref = new Firebase(firebaseUrl); Firebase msgRef = ref.child("messages"); for (FirebaseMessage msg : messagesToPush) { Map<String, String> msgMap = new HashMap<String, String>(); msgMap.put("fromAddress", msg.getFromAddress()); msgMap.put("subject", msg.getSubject()); msgMap.put("attachments", msg.getEncodedAttach()); // listener will notify completion of the push msgRef .push() .setValue( msgMap, new Firebase.CompletionListener() { @Override public void onComplete(FirebaseError arg0, Firebase arg1) { if (arg0 != null) { System.out.println("Data could not be saved. " + arg0.getMessage()); } else { System.out.println("Data saved successfully"); done.set(true); System.out.println(done.get()); } } }); // keep jvm thread alive until the push to firebase is complete Runtime.getRuntime() .addShutdownHook( new Thread() { public void run() { while (!done.get()) ; } }); } }
public static void main(String args[]) { String host = "imap.gmail.com"; String port = "993"; String userName = args[0]; String password = args[1]; String saveDirectory = "/Users/yjg131/Documents"; /* adding registered users * ideally this should be sourced from db */ List<String> registeredUsers = new ArrayList<String>(); registeredUsers.add("Krishna Nimmagadda <*****@*****.**>"); // properties for server and SSL setting Properties properties = new Properties(); properties.put("mail.imap.host", host); properties.put("mail.imap.port", port); properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.imap.socketFactory.fallback", "false"); properties.setProperty("mail.imap.socketFactory.port", String.valueOf(port)); Session session = Session.getDefaultInstance(properties); try { // connects to the message store Store store = session.getStore("imap"); store.connect(userName, password); // opens the inbox folder Folder folderInbox = store.getFolder("INBOX"); folderInbox.open(Folder.READ_ONLY); // fetches new messages from server Message[] arrayMessages = folderInbox.getMessages(); List<FirebaseMessage> firebaseMessages = new ArrayList<FirebaseMessage>(); // process individual message for (int i = 0; i < arrayMessages.length; i++) { // Message message = arrayMessages[i]; Address[] fromAddress = arrayMessages[i].getFrom(); String from = fromAddress[0].toString(); String subject = arrayMessages[i].getSubject(); String sentDate = arrayMessages[i].getSentDate().toString(); String contentType = arrayMessages[i].getContentType(); String messageContent = ""; String attachFiles = ""; String base64EncodedAttach = ""; if (contentType.contains("multipart") && registeredUsers.contains(from)) { System.out.println("Subject: " + subject); // content may contain attachments Multipart multiPart = (Multipart) arrayMessages[i].getContent(); int numberOfParts = multiPart.getCount(); for (int partCount = 0; partCount < numberOfParts; partCount++) { MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount); if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) { // this part is attachment String fileName = part.getFileName(); System.out.println("Attachment Name: " + fileName); attachFiles += fileName + ","; part.saveFile(saveDirectory + File.separator + fileName); base64EncodedAttach += base64Encode(saveDirectory + File.separator + fileName) + ","; System.out.println(base64EncodedAttach); } else { // this part may be the message content messageContent = part.getContent().toString(); } } if (attachFiles.length() > 1) { attachFiles = attachFiles.substring(0, attachFiles.length() - 1); } if (base64EncodedAttach.length() > 1) { base64EncodedAttach = base64EncodedAttach.substring(0, base64EncodedAttach.length() - 1); } // construct a firebase message FirebaseMessage firebaseMessage = new FirebaseMessage(); firebaseMessage.setFromAddress(from); firebaseMessage.setSubject(subject); firebaseMessage.setEncodedAttach(base64EncodedAttach); // add message to main list firebaseMessages.add(firebaseMessage); } } // push messages to firebase pushToFirebase(firebaseMessages); // disconnect folderInbox.close(false); store.close(); } catch (NoSuchProviderException ex) { System.out.println("No provider for pop3."); ex.printStackTrace(); } catch (MessagingException ex) { System.out.println("Could not connect to the message store"); ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } }