List of usage examples for javax.mail Store connect
public void connect(String host, String user, String password) throws MessagingException
From source file:MainClass.java
public static void main(String[] args) throws Exception { Properties props = new Properties(); String host = "yourHost.edu"; String username = "userName"; String password = "mypassword"; String provider = "pop3"; Session session = Session.getDefaultInstance(props, null); Store store = session.getStore(provider); store.connect(host, username, password); Folder inbox = store.getFolder("INBOX"); if (inbox == null) { System.out.println("No INBOX"); System.exit(1);//from w ww. j a v a2s. c o m } inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { System.out.println("Message " + (i + 1)); messages[i].writeTo(System.out); } inbox.close(false); store.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Properties props = new Properties(); String host = "yourserver.edu"; String provider = "pop3"; Session session = Session.getDefaultInstance(props, new MailAuthenticator()); Store store = session.getStore(provider); store.connect(host, null, null); Folder inbox = store.getFolder("INBOX"); if (inbox == null) { System.out.println("No INBOX"); System.exit(1);//from ww w . java2 s. c o m } inbox.open(Folder.READ_ONLY); Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { System.out.println("Message " + (i + 1)); messages[i].writeTo(System.out); } inbox.close(false); store.close(); }
From source file:GetMessageExample.java
public static void main(String args[]) throws Exception { if (args.length != 3) { System.err.println("Usage: java MailExample host username password"); System.exit(-1);//from w w w . java2 s .co m } String host = args[0]; String username = args[1]; String password = args[2]; // Create empty properties Properties props = new Properties(); // Get session Session session = Session.getDefaultInstance(props, 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_ONLY); 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("Read message? [YES to read/QUIT to end]"); String line = reader.readLine(); if ("YES".equalsIgnoreCase(line)) { System.out.println(message[i].getContent()); } else if ("QUIT".equalsIgnoreCase(line)) { break; } } // Close connection folder.close(false); store.close(); }
From source file:net.prhin.mailman.MailMan.java
public static void main(String[] args) { Properties props = new Properties(); props.setProperty(resourceBundle.getString("mailman.mail.store"), resourceBundle.getString("mailman.protocol")); Session session = Session.getInstance(props); try {//from w w w . j a va2s. com Store store = session.getStore(); store.connect(resourceBundle.getString("mailman.host"), resourceBundle.getString("mailman.user"), resourceBundle.getString("mailman.password")); Folder inbox = store.getFolder(resourceBundle.getString("mailman.folder")); inbox.open(Folder.READ_ONLY); inbox.getUnreadMessageCount(); Message[] messages = inbox.getMessages(); for (int i = 0; i <= messages.length / 2; i++) { Message tmpMessage = messages[i]; Multipart multipart = (Multipart) tmpMessage.getContent(); System.out.println("Multipart count: " + multipart.getCount()); for (int j = 0; j < multipart.getCount(); j++) { BodyPart bodyPart = multipart.getBodyPart(j); if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) { if (bodyPart.getContent().getClass().equals(MimeMultipart.class)) { MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent(); for (int k = 0; k < mimeMultipart.getCount(); k++) { if (mimeMultipart.getBodyPart(k).getFileName() != null) { printFileContents(mimeMultipart.getBodyPart(k)); } } } } else { printFileContents(bodyPart); } } } inbox.close(false); store.close(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:monitor.java
public static void main(String argv[]) { if (argv.length != 5) { System.out.println("Usage: monitor <host> <user> <password> <mbox> <freq>"); System.exit(1);// www . j a v a 2 s .c o m } System.out.println("\nTesting monitor\n"); try { Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); // session.setDebug(true); // Get a Store object Store store = session.getStore("imap"); // Connect store.connect(argv[0], argv[1], argv[2]); // Open a Folder Folder folder = store.getFolder(argv[3]); if (folder == null || !folder.exists()) { System.out.println("Invalid folder"); System.exit(1); } folder.open(Folder.READ_WRITE); // Add messageCountListener to listen for new messages folder.addMessageCountListener(new MessageCountAdapter() { public void messagesAdded(MessageCountEvent ev) { Message[] msgs = ev.getMessages(); System.out.println("Got " + msgs.length + " new messages"); // Just dump out the new messages for (int i = 0; i < msgs.length; i++) { try { System.out.println("-----"); System.out.println("Message " + msgs[i].getMessageNumber() + ":"); msgs[i].writeTo(System.out); } catch (IOException ioex) { ioex.printStackTrace(); } catch (MessagingException mex) { mex.printStackTrace(); } } } }); // Check mail once in "freq" MILLIseconds int freq = Integer.parseInt(argv[4]); boolean supportsIdle = false; try { if (folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; f.idle(); supportsIdle = true; } } catch (FolderClosedException fex) { throw fex; } catch (MessagingException mex) { supportsIdle = false; } for (;;) { if (supportsIdle && folder instanceof IMAPFolder) { IMAPFolder f = (IMAPFolder) folder; f.idle(); System.out.println("IDLE done"); } else { Thread.sleep(freq); // sleep for freq milliseconds // This is to force the IMAP server to send us // EXISTS notifications. folder.getMessageCount(); } } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:org.wso2.carbon.registry.es.utils.EmailUtil.java
/** * This method delete all the sent mails. Can be used after a particular test class * * @throws MessagingException//from ww w . j av a 2 s. c o m * @throws IOException */ public static void deleteSentMails() throws MessagingException, IOException { Properties props = new Properties(); props.load(new FileInputStream(new File(TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator + "smtp.properties"))); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString()); Folder sentMail = store.getFolder("[Gmail]/Sent Mail"); sentMail.open(Folder.READ_WRITE); Message[] messages = sentMail.getMessages(); Flags deleted = new Flags(Flags.Flag.DELETED); sentMail.setFlags(messages, deleted, true); sentMail.close(true); store.close(); }
From source file:org.wso2.carbon.registry.es.utils.EmailUtil.java
/** * This method read verification e-mail from Gmail inbox and returns the verification URL. * * @return verification redirection URL. * @throws Exception/* ww w . j av a 2s .c o m*/ */ public static String readGmailInboxForVerification() throws Exception { boolean isEmailVerified = false; long waitTime = 10000; String pointBrowserURL = ""; Properties props = new Properties(); props.load(new FileInputStream(new File(TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator + "smtp.properties"))); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString()); Folder inbox = store.getFolder("inbox"); inbox.open(Folder.READ_WRITE); Thread.sleep(waitTime); long startTime = System.currentTimeMillis(); long endTime = 0; int count = 1; while (endTime - startTime < 180000 && !isEmailVerified) { Message[] messages = inbox.getMessages(); for (Message message : messages) { if (!message.isExpunged()) { try { log.info("Mail Subject:- " + message.getSubject()); if (message.getSubject().contains("EmailVerification")) { pointBrowserURL = getBodyFromMessage(message); isEmailVerified = true; } // Optional : deleting the mail message.setFlag(Flags.Flag.DELETED, true); } catch (MessageRemovedException e) { log.error("Could not read the message subject. Message is removed from inbox"); } } } endTime = System.currentTimeMillis(); Thread.sleep(waitTime); endTime += count * waitTime; count++; } inbox.close(true); store.close(); return pointBrowserURL; }
From source file:org.wso2.carbon.registry.es.utils.EmailUtil.java
/** * This method read e-mails from Gmail inbox and find whether the notification of particular type is found. * * @param notificationType Notification types supported by publisher and store. * @return whether email is found for particular type. * @throws Exception/*from w ww . j a v a2s . c om*/ */ public static boolean readGmailInboxForNotification(String notificationType) throws Exception { boolean isNotificationMailAvailable = false; long waitTime = 10000; Properties props = new Properties(); props.load(new FileInputStream(new File(TestConfigurationProvider.getResourceLocation("GREG") + File.separator + "axis2" + File.separator + "smtp.properties"))); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("smtp.gmail.com", emailAddress, java.nio.CharBuffer.wrap(emailPassword).toString()); Folder inbox = store.getFolder("inbox"); inbox.open(Folder.READ_WRITE); Thread.sleep(waitTime); long startTime = System.currentTimeMillis(); long endTime = 0; int count = 1; while (endTime - startTime < 180000 && !isNotificationMailAvailable) { Message[] messages = inbox.getMessages(); for (Message message : messages) { if (!message.isExpunged()) { try { log.info("Mail Subject:- " + message.getSubject()); if (message.getSubject().contains(notificationType)) { isNotificationMailAvailable = true; } // Optional : deleting the mail message.setFlag(Flags.Flag.DELETED, true); } catch (MessageRemovedException e) { log.error("Could not read the message subject. Message is removed from inbox"); } } } endTime = System.currentTimeMillis(); Thread.sleep(waitTime); endTime += count * waitTime; count++; } inbox.close(true); store.close(); return isNotificationMailAvailable; }
From source file:org.wso2.es.ui.integration.util.ESUtil.java
/** * To check if a e-mail exists with a given subject * * @param smtpPropertyFile smtp property file path * @param password password//from w ww. j ava 2 s. c om * @param email email address * @param subject email subject * @return if the mail exist * @throws java.io.IOException * @throws MessagingException * @throws InterruptedException */ public static String readEmail(String smtpPropertyFile, String password, String email, String subject) throws MessagingException, InterruptedException, IOException { Properties props = new Properties(); String message = null; Folder inbox = null; Store store = null; FileInputStream inputStream = null; try { inputStream = new FileInputStream(new File(smtpPropertyFile)); props.load(inputStream); Session session = Session.getDefaultInstance(props, null); store = session.getStore(IMAPS); store.connect(SMTP_GMAIL_COM, email, password); inbox = store.getFolder(INBOX); inbox.open(Folder.READ_ONLY); message = getMailWithSubject(inbox, subject); } catch (MessagingException e) { LOG.error(getErrorMessage(email), e); throw e; } catch (InterruptedException e) { LOG.error(getErrorMessage(email), e); throw e; } catch (FileNotFoundException e) { LOG.error(getErrorMessage(email), e); throw e; } catch (IOException e) { LOG.error(getErrorMessage(email), e); throw e; } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { LOG.error("Input stream closing failed"); } } if (inbox != null) { try { inbox.close(true); } catch (MessagingException e) { LOG.error("Inbox closing failed"); } } if (store != null) { try { store.close(); } catch (MessagingException e) { LOG.error("Message store closing failed"); } } } return message; }
From source file:dao.FetchEmailDAO.java
/** * * @return/*from ww w. j a va 2s . co m*/ */ public Message[] fetchEmail() { Properties props = new Properties(); props.setProperty("mail.store.protocol", "imaps"); //store inbox emails as Message object, store all into Message[] array. Message[] msgArr = null; try { Session session = Session.getInstance(props, null); Store store = session.getStore(); store.connect("imap-mail.outlook.com", "joshymantou@outlook.com", "mcgrady11"); Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); msgArr = inbox.getMessages(); //System.out.println(msgArr.length); /*for (int i = 0; i < msgArr.length; i++) { Message msg = msgArr[i]; Address[] in = msg.getFrom(); for (Address address : in) { System.out.println("FROM:" + address.toString()); } Multipart mp = (Multipart) msg.getContent(); BodyPart bp = mp.getBodyPart(0); /* System.out.println("SENT DATE:" + msg.getSentDate()); System.out.println("SUBJECT:" + msg.getSubject()); System.out.println("CONTENT:" + bp.getContent()); }*/ ArrayUtils.reverse(msgArr); return msgArr; } catch (Exception mex) { mex.printStackTrace(); } return msgArr; }