Пример #1
0
  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();
      }
    }
  }
  public List<MailInfo> receive() throws Exception {

    // TODO: externalize ....
    // mail server connection parameters
    // String host = "SSL0.OVH.NET";
    // String user = "******";
    // String password = "******";

    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    Properties pop3Props = new Properties();
    pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
    pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
    pop3Props.setProperty("mail.pop3.port", port);
    pop3Props.setProperty("mail.pop3.socketFactory.port", port);

    URLName url = new URLName("pop3", host, Integer.parseInt(port), "", user, password);

    Session session = Session.getInstance(pop3Props, null);
    Store store = new POP3SSLStore(session, url);
    store.connect();

    Folder inbox = store.getFolder("Inbox");
    inbox.open(Folder.READ_WRITE);

    // get the list of inbox messages
    Message[] messages = inbox.getMessages();

    if (messages.length == 0) log.info("No messages found.");

    List<MailInfo> mails = new ArrayList<>();

    for (int i = 0; i < messages.length; i++) {
      MailInfo mail = new MailInfo();
      writePart(messages[i], mail);

      mails.add(mail);

      // TO REACTIVATE ...
      messages[i].setFlag(Flags.Flag.DELETED, true);
    }

    inbox.close(true);
    store.close();

    return mails;
  }
  public static void main(String[] args) throws Throwable {
    String mailbox = null;
    boolean DEBUG = false;

    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-debug")) {
        DEBUG = true;

      } else if (mailbox == null) {
        mailbox = args[i];

      } else {
        usage("Spurious command line: " + args);
      }
    }

    if (mailbox == null) {
      usage("Missing mailbox");
    }

    // Get a connection to the mail server
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(DEBUG);

    Provider[] providers = session.getProviders();
    for (int i = 0; i < providers.length; i++) {
      Provider provider = providers[i];
      out.println(provider);
    }

    Store store = session.getStore("imap");
    Folder folder = store.getFolder(mailbox);
    Message[] messages = folder.getMessages();
    out.println("Folder " + folder.getName() + " with " + messages.length + " messages");
    for (int i = 0; i < messages.length; i++) {
      Message message = messages[i];
      out.println("  " + i + ": " + message.getSubject());
    }

    out.println("");
  }
  private Object receiveMail() throws Exception {
    Folder folder = null;
    Store store = null;
    ConfigurationPropertyManager configManager =
        ConfigurationPropertyManager.getConfigurationPropertyManager();
    final String email_address = configManager.getPropValues("gmail_address");
    final String password = configManager.getPropValues("gmail_pw");

    try {
      Properties props = System.getProperties();
      props.setProperty("mail.store.protocol", "imaps");

      // Session session = Session.getDefaultInstance(props, null);
      Session session = Session.getInstance(props, new GMailAuthenticator(email_address, password));
      store = session.getStore("imaps");
      store.connect("imap.gmail.com", email_address, password);
      folder = store.getFolder("Inbox");
      folder.open(Folder.READ_WRITE);
      // Message messages[] = folder.getMessages();
      // search for all "unseen" messages
      Flags seen = new Flags(Flags.Flag.SEEN);
      FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
      Message messages[] = folder.search(unseenFlagTerm);

      for (int i = 0; i < messages.length; i++) {
        folder.getMessage(
            messages[i].getMessageNumber()); // this will mark the retrieved messages as READ	    	
      }
      String s = updateEmailrepository(messages);
      // System.out.println(s);
      StringTerm st = new StringTermImpl(s);
      return st;

    } catch (Exception e) {
      System.out.println(e.toString());
    } finally {
      if (folder != null) {
        folder.close(true);
      }
    }
    return null;
  }
Пример #5
0
  public static void main(String args[]) throws Exception {
    // String host = args[0];
    // String username = args[1];
    // String password = args[2];

    String host = "triggeremails.com";
    String username = "******";
    String password = "******";

    // Get session
    Session session = Session.getInstance(System.getProperties(), null);

    // Get the store
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    // Get directory
    Message message[] = folder.getMessages();
    for (int i = 0, n = message.length; i < n; i++) {
      System.out.println(i + ": " + message[i].getFrom()[0] + "\t" + message[i].getSubject());

      System.out.println("Do you want to delete message? [YES to delete]");
      String line = reader.readLine();
      // Mark as deleted if appropriate
      if ("YES".equals(line)) {
        message[i].setFlag(Flags.Flag.DELETED, true);
      }
    }

    // Close connection
    folder.close(true);
    store.close();
  }