/** * Opens session. */ public void open() { try { service.connect(); } catch (final MessagingException msex) { throw new MailException("Open session error", msex); } }
/** * Returns {@code true} if mail session is still connected. * * @return {@code true} if mail session is still connected. */ public boolean isConnected() { return service.isConnected(); }
/** * Return <code>getURLName.toString()</code> if this service has a URLName, * otherwise it will return the default <code>toString</code>. */ public String toString() { URLName url = getURLName(); if (url != null) return url.toString(); else return super.toString(); }
/** * Close this service and terminate its connection. A close * ConnectionEvent is delivered to any ConnectionListeners. Any * Messaging components (Folders, Messages, etc.) belonging to this * service are invalid after this service is closed. Note that the service * is closed even if this method terminates abnormally by throwing * a MessagingException. <p> * * This implementation uses <code>setConnected(false)</code> to set * this service's connected state to <code>false</code>. It will then * send a close ConnectionEvent to any registered ConnectionListeners. * Subclasses overriding this method to do implementation specific * cleanup should call this method as a last step to insure event * notification, probably by including a call to <code>super.close()</code> * in a <code>finally</code> clause. * * @see javax.mail.event.ConnectionEvent * @throws MessagingException for errors while closing */ public synchronized void close() throws MessagingException { setConnected(false); notifyConnectionListeners(ConnectionEvent.CLOSED); }
if (isConnected()) throw new IllegalStateException("already connected"); setURLName(new URLName(protocol, host, port, file, user, null)); pw = session.getPasswordAuthentication(getURLName()); if (pw != null) { if (user == null) { connected = protocolConnect(host, port, user, password); } catch (AuthenticationFailedException ex) { authEx = ex; connected = protocolConnect(host, port, user, password); setURLName(new URLName(protocol, host, port, file, user, password)); session.setPasswordAuthentication(getURLName(), new PasswordAuthentication(user, password)); setConnected(true); notifyConnectionListeners(ConnectionEvent.OPENED);
queueEvent(e, connectionListeners);
/** * Gets the local host from the given service object. * @param s the service to check. * @return the local host or null. * @since JavaMail 1.5.3 */ private String getLocalHost(final Service s) { try { return LogManagerProperties.getLocalHost(s); } catch (SecurityException | NoSuchMethodException | LinkageError ignore) { } catch (final Exception ex) { reportError(s.toString(), ex, ErrorManager.OPEN_FAILURE); } return null; }
/** * Notify all ConnectionListeners. Service implementations are * expected to use this method to broadcast connection events. <p> * * The provided default implementation queues the event into * an internal event queue. An event dispatcher thread dequeues * events from the queue and dispatches them to the registered * ConnectionListeners. Note that the event dispatching occurs * in a separate thread, thus avoiding potential deadlock problems. */ protected synchronized void notifyConnectionListeners(int type) { if (connectionListeners != null) { ConnectionEvent e = new ConnectionEvent(this, type); queueEvent(e, connectionListeners); } /* Fix for broken JDK1.1.x Garbage collector : * The 'conservative' GC in JDK1.1.x occasionally fails to * garbage-collect Threads which are in the wait state. * This would result in thread (and consequently memory) leaks. * * We attempt to fix this by sending a 'terminator' event * to the queue, after we've sent the CLOSED event. The * terminator event causes the event-dispatching thread to * self destruct. */ if (type == ConnectionEvent.CLOSED) terminateQueue(); }
/** * Closes session. */ @Override public void close() { try { service.close(); } catch (final MessagingException mex) { throw new MailException("Failed to close session", mex); } }
if (isConnected()) throw new IllegalStateException("already connected"); setURLName(new URLName(protocol, host, port, file, user, null)); pw = session.getPasswordAuthentication(getURLName()); if (pw != null) { if (user == null) { connected = protocolConnect(host, port, user, password); } catch (AuthenticationFailedException ex) { authEx = ex; connected = protocolConnect(host, port, user, password); setURLName(new URLName(protocol, host, port, file, user, password)); session.setPasswordAuthentication(getURLName(), new PasswordAuthentication(user, password)); setConnected(true); notifyConnectionListeners(ConnectionEvent.OPENED);
/** * Close this service and terminate its connection. A close * ConnectionEvent is delivered to any ConnectionListeners. Any * Messaging components (Folders, Messages, etc.) belonging to this * service are invalid after this service is closed. Note that the service * is closed even if this method terminates abnormally by throwing * a MessagingException. <p> * * This implementation uses <code>setConnected(false)</code> to set * this service's connected state to <code>false</code>. It will then * send a close ConnectionEvent to any registered ConnectionListeners. * Subclasses overriding this method to do implementation specific * cleanup should call this method as a last step to insure event * notification, probably by including a call to <code>super.close()</code> * in a <code>finally</code> clause. * * @see javax.mail.event.ConnectionEvent * @throws MessagingException for errors while closing */ public synchronized void close() throws MessagingException { setConnected(false); notifyConnectionListeners(ConnectionEvent.CLOSED); }
queueEvent(e, connectionListeners);
/** * Gets the local host from the given service object. * @param s the service to check. * @return the local host or null. * @since JavaMail 1.5.3 */ private String getLocalHost(final Service s) { try { return LogManagerProperties.getLocalHost(s); } catch (final SecurityException ignore) { } catch (final NoSuchMethodException ignore) { } catch (final LinkageError ignore) { } catch (final Exception ex) { reportError(s.toString(), ex, ErrorManager.OPEN_FAILURE); } return null; }
queueEvent(e, connectionListeners); terminateQueue();
/** * Close the given JavaMail Service and ignore any thrown exception. This is useful for typical * <code>finally</code> * blocks in manual JavaMail code. * @param service the JavaMail Service to close (may be <code>null</code>) * @see javax.mail.Transport * @see javax.mail.Store */ public static void closeService(Service service) { if (service != null) { try { service.close(); } catch (MessagingException ex) { LOGGER.debug("Could not close JavaMail Service", ex); } } }
if (isConnected()) throw new IllegalStateException("already connected"); setURLName(new URLName(protocol, host, port, file, user, null)); pw = session.getPasswordAuthentication(getURLName()); if (pw != null) { if (user == null) { connected = protocolConnect(host, port, user, password); } catch (AuthenticationFailedException ex) { authEx = ex; connected = protocolConnect(host, port, user, password); setURLName(new URLName(protocol, host, port, file, user, password)); session.setPasswordAuthentication(getURLName(), new PasswordAuthentication(user, password)); setConnected(true); notifyConnectionListeners(ConnectionEvent.OPENED);
/** * Connect to the current host using the specified username * and password. This method is equivalent to calling the * <code>connect(host, user, password)</code> method with null * for the host name. * * @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 javax.mail.event.ConnectionEvent * @see javax.mail.Session#setPasswordAuthentication * @see #connect(java.lang.String, java.lang.String, java.lang.String) * @since JavaMail 1.4 */ public void connect(String user, String password) throws MessagingException { connect(null, user, password); }
/** * Close this service and terminate its physical connection. * The default implementation simply calls setConnected(false) and then * sends a CLOSED event to all registered ConnectionListeners. * Subclasses overriding this method should still ensure it is closed; they should * also ensure that it is called if the connection is closed automatically, for * for example in a finalizer. * *@throws MessagingException if there were errors closing; the connection is still closed */ public void close() throws MessagingException { setConnected(false); notifyConnectionListeners(ConnectionEvent.CLOSED); }
/** * Return <code>getURLName.toString()</code> if this service has a URLName, * otherwise it will return the default <code>toString</code>. */ @Override public String toString() { URLName url = getURLName(); if (url != null) return url.toString(); else return super.toString(); }
protected void notifyConnectionListeners(int type) { queueEvent(new ConnectionEvent(this, type), connectionListeners); }