/** * Constructor. * * @param session Session object for this service * @param urlname URLName object to be used for this service */ protected Service(Session session, URLName urlname) { this.session = session; debug = session.getDebug(); url = urlname; /* * Initialize the URLName with default values. * The URLName will be updated when connect is called. */ String protocol = null; String host = null; int port = -1; String user = null; String password = null; String file = null; // get whatever information we can from the URL // XXX - url should always be non-null here, Session // passes it into the constructor if (url != null) { protocol = url.getProtocol(); host = url.getHost(); port = url.getPort(); user = url.getUsername(); password = url.getPassword(); file = url.getFile(); } // try to get protocol-specific default properties if (protocol != null) { if (host == null) host = session.getProperty("mail." + protocol + ".host"); if (user == null) user = session.getProperty("mail." + protocol + ".user"); } // try to get mail-wide default properties if (host == null) host = session.getProperty("mail.host"); if (user == null) user = session.getProperty("mail.user"); // try using the system username if (user == null) { try { user = System.getProperty("user.name"); } catch (SecurityException sex) { // XXX - it's not worth creating a MailLogger just for this // logger.log(Level.CONFIG, "Can't get user.name property", sex); } } url = new URLName(protocol, host, port, file, user, password); // create or choose the appropriate event queue String scope = session.getProperties().getProperty("mail.event.scope", "folder"); Executor executor = (Executor) session.getProperties().get("mail.event.executor"); if (scope.equalsIgnoreCase("application")) q = EventQueue.getApplicationEventQueue(executor); else if (scope.equalsIgnoreCase("session")) q = session.getEventQueue(); else // if (scope.equalsIgnoreCase("store") || // scope.equalsIgnoreCase("folder")) q = new EventQueue(executor); }
public static void ClaimsMsgShow( java.lang.String argvs[], Folder folder, Store store, biz.systempartners.claims.ClaimsViewer claimsViewer) { if (claimsViewer == null) {} int msgnum = -1; int optind = 0; InputStream msgStream = System.in; if (argvs != null) { for (optind = 0; optind < argvs.length; optind++) { if (argvs[optind].equals("-T")) { protocol = argvs[++optind]; } else if (argvs[optind].equals("-H")) { host = argvs[++optind]; } else if (argvs[optind].equals("-U")) { user = argvs[++optind]; } else if (argvs[optind].equals("-P")) { password = argvs[++optind]; } else if (argvs[optind].equals("-v")) { verbose = true; } else if (argvs[optind].equals("-D")) { debug = true; } else if (argvs[optind].equals("-f")) { mbox = argvs[++optind]; } else if (argvs[optind].equals("-L")) { url = argvs[++optind]; } else if (argvs[optind].equals("-p")) { port = Integer.parseInt(argvs[++optind]); } else if (argvs[optind].equals("-s")) { showStructure = true; } else if (argvs[optind].equals("-S")) { saveAttachments = true; } else if (argvs[optind].equals("-m")) { showMessage = true; } else if (argvs[optind].equals("-a")) { showAlert = true; } else if (argvs[optind].equals("--")) { optind++; break; } else if (argvs[optind].startsWith("-")) { System.out.println("Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]"); System.out.println("\t[-P password] [-f mailbox] [msgnum] [-v] [-D] [-s] [-S] [-a]"); System.out.println("or msgshow -m [-v] [-D] [-s] [-S] < msg"); System.exit(1); } else { break; } } } try { if (optind < argvs.length) msgnum = Integer.parseInt(argvs[optind]); // msgnum = 1; // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); session.setDebug(debug); if (showMessage) { MimeMessage msg = new MimeMessage(session, msgStream); dumpPart(msg, claimsViewer); // System.exit(0); } /* // 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(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, port, user, password); else store.connect(); } // Open the Folder 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("-------------------------------"); } if (msgnum == -1) { // 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++) { System.out.println("--------------------------"); System.out.println("MESSAGE #" + (i + 1) + ":"); dumpEnvelope(msgs[i]); // dumpPart(msgs[i]); } } else { Message[] msgs = folder.getMessages(); for (int n = 0; n < msgs.length; n++) { System.out.println("Getting message number: " + n + 1); Message m = null; try { m = folder.getMessage(msgnum + 1); // m.setDisposition( dumpPart(m, claimsViewer); // m.setExpunged(true); } catch (IndexOutOfBoundsException iex) { System.out.println("Message number out of range"); } } folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); } // folder.expunge(); folder.close(true); store.close(); } catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); // System.exit(1); } // System.exit(0); }
/** * Similar to connect(host, user, password) except a specific port can be specified. * * @param host the host to connect to * @param port the port to connect to (-1 means the default port) * @param user the user name * @param password this user's password * @exception AuthenticationFailedException for authentication failures * @exception MessagingException for other failures * @exception IllegalStateException if the service is already connected * @see #connect(java.lang.String, java.lang.String, java.lang.String) * @see javax.mail.event.ConnectionEvent */ public synchronized void connect(String host, int port, String user, String password) throws MessagingException { // see if the service is already connected if (isConnected()) throw new IllegalStateException("already connected"); PasswordAuthentication pw; boolean connected = false; boolean save = false; String protocol = null; String file = null; // get whatever information we can from the URL // XXX - url should always be non-null here, Session // passes it into the constructor if (url != null) { protocol = url.getProtocol(); if (host == null) host = url.getHost(); if (port == -1) port = url.getPort(); if (user == null) { user = url.getUsername(); if (password == null) // get password too if we need it password = url.getPassword(); } else { if (password == null && user.equals(url.getUsername())) // only get the password if it matches the username password = url.getPassword(); } file = url.getFile(); } // try to get protocol-specific default properties if (protocol != null) { if (host == null) host = session.getProperty("mail." + protocol + ".host"); if (user == null) user = session.getProperty("mail." + protocol + ".user"); } // try to get mail-wide default properties if (host == null) host = session.getProperty("mail.host"); if (user == null) user = session.getProperty("mail.user"); // try using the system username if (user == null) { try { user = System.getProperty("user.name"); } catch (SecurityException sex) { // XXX - it's not worth creating a MailLogger just for this // logger.log(Level.CONFIG, "Can't get user.name property", sex); } } // if we don't have a password, look for saved authentication info if (password == null && url != null) { // canonicalize the URLName setURLName(new URLName(protocol, host, port, file, user, null)); pw = session.getPasswordAuthentication(getURLName()); if (pw != null) { if (user == null) { user = pw.getUserName(); password = pw.getPassword(); } else if (user.equals(pw.getUserName())) { password = pw.getPassword(); } } else save = true; } // try connecting, if the protocol needs some missing // information (user, password) it will not connect. // if it tries to connect and fails, remember why for later. AuthenticationFailedException authEx = null; try { connected = protocolConnect(host, port, user, password); } catch (AuthenticationFailedException ex) { authEx = ex; } // if not connected, ask the user and try again if (!connected) { InetAddress addr; try { addr = InetAddress.getByName(host); } catch (UnknownHostException e) { addr = null; } pw = session.requestPasswordAuthentication(addr, port, protocol, null, user); if (pw != null) { user = pw.getUserName(); password = pw.getPassword(); // have the service connect again connected = protocolConnect(host, port, user, password); } } // if we're not connected by now, we give up if (!connected) { if (authEx != null) throw authEx; else if (user == null) throw new AuthenticationFailedException("failed to connect, no user name specified?"); else if (password == null) throw new AuthenticationFailedException("failed to connect, no password specified?"); else throw new AuthenticationFailedException("failed to connect"); } setURLName(new URLName(protocol, host, port, file, user, password)); if (save) session.setPasswordAuthentication(getURLName(), new PasswordAuthentication(user, password)); // set our connected state setConnected(true); // finally, deliver the connection event notifyConnectionListeners(ConnectionEvent.OPENED); }