⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 service.java

📁 java Email you can use it to send email to others
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License").  You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code.  If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license."  If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above.  However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. *//* * @(#)Service.java	1.33 07/05/14 */package javax.mail;import java.io.*;import java.net.*;import java.util.*;import javax.mail.event.*;/** * An abstract class that contains the functionality * common to messaging services, such as stores and transports. <p> * A messaging service is created from a <code>Session</code> and is * named using a <code>URLName</code>.  A service must be connected * before it can be used.  Connection events are sent to reflect * its connection status. * * @author Christopher Cotton * @author Bill Shannon * @author Kanwar Oberoi * @version 1.33, 07/05/14 */public abstract class Service {    /**     * The session from which this service was created.     */    protected Session	session;    /**     * The <code>URLName</code> of this service.     */    protected URLName	url = null;    /**     * Debug flag for this service.  Set from the session's debug     * flag when this service is created.     */    protected boolean	debug = false;    private boolean	connected = false;    private Vector	connectionListeners = null;    /**     * 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;	url = urlname;	debug = session.getDebug();    }    /**     * A generic connect method that takes no parameters. Subclasses     * can implement the appropriate authentication schemes. Subclasses     * that need additional information might want to use some properties     * or might get it interactively using a popup window. <p>     *     * If the connection is successful, an "open" <code>ConnectionEvent</code>     * is delivered to any <code>ConnectionListeners</code> on this service. <p>     *     * Most clients should just call this method to connect to the service.<p>     *     * It is an error to connect to an already connected service. <p>     *     * The implementation provided here simply calls the following     * <code>connect(String, String, String)</code> method with nulls.     *     * @exception AuthenticationFailedException	for authentication failures     * @exception MessagingException	for other failures     * @exception IllegalStateException	if the service is already connected     *     * @see javax.mail.event.ConnectionEvent     */    public void connect() throws MessagingException {	connect(null, null, null);    }    /**     * Connect to the specified address. This method provides a simple     * authentication scheme that requires a username and password. <p>     *     * If the connection is successful, an "open" <code>ConnectionEvent</code>     * is delivered to any <code>ConnectionListeners</code> on this service. <p>     *     * It is an error to connect to an already connected service. <p>     *     * The implementation in the Service class will collect defaults     * for the host, user, and password from the session, from the     * <code>URLName</code> for this service, and from the supplied     * parameters and then call the <code>protocolConnect</code> method.     * If the <code>protocolConnect</code> method returns <code>false</code>,     * the user will be prompted for any missing information and the     * <code>protocolConnect</code> method will be called again.  The     * subclass should override the <code>protocolConnect</code> method.     * The subclass should also implement the <code>getURLName</code>     * method, or use the implementation in this class. <p>     *     * On a successful connection, the <code>setURLName</code> method is     * called with a URLName that includes the information used to make     * the connection, including the password. <p>     *     * If the username passed in is null, a default value will be chosen     * as described above.     *     * If the password passed in is null and this is the first successful     * connection to this service, the user name and the password     * collected from the user will be saved as defaults for subsequent     * connection attempts to this same service when using other Service object     * instances (the connection information is typically always saved within     * a particular Service object instance).  The password is saved using the     * Session method <code>setPasswordAuthentication</code>.  If the     * password passed in is not null, it is not saved, on the assumption     * that the application is managing passwords explicitly.     *     * @param host 	the host to connect to     * @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     */    public void connect(String host, String user, String password)			throws MessagingException {	connect(host, -1, user, password);    }    /**     * 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);    }    /**     * 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) {		if (debug)		    sex.printStackTrace(session.getDebugOut());	    }	}	// 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();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -