Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2016
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package replyer;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.mail.Flags;
  6. import javax.mail.Folder;
  7. import javax.mail.Message;
  8. import javax.mail.MessagingException;
  9. import javax.mail.NoSuchProviderException;
  10. import javax.mail.Session;
  11. import javax.mail.Store;
  12. import javax.mail.search.FlagTerm;
  13.  
  14. public class MailReader {
  15.  
  16.     public static void check(String host, String storeType, String user,
  17.             String password) {
  18.         try {
  19.  
  20.             // create properties field
  21.             Properties properties = new Properties();
  22.  
  23.             properties.put("mail.pop3.host", host);
  24.             properties.put("mail.pop3.port", "995");
  25.             properties.put("mail.pop3.starttls.enable", "true");
  26.             Session emailSession = Session.getDefaultInstance(properties);
  27.  
  28.             // create the POP3 store object and connect with the pop server
  29.             Store store = emailSession.getStore("pop3s");
  30.  
  31.             store.connect(host, user, password);
  32.  
  33.             // create the folder object and open it
  34.             Folder emailFolder = store.getFolder("INBOX");
  35.             emailFolder.open(Folder.READ_ONLY);
  36.  
  37.             // retrieve the messages from the folder in an array and print it
  38.             Message[] messages = emailFolder.search(new FlagTerm(new Flags(
  39.                     Flags.Flag.SEEN), false));
  40.             System.out.println("messages.length---" + messages.length);
  41.             System.out.println(emailFolder.getUnreadMessageCount());
  42.             for (int i = 0, n = messages.length; i < n; i++) {
  43.                 Message message = messages[i];
  44.                 System.out.println("---------------------------------");
  45.                 System.out.println("Email Number " + (i + 1));
  46.                 System.out.println("Subject: " + message.getSubject());
  47.                 System.out.println("From: " + message.getFrom()[0]);
  48.                 System.out.println("Text: " + message.getContent().toString());
  49.  
  50.             }
  51.            
  52.             // close the store and folder objects
  53.             emailFolder.close(false);
  54.             store.close();
  55.  
  56.         } catch (NoSuchProviderException e) {
  57.             e.printStackTrace();
  58.         } catch (MessagingException e) {
  59.             e.printStackTrace();
  60.         } catch (Exception e) {
  61.             e.printStackTrace();
  62.         }
  63.        
  64.     }
  65.  
  66.     public static void main(String[] args) {
  67.  
  68.         String host = "pop.zoho.com";// change accordingly
  69.         String mailStoreType = "pop3";
  70.         String username = "me@adhene.com";// change accordingly
  71.         String password = "hackme";// change accordingly
  72.  
  73.         check(host, mailStoreType, username, password);
  74.  
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement