public static void main(String[] args) throws MessagingException, IOException { IMAPFolder folder = null; Store store = null; String subject = null; Flag flag = null; try { Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); props.setProperty("mail.imap.host", "imap.googlemail.com"); SimpleAuthenticator authenticator = new SimpleAuthenticator("*****@*****.**", "hhy8611hhyy"); Session session = Session.getDefaultInstance(props, null); // Session session = Session.getDefaultInstance(props, authenticator); store = session.getStore("imaps"); // store.connect("imap.googlemail.com","*****@*****.**", "hhy8611hhyy"); // store.connect("imap.googlemail.com","*****@*****.**", "hhy8611hhyy"); store.connect("*****@*****.**", "hhy8611hhy"); // folder = (IMAPFolder) store.getFolder("[Gmail]/Spam"); // This doesn't work for other email // account folder = (IMAPFolder) store.getFolder("inbox"); // This works for both email account if (!folder.isOpen()) folder.open(Folder.READ_WRITE); Message[] messages = messages = folder.getMessages(150, 150); // folder.getMessages(); System.out.println("No of get Messages : " + messages.length); System.out.println("No of Messages : " + folder.getMessageCount()); System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount()); System.out.println("No of New Messages : " + folder.getNewMessageCount()); System.out.println(messages.length); for (int i = 0; i < messages.length; i++) { System.out.println( "*****************************************************************************"); System.out.println("MESSAGE " + (i + 1) + ":"); Message msg = messages[i]; // System.out.println(msg.getMessageNumber()); // Object String; // System.out.println(folder.getUID(msg) subject = msg.getSubject(); System.out.println("Subject: " + subject); System.out.println("From: " + msg.getFrom()[0]); System.out.println("To: " + msg.getAllRecipients()[0]); System.out.println("Date: " + msg.getReceivedDate()); System.out.println("Size: " + msg.getSize()); System.out.println(msg.getFlags()); System.out.println("Body: \n" + msg.getContent()); System.out.println(msg.getContentType()); } } finally { if (folder != null && folder.isOpen()) { folder.close(true); } if (store != null) { store.close(); } } }
/** * metoda preuzmiPoruke preuzima poruke iz odabrane mape i puni listu poruka * * @throws MessagingException * @throws IOException */ private void preuzmiPoruke() throws MessagingException, IOException { Message[] messages; // Open the INBOX folder folder = store.getFolder(this.odabranaMapa); folder.open(Folder.READ_ONLY); messages = folder.getMessages(); this.poruke = new ArrayList<Poruka>(); for (int i = 0; i < messages.length; ++i) { Message m = messages[i]; Poruka p = new Poruka( m.getHeader("Message-ID")[0], m.getSentDate(), m.getFrom()[0].toString(), m.getSubject(), m.getContentType(), m.getSize(), 0, m.getFlags(), null, true, true, m.getContent().toString()); // TODO potraziti broj privitaka, sad je hardkodirano da ih je 0 this.poruke.add(p); } }
/** * Fetches all e-mail headers from the server, with appropriate fields already set. * * @param handler * @return ArrayList of MessageHeaders * @throws ConnectionException */ public ArrayList fetchAllHeaders() throws SystemException, ConnectionException { ArrayList headers = null; Folder fold = null; try { fold = getFolder(); closeFolder(fold); fold = getFolder(); headers = new ArrayList(); EmailHeader header = null; Message[] msgs = fold.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); fp.add("Size"); fp.add("Date"); fold.fetch(msgs, fp); Message msg = null; for (int i = 0; i < msgs.length; i++) { try { header = new EmailHeader(); msg = msgs[i]; header.setMultipart((msg.isMimeType("multipart/*")) ? true : false); header.setMessageId(i + 1); header.setFrom(msg.getFrom()); header.setTo(msg.getRecipients(Message.RecipientType.TO)); header.setCc(msg.getRecipients(Message.RecipientType.CC)); header.setBcc(msg.getRecipients(Message.RecipientType.BCC)); header.setDate(msg.getSentDate()); header.setReplyTo(msg.getReplyTo()); header.setSize(msg.getSize()); header.setSubject(msg.getSubject()); // now set the human readables. header.setDateShown(Formatter.formatDate(header.getDate(), "dd.MM.yyyy HH:mm")); header.setFromShown(Utility.addressArrToString(header.getFrom())); header.setToShown(Utility.addressArrToString(header.getTo())); header.setCcShown(Utility.addressArrToString(header.getCc())); header.setSizeShown(Utility.sizeToHumanReadable(header.getSize())); // it is time to add it to the arraylist headers.add(header); } catch (MessagingException e1) { System.out.println( "Could not parse headers of e-mail. Message might be defuncted or illegal formatted."); } } } catch (Exception e) { System.out.println("Could not fetch message headers. Is mbox connection still alive???"); throw new ConnectionException(e); } return headers; }
/** * Method getSize answers the receiver's message size in octets. * * @return int * @throws SieveMailException */ public int getSize() throws SieveMailException { int result = 0; if (mail != null) { try { result = mail.getSize(); } catch (MessagingException e) { throw new SieveMailException(e); } } return result; }
/** * The match method. * * @param msg the size comparator is applied to this Message's size * @return true if the size is equal, otherwise false */ public boolean match(Message msg) { int size; try { size = msg.getSize(); } catch (Exception e) { return false; } if (size == -1) return false; return super.match(size); }
protected int getMessageSize(Message message, Account account) throws IOException, MessagingException { int count = 0; if (account.getLoginName().contains("aol.com") && message.getContent() instanceof Multipart) { Multipart multipart = (Multipart) message.getContent(); for (int i = 0; i < multipart.getCount(); i++) { count += multipart.getBodyPart(i).getSize(); } } else { count = message.getSize(); } return count; }
/** * Fetches all e-mail headers from the server, with appropriate fields already set. * * @param handler * @return ArrayList of MessageHeaders * @throws ConnectionException */ public ArrayList fetchAllHeaders() throws SystemException, ConnectionException { ArrayList headers = new ArrayList(); Folder fold = null; try { fold = getFolder(); EmailHeader header = null; Message[] msgs = fold.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add(FetchProfile.Item.CONTENT_INFO); fp.add("Size"); fp.add("Date"); fp.add("Disposition-Notification-To"); fp.add("X-Priority"); fp.add("X-MSMail-Priority"); fp.add("Sensitivity"); fold.fetch(msgs, fp); Message msg = null; for (int i = 0; i < msgs.length; i++) { try { header = new EmailHeader(); msg = msgs[i]; header.setMultipart((msg.isMimeType("multipart/*")) ? true : false); header.setMessageId(i + 1); header.setFrom(msg.getFrom()); header.setTo(msg.getRecipients(Message.RecipientType.TO)); header.setCc(msg.getRecipients(Message.RecipientType.CC)); header.setBcc(msg.getRecipients(Message.RecipientType.BCC)); header.setDate(msg.getSentDate()); header.setReplyTo(msg.getReplyTo()); header.setSize(msg.getSize()); header.setSubject(org.claros.commons.utility.Utility.updateTRChars(msg.getSubject())); // now set the human readables. header.setDateShown(Formatter.formatDate(header.getDate(), "dd.MM.yyyy HH:mm")); header.setFromShown( org.claros.commons.utility.Utility.updateTRChars( Utility.addressArrToStringShort(header.getFrom()))); header.setToShown(Utility.addressArrToStringShort(header.getTo())); header.setCcShown(Utility.addressArrToStringShort(header.getCc())); header.setSizeShown(Utility.sizeToHumanReadable(header.getSize())); org.claros.commons.mail.parser.MessageParser.setHeaders(msg, header); boolean deleted = false; if (profile.getProtocol().equals(Constants.IMAP)) { Flags.Flag flags[] = msg.getFlags().getSystemFlags(); if (flags != null) { Flags.Flag flag = null; for (int m = 0; m < flags.length; m++) { flag = flags[m]; if (flag.equals(Flags.Flag.SEEN)) { header.setUnread(new Boolean(false)); } if (flag.equals(Flags.Flag.DELETED)) { deleted = true; } } } } if (header.getUnread() == null) { header.setUnread(new Boolean(true)); } // it is time to add it to the arraylist if (!deleted) { headers.add(header); } } catch (MessagingException e1) { log.error( "Could not parse headers of e-mail. Message might be defuncted or illegal formatted.", e1); } } } catch (MessagingException e) { log.error("Could not fetch message headers. Is mbox connection still alive???", e); // throw new ConnectionException(e); } catch (Exception e) { log.error("Could not fetch message headers. Is mbox connection still alive???", e); // throw new ConnectionException(e); } return headers; }