Package javax.mail

Examples of javax.mail.Store


     
      // Pass "this" for SMTP authentication with Authenticator
      this.session = Session.getInstance(this.props, this);

      // Produces a success logging output
      Store store = null;
      try {
         store = getStore();
      } catch (XmlBlasterException e) {
         log.warning(e.getMessage() + " We poll every " + this.pollingInterval
               + " milliseconds again.");
      }
      finally {
         try {
            if (store != null) store.close();
         } catch (MessagingException e) {
            e.printStackTrace();
         }
      }
   }
View Full Code Here


    * NOTE: You need to call store.close() after usage to cleanup resources.
    * @return never null
    * @throws XmlBlasterException
    */
   private Store getStore() throws XmlBlasterException {
      Store store = null;
      //Fails if username contains '@', should be ok for %40?
      //URLName urln = new URLName(this.pop3Url);
     
      // This constructor automatically transforms a '@' in a username to '%40'
      URLName urln = new URLName(this.xbUri.getScheme(), this.xbUri.getHost(),
            this.xbUri.getPort(), this.xbUri.getPath(),
            this.xbUri.getUser(), this.xbUri.getPassword());
      try {
         store = getSession().getStore(urln);
      } catch (NoSuchProviderException e) {
         throw new XmlBlasterException(this.glob,
               ErrorCode.RESOURCE_CONFIGURATION, Pop3Driver.class.getName(),
               "No POP3 provider for url '" + getUrlWithoutPassword()
                     + "' found", e);
      }
      try {
         store.connect();
         if (!this.isConnected) { // Avoid too many logging output
            log.info("Successfully contacted POP3 server '"
                  + getUrlWithoutPassword() + "', we poll every "
                  + this.pollingInterval + " milliseconds for emails.");
            this.isConnected = true;
View Full Code Here

    */
   public EmailData[] readInbox(boolean clear) throws XmlBlasterException {
      // if (isShutdown()) Does it recover automatically after a shutdown?
      // throw new XmlBlasterException(glob, ErrorCode.INTERNAL_ILLEGALSTATE,
      // Pop3Driver.class.getName(), "The plugin is shutdown");
      Store store = null;
      Folder inbox = null;
      try {
         store = getStore();
         Folder root = store.getDefaultFolder();
         inbox = root.getFolder(POP3_FOLDER);
         inbox.open(Folder.READ_WRITE);
         Message[] msgs = inbox.getMessages();
         if (msgs == null)
            msgs = new Message[0];

         EmailData[] datas = new EmailData[msgs.length];
         for (int i = 0; i < msgs.length; i++) {
            log.fine("Reading message #" + (i+1) + "/" + msgs.length + " from INBOX");
            MimeMessage msg = (MimeMessage) msgs[i];
            if (clear)
               msg.setFlag(Flags.Flag.DELETED, true);

            Address[] froms = msg.getFrom();
            String from = (froms != null && froms.length > 0) ? froms[0]
                  .toString() : "";

            Address[] arr = msg.getAllRecipients();
            if (arr == null)
               arr = new Address[0];
            String[] recips = new String[arr.length];
            for (int j = 0; j < arr.length; j++)
               recips[j] = arr[j].toString();
           
            //String content = retrieveContent(msg); // Would sometimes deliver an attachment
            String content = "";
            datas[i] = new EmailData(recips, from, msg.getSubject(), content);
           
            datas[i].setSentDate(msg.getSentDate());
            datas[i].setReplyTo((InternetAddress[])msg.getReplyTo());
           
            /*
            String[] expires = msg.getHeader(EmailData.EXPIRES_HEADER);
            // "X-xmlBlaster-ExpiryDate: 2005-12-24 16:45:55.322"
            if (expires != null && expires.length > 0) {
               // expires[0]="2005-12-24 16:45:55.322"
               String value = expires[0].trim();
               try {
                  datas[i].setExpiryTime(java.sql.Timestamp.valueOf(value)); // Change to IsoDateParser with UTC!
               }
               catch (Throwable e) {
                  System.err.println("xmlBlaster Pop3Driver.java: Ignoring illegal email header '" + expires[0] + "'");
                  e.printStackTrace();
               }
            }
            else {
            */
               // Expires: Thu, 15 Dec 2005 21:45:01 +0100 (CET)
               String[]  expires = msg.getHeader(EmailData.EXPIRES_HEADER_RFC2156);
               if (expires != null && expires.length > 0) {
                  // Date: Thu, 17 Nov 2005 16:45:12 +0100 (CET)
                  String value = expires[0].trim();
                  try {
                     datas[i].setExpiryTime(MailUtil.dateTimeTS(value));
                  }
                  catch (Throwable e) {
                     System.err.println("xmlBlaster Pop3Driver.java: Ignoring illegal email header '" + expires[0] + "'");
                     e.printStackTrace();
                  }
               }
            //}

            datas[i].setAttachments(MailUtil.accessAttachments(msg));
         }
         return datas;
      } catch (MessagingException e) {
         throw new XmlBlasterException(this.glob,
               ErrorCode.RESOURCE_CONFIGURATION, Pop3Driver.class.getName(),
               "Problems to read POP3 email from '" + getUrlWithoutPassword()
                     + "'", e);
      } finally {
         try {
            if (inbox != null) {
               inbox.close(true);
               inbox = null;
            }
         } catch (Exception e) { // MessagingException, IOException
            log.warning("Ignoring inbox close problem: " + e.toString());
         }
         try {
            if (store != null) {
               store.close();
               store = null;
            }
         } catch (Exception e) { // MessagingException, IOException
            log.warning("Ignoring store close problem: " + e.toString());
         }
View Full Code Here

         // Get a Session object
         Session session = Session.getInstance(props, null);
         session.setDebug(debug);

         // Get a Store object
         Store store = null;
         if (url != null) {
            URLName urln = new URLName(url);
            store = session.getStore(urln);
            if (showAlert) {
               store.addStoreListener(new StoreListener() {
                  public void notification(StoreEvent e) {
                     String s;
                     if (e.getMessageType() == StoreEvent.ALERT)
                        s = "ALERT: ";
                     else
                        s = "NOTICE: ";
                     System.out.println(s + e.getMessage());
                  }
               });
            }
            store.connect();
         }

         Folder folder = store.getDefaultFolder();
         if (folder == null) {
            System.out.println("No default folder");
            System.exit(1);
         }

         folder = folder.getFolder(mbox);
         if (folder == null) {
            System.out.println("Invalid folder");
            System.exit(1);
         }

         // try to open read/write and if that fails try read-only
         try {
            folder.open(Folder.READ_WRITE);
         } catch (MessagingException ex) {
            folder.open(Folder.READ_ONLY);
         }
         int totalMessages = folder.getMessageCount();

         if (totalMessages == 0) {
            System.out.println("Empty folder");
            folder.close(false);
            store.close();
            System.exit(1);
         }

         if (verbose) {
            int newMessages = folder.getNewMessageCount();
            System.out.println("Total messages = " + totalMessages);
            System.out.println("New messages = " + newMessages);
            System.out.println("===============================");
         }

         // Attributes & Flags for all messages ..
         Message[] msgs = folder.getMessages();

         // Use a suitable FetchProfile
         FetchProfile fp = new FetchProfile();
         fp.add(FetchProfile.Item.ENVELOPE);
         fp.add(FetchProfile.Item.FLAGS);
         fp.add("X-Mailer");
         folder.fetch(msgs, fp);

         for (int i = 0; i < msgs.length; i++) {
            int level = 0;
            System.out.println("\n=========MESSAGE #" + (i + 1) + ":=================");
            dumpEnvelope(msgs[i], level);
            level = dumpPart(msgs[i], level);
         }

         folder.close(false);
         store.close();
      } catch (Exception ex) {
         System.out.println("Oops, got exception! " + ex.getMessage());
         ex.printStackTrace();
         System.exit(1);
      }
View Full Code Here

        if(isFromLocalFile()){
            importFromLocalFile(getFiles());
        }
        else{
            Progress.setDisplayName(progress, "Connect to email server");
            Store store = connectToMailService();
            if(store != null)
                importEmail(store);
        }
    }
View Full Code Here

     * @return
     */
    private Store connectToMailService() {
        Properties property = System.getProperties();
        Session session = Session.getInstance(property, null);
        Store store = null;

        try {
            if(getServerType().equals(EmailDataType.SERVER_TYPE_POP3) && isUseSSL()){
                store = session.getStore("pop3s");
            }
            else if(getServerType().equals(EmailDataType.SERVER_TYPE_POP3) && !isUseSSL()){
                store = session.getStore("pop3");
            }
            else if(getServerType().equals(EmailDataType.SERVER_TYPE_IMAP) && !isUseSSL()){
                store = session.getStore("imap");
            }
            else if(getServerType().equals(EmailDataType.SERVER_TYPE_IMAP) && isUseSSL()){
                store = session.getStore("imaps");
            }
            else
                return null;
            store.connect(getServerURL(), getPort(), getUserName(), getUserPsw());

            return store;
        }catch (NoSuchProviderException ex) {
            Exceptions.printStackTrace(ex);
            return null;
View Full Code Here

        }

        boolean found = false;
        try {
            Session session = Session.getDefaultInstance(System.getProperties());
            Store store = session.getStore("pop3");
            store.connect(pop3Host, pop3Port, mailAddress, mailPassword);

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

            Message[] messages = inbox.getMessages();
            for (int i = 0; i < messages.length; i++) {
                if (sender.equals(messages[i].getFrom()[0].toString()) && subject.equals(messages[i].getSubject())) {
                    found = true;
                    messages[i].setFlag(Flag.DELETED, true);
                }
            }
            inbox.close(true);
            store.close();
        } catch (Exception e) {
            LOG.error("Unexpected exception", e);
            fail("Unexpected exception while fetching e-mail");
        }
View Full Code Here

                    + event.getMessageType());
        } catch (IllegalArgumentException e) {
        }
    }
    private void doEventTests(int type) {
        Store source = TestData.getTestStore();
        StoreEvent event = new StoreEvent(source, type, "Hello World");
        assertEquals(source, event.getSource());
        assertEquals("Hello World", event.getMessage());
        assertEquals(type, event.getMessageType());
        StoreListenerTest listener = new StoreListenerTest();
View Full Code Here

                this.message.setSentDate(getSentDate());
            }

            if (this.popBeforeSmtp)
            {
                Store store = session.getStore("pop3");
                store.connect(this.popHost, this.popUsername, this.popPassword);
            }
        }
        catch (MessagingException me)
        {
            throw new EmailException(me);
View Full Code Here

    private void prepareMailbox() throws Exception {
        // connect to mailbox
        Mailbox.clearAll();
        JavaMailSender sender = new DefaultJavaMailSender();
        Store store = sender.getSession().getStore("pop3");
        store.connect("localhost", 25, "bill", "secret");
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);
        folder.expunge();

        // inserts 1 new message
        Message[] messages = new Message[1];
View Full Code Here

TOP

Related Classes of javax.mail.Store

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.