Methods Summary |
---|
public synchronized void | addProvider(javax.mail.Provider provider)Add a provider to the session.
providers.addElement(provider);
providersByClassName.put(provider.getClassName(), provider);
if (!providersByProtocol.containsKey(provider.getProtocol()))
providersByProtocol.put(provider.getProtocol(), provider);
|
private static java.lang.ClassLoader | getContextClassLoader()
return (ClassLoader)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
} catch (SecurityException ex) { }
return cl;
}
});
|
public synchronized boolean | getDebug()Get the debug setting for this Session.
return debug;
|
public synchronized java.io.PrintStream | getDebugOut()Returns the stream to be used for debugging output. If no stream
has been set, System.out is returned.
if (out == null)
return System.out;
else
return out;
|
public static synchronized javax.mail.Session | getDefaultInstance(java.util.Properties props, javax.mail.Authenticator authenticator)Get the default Session object. If a default has not yet been
setup, a new Session object is created and installed as the
default.
Since the default session is potentially available to all
code executing in the same Java virtual machine, and the session
can contain security sensitive information such as user names
and passwords, access to the default session is restricted.
The Authenticator object, which must be created by the caller,
is used indirectly to check access permission. The Authenticator
object passed in when the session is created is compared with
the Authenticator object passed in to subsequent requests to
get the default session. If both objects are the same, or are
from the same ClassLoader, the request is allowed. Otherwise,
it is denied.
Note that if the Authenticator object used to create the session
is null, anyone can get the default session by passing in null.
Note also that the Properties object is used only the first time
this method is called, when a new Session object is created.
Subsequent calls return the Session object that was created by the
first call, and ignore the passed Properties object. Use the
getInstance method to get a new Session object every
time the method is called.
In JDK 1.2, additional security Permission objects may be used to
control access to the default session.
if (defaultSession == null)
defaultSession = new Session(props, authenticator);
else {
// have to check whether caller is allowed to see default session
if (defaultSession.authenticator == authenticator)
; // either same object or both null, either way OK
else if (defaultSession.authenticator != null &&
authenticator != null &&
defaultSession.authenticator.getClass().getClassLoader() ==
authenticator.getClass().getClassLoader())
; // both objects came from the same class loader, OK
else
// anything else is not allowed
throw new SecurityException("Access to default session denied");
}
return defaultSession;
|
public static javax.mail.Session | getDefaultInstance(java.util.Properties props)Get the default Session object. If a default has not yet been
setup, a new Session object is created and installed as the
default.
Note that a default session created with no Authenticator is
available to all code executing in the same Java virtual
machine, and the session can contain security sensitive
information such as user names and passwords.
return getDefaultInstance(props, null);
|
public javax.mail.Folder | getFolder(javax.mail.URLName url)Get a closed Folder object for the given URLName. If the requested
Folder object cannot be obtained, null is returned.
The "scheme" part of the URL string (Refer RFC 1738) is used
to locate the Store protocol. The rest of the URL string (that is,
the "schemepart", as per RFC 1738) is used by that Store
in a protocol dependent manner to locate and instantiate the
appropriate Folder object.
Note that RFC 1738 also specifies the syntax for the
"schemepart" for IP-based protocols (IMAP4, POP3, etc.).
Providers of IP-based mail Stores should implement that
syntax for referring to Folders.
// First get the Store
Store store = getStore(url);
store.connect();
return store.getFolder(url);
|
public static javax.mail.Session | getInstance(java.util.Properties props, javax.mail.Authenticator authenticator)Get a new Session object.
return new Session(props, authenticator);
|
public static javax.mail.Session | getInstance(java.util.Properties props)Get a new Session object.
return new Session(props, null);
|
public javax.mail.PasswordAuthentication | getPasswordAuthentication(javax.mail.URLName url)Return any saved PasswordAuthentication for this (store or transport)
URLName. Normally used only by store or transport implementations.
return (PasswordAuthentication)authTable.get(url);
|
public java.util.Properties | getProperties()Returns the Properties object associated with this Session
return props;
|
public java.lang.String | getProperty(java.lang.String name)Returns the value of the specified property. Returns null
if this property does not exist.
return props.getProperty(name);
|
public synchronized javax.mail.Provider | getProvider(java.lang.String protocol)Returns the default Provider for the protocol
specified. Checks mail.<protocol>.class property
first and if it exists, returns the Provider
associated with this implementation. If it doesn't exist,
returns the Provider that appeared first in the
configuration files. If an implementation for the protocol
isn't found, throws NoSuchProviderException
if (protocol == null || protocol.length() <= 0) {
throw new NoSuchProviderException("Invalid protocol: null");
}
Provider _provider = null;
// check if the mail.<protocol>.class property exists
String _className = props.getProperty("mail."+protocol+".class");
if (_className != null) {
if (debug) {
pr("DEBUG: mail."+protocol+
".class property exists and points to " +
_className);
}
_provider = (Provider)providersByClassName.get(_className);
}
if (_provider != null) {
return _provider;
} else {
// returning currently default protocol in providersByProtocol
_provider = (Provider)providersByProtocol.get(protocol);
}
if (_provider == null) {
throw new NoSuchProviderException("No provider for " + protocol);
} else {
if (debug) {
pr("DEBUG: getProvider() returning " +
_provider.toString());
}
return _provider;
}
|
public synchronized javax.mail.Provider[] | getProviders()This method returns an array of all the implementations installed
via the javamail.[default.]providers files that can
be loaded using the ClassLoader available to this application.
Provider[] _providers = new Provider[providers.size()];
providers.copyInto(_providers);
return _providers;
|
private static java.io.InputStream | getResourceAsStream(java.lang.Class c, java.lang.String name)
try {
return (InputStream)
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws IOException {
return c.getResourceAsStream(name);
}
});
} catch (PrivilegedActionException e) {
throw (IOException)e.getException();
}
|
private static java.net.URL[] | getResources(java.lang.ClassLoader cl, java.lang.String name)
return (URL[])
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
URL[] ret = null;
try {
Vector v = new Vector();
Enumeration e = cl.getResources(name);
while (e != null && e.hasMoreElements()) {
URL url = (URL)e.nextElement();
if (url != null)
v.addElement(url);
}
if (v.size() > 0) {
ret = new URL[v.size()];
v.copyInto(ret);
}
} catch (IOException ioex) {
} catch (SecurityException ex) { }
return ret;
}
});
|
private java.lang.Object | getService(javax.mail.Provider provider, javax.mail.URLName url)Get a Service object. Needs a provider object, but will
create a URLName if needed. It attempts to instantiate
the correct class.
// need a provider and url
if (provider == null) {
throw new NoSuchProviderException("null");
}
// create a url if needed
if (url == null) {
url = new URLName(provider.getProtocol(), null, -1,
null, null, null);
}
Object service = null;
// get the ClassLoader associated with the Authenticator
ClassLoader cl;
if (authenticator != null)
cl = authenticator.getClass().getClassLoader();
else
cl = this.getClass().getClassLoader();
// now load the class
Class serviceClass = null;
try {
// First try the "application's" class loader.
ClassLoader ccl = getContextClassLoader();
if (ccl != null)
try {
serviceClass =
Class.forName(provider.getClassName(), false, ccl);
} catch (ClassNotFoundException ex) {
// ignore it
}
if (serviceClass == null)
serviceClass =
Class.forName(provider.getClassName(), false, cl);
} catch (Exception ex1) {
// That didn't work, now try the "system" class loader.
// (Need both of these because JDK 1.1 class loaders
// may not delegate to their parent class loader.)
try {
serviceClass = Class.forName(provider.getClassName());
} catch (Exception ex) {
// Nothing worked, give up.
if (debug) ex.printStackTrace(getDebugOut());
throw new NoSuchProviderException(provider.getProtocol());
}
}
// construct an instance of the class
try {
Class[] c = {javax.mail.Session.class, javax.mail.URLName.class};
Constructor cons = serviceClass.getConstructor(c);
Object[] o = {this, url};
service = cons.newInstance(o);
} catch (Exception ex) {
if (debug) ex.printStackTrace(getDebugOut());
throw new NoSuchProviderException(provider.getProtocol());
}
return service;
|
public javax.mail.Store | getStore()Get a Store object that implements this user's desired Store
protocol. The mail.store.protocol property specifies the
desired protocol. If an appropriate Store object is not obtained,
NoSuchProviderException is thrown
return getStore(getProperty("mail.store.protocol"));
|
public javax.mail.Store | getStore(java.lang.String protocol)Get a Store object that implements the specified protocol. If an
appropriate Store object cannot be obtained,
NoSuchProviderException is thrown.
return getStore(new URLName(protocol, null, -1, null, null, null));
|
public javax.mail.Store | getStore(javax.mail.URLName url)Get a Store object for the given URLName. If the requested Store
object cannot be obtained, NoSuchProviderException is thrown.
The "scheme" part of the URL string (Refer RFC 1738) is used
to locate the Store protocol.
String protocol = url.getProtocol();
Provider p = getProvider(protocol);
return getStore(p, url);
|
public javax.mail.Store | getStore(javax.mail.Provider provider)Get an instance of the store specified by Provider. Instantiates
the store and returns it.
return getStore(provider, null);
|
private javax.mail.Store | getStore(javax.mail.Provider provider, javax.mail.URLName url)Get an instance of the store specified by Provider. If the URLName
is not null, uses it, otherwise creates a new one. Instantiates
the store and returns it. This is a private method used by
getStore(Provider) and getStore(URLName)
// make sure we have the correct type of provider
if (provider == null || provider.getType() != Provider.Type.STORE ) {
throw new NoSuchProviderException("invalid provider");
}
try {
return (Store) getService(provider, url);
} catch (ClassCastException cce) {
throw new NoSuchProviderException("incorrect class");
}
|
private static java.net.URL[] | getSystemResources(java.lang.String name)
return (URL[])
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
URL[] ret = null;
try {
Vector v = new Vector();
Enumeration e = ClassLoader.getSystemResources(name);
while (e != null && e.hasMoreElements()) {
URL url = (URL)e.nextElement();
if (url != null)
v.addElement(url);
}
if (v.size() > 0) {
ret = new URL[v.size()];
v.copyInto(ret);
}
} catch (IOException ioex) {
} catch (SecurityException ex) { }
return ret;
}
});
|
public javax.mail.Transport | getTransport()Get a Transport object that implements this user's desired
Transport protcol. The mail.transport.protocol property
specifies the desired protocol. If an appropriate Transport
object cannot be obtained, MessagingException is thrown.
return getTransport(getProperty("mail.transport.protocol"));
|
public javax.mail.Transport | getTransport(java.lang.String protocol)Get a Transport object that implements the specified protocol.
If an appropriate Transport object cannot be obtained, null is
returned.
return getTransport(new URLName(protocol, null, -1, null, null, null));
|
public javax.mail.Transport | getTransport(javax.mail.URLName url)Get a Transport object for the given URLName. If the requested
Transport object cannot be obtained, NoSuchProviderException is thrown.
The "scheme" part of the URL string (Refer RFC 1738) is used
to locate the Transport protocol.
String protocol = url.getProtocol();
Provider p = getProvider(protocol);
return getTransport(p, url);
|
public javax.mail.Transport | getTransport(javax.mail.Provider provider)Get an instance of the transport specified in the Provider. Instantiates
the transport and returns it.
return getTransport(provider, null);
|
public javax.mail.Transport | getTransport(javax.mail.Address address)Get a Transport object that can transport a Message of the
specified address type.
String transportProtocol;
transportProtocol =
getProperty("mail.transport.protocol." + address.getType());
if (transportProtocol != null)
return getTransport(transportProtocol);
transportProtocol = (String)addressMap.get(address.getType());
if (transportProtocol != null)
return getTransport(transportProtocol);
throw new NoSuchProviderException("No provider for Address type: "+
address.getType());
|
private javax.mail.Transport | getTransport(javax.mail.Provider provider, javax.mail.URLName url)Get a Transport object using the given provider and urlname.
// make sure we have the correct type of provider
if (provider == null || provider.getType() != Provider.Type.TRANSPORT) {
throw new NoSuchProviderException("invalid provider");
}
try {
return (Transport) getService(provider, url);
} catch (ClassCastException cce) {
throw new NoSuchProviderException("incorrect class");
}
|
private void | loadAddressMap(java.lang.Class cl)
StreamLoader loader = new StreamLoader() {
public void load(InputStream is) throws IOException {
addressMap.load(is);
}
};
// load default META-INF/javamail.default.address.map from mail.jar
loadResource("/META-INF/javamail.default.address.map", cl, loader);
// load the META-INF/javamail.address.map file supplied by an app
loadAllResources("META-INF/javamail.address.map", cl, loader);
// load system-wide javamail.address.map from the <java.home>/lib dir
try {
String res = System.getProperty("java.home") +
File.separator + "lib" +
File.separator + "javamail.address.map";
loadFile(res, loader);
} catch (SecurityException sex) {
if (debug)
pr("DEBUG: can't get java.home: " + sex);
}
if (addressMap.isEmpty()) {
if (debug)
pr("DEBUG: failed to load address map, using defaults");
addressMap.put("rfc822", "smtp");
}
|
private void | loadAllResources(java.lang.String name, java.lang.Class cl, javax.mail.StreamLoader loader)Load all of the named resource.
boolean anyLoaded = false;
try {
URL[] urls;
ClassLoader cld = null;
// First try the "application's" class loader.
cld = getContextClassLoader();
if (cld == null)
cld = cl.getClassLoader();
if (cld != null)
urls = getResources(cld, name);
else
urls = getSystemResources(name);
if (urls != null) {
for (int i = 0; i < urls.length; i++) {
URL url = urls[i];
InputStream clis = null;
if (debug)
pr("DEBUG: URL " + url);
try {
clis = openStream(url);
if (clis != null) {
loader.load(clis);
anyLoaded = true;
if (debug)
pr("DEBUG: successfully loaded resource: " +
url);
} else {
if (debug)
pr("DEBUG: not loading resource: " + url);
}
} catch (FileNotFoundException fex) {
// ignore it
} catch (IOException ioex) {
if (debug)
pr("DEBUG: " + ioex);
} catch (SecurityException sex) {
if (debug)
pr("DEBUG: " + sex);
} finally {
try {
if (clis != null)
clis.close();
} catch (IOException cex) { }
}
}
}
} catch (Exception ex) {
if (debug)
pr("DEBUG: " + ex);
}
// if failed to load anything, fall back to old technique, just in case
if (!anyLoaded) {
/*
if (debug)
pr("DEBUG: !anyLoaded");
*/
loadResource("/" + name, cl, loader);
}
|
private void | loadFile(java.lang.String name, javax.mail.StreamLoader loader)Load from the named file.
InputStream clis = null;
try {
clis = new BufferedInputStream(new FileInputStream(name));
loader.load(clis);
if (debug)
pr("DEBUG: successfully loaded file: " + name);
} catch (FileNotFoundException fex) {
// ignore it
} catch (IOException e) {
if (debug) {
pr("DEBUG: not loading file: " + name);
pr("DEBUG: " + e);
}
} catch (SecurityException sex) {
if (debug) {
pr("DEBUG: not loading file: " + name);
pr("DEBUG: " + sex);
}
} finally {
try {
if (clis != null)
clis.close();
} catch (IOException ex) { } // ignore it
}
|
private void | loadProviders(java.lang.Class cl)Load the protocol providers config files.
StreamLoader loader = new StreamLoader() {
public void load(InputStream is) throws IOException {
loadProvidersFromStream(is);
}
};
// load system-wide javamail.providers from the <java.home>/lib dir
try {
String res = System.getProperty("java.home") +
File.separator + "lib" +
File.separator + "javamail.providers";
loadFile(res, loader);
} catch (SecurityException sex) {
if (debug)
pr("DEBUG: can't get java.home: " + sex);
}
// load the META-INF/javamail.providers file supplied by an application
loadAllResources("META-INF/javamail.providers", cl, loader);
// load default META-INF/javamail.default.providers from mail.jar file
loadResource("/META-INF/javamail.default.providers", cl, loader);
if (providers.size() == 0) {
if (debug)
pr("DEBUG: failed to load any providers, using defaults");
// failed to load any providers, initialize with our defaults
addProvider(new Provider(Provider.Type.STORE,
"imap", "com.sun.mail.imap.IMAPStore",
"Sun Microsystems, Inc.", Version.version));
addProvider(new Provider(Provider.Type.STORE,
"imaps", "com.sun.mail.imap.IMAPSSLStore",
"Sun Microsystems, Inc.", Version.version));
addProvider(new Provider(Provider.Type.STORE,
"pop3", "com.sun.mail.pop3.POP3Store",
"Sun Microsystems, Inc.", Version.version));
addProvider(new Provider(Provider.Type.STORE,
"pop3s", "com.sun.mail.pop3.POP3SSLStore",
"Sun Microsystems, Inc.", Version.version));
addProvider(new Provider(Provider.Type.TRANSPORT,
"smtp", "com.sun.mail.smtp.SMTPTransport",
"Sun Microsystems, Inc.", Version.version));
addProvider(new Provider(Provider.Type.TRANSPORT,
"smtps", "com.sun.mail.smtp.SMTPSSLTransport",
"Sun Microsystems, Inc.", Version.version));
}
if (debug) {
// dump the output of the tables for debugging
pr("DEBUG: Tables of loaded providers");
pr("DEBUG: Providers Listed By Class Name: " +
providersByClassName.toString());
pr("DEBUG: Providers Listed By Protocol: " +
providersByProtocol.toString());
}
|
private void | loadProvidersFromStream(java.io.InputStream is)
if (is != null) {
LineInputStream lis = new LineInputStream(is);
String currLine;
// load and process one line at a time using LineInputStream
while ((currLine = lis.readLine()) != null) {
if (currLine.startsWith("#"))
continue;
Provider.Type type = null;
String protocol = null, className = null;
String vendor = null, version = null;
// separate line into key-value tuples
StringTokenizer tuples = new StringTokenizer(currLine,";");
while (tuples.hasMoreTokens()) {
String currTuple = tuples.nextToken().trim();
// set the value of each attribute based on its key
int sep = currTuple.indexOf("=");
if (currTuple.startsWith("protocol=")) {
protocol = currTuple.substring(sep+1);
} else if (currTuple.startsWith("type=")) {
String strType = currTuple.substring(sep+1);
if (strType.equalsIgnoreCase("store")) {
type = Provider.Type.STORE;
} else if (strType.equalsIgnoreCase("transport")) {
type = Provider.Type.TRANSPORT;
}
} else if (currTuple.startsWith("class=")) {
className = currTuple.substring(sep+1);
} else if (currTuple.startsWith("vendor=")) {
vendor = currTuple.substring(sep+1);
} else if (currTuple.startsWith("version=")) {
version = currTuple.substring(sep+1);
}
}
// check if a valid Provider; else, continue
if (type == null || protocol == null || className == null
|| protocol.length() <= 0 || className.length() <= 0) {
if (debug)
pr("DEBUG: Bad provider entry: " + currLine);
continue;
}
Provider provider = new Provider(type, protocol, className,
vendor, version);
// add the newly-created Provider to the lookup tables
addProvider(provider);
}
}
|
private void | loadResource(java.lang.String name, java.lang.Class cl, javax.mail.StreamLoader loader)Load from the named resource.
InputStream clis = null;
try {
clis = getResourceAsStream(cl, name);
if (clis != null) {
loader.load(clis);
if (debug)
pr("DEBUG: successfully loaded resource: " + name);
} else {
/*
if (debug)
pr("DEBUG: not loading resource: " + name);
*/
}
} catch (IOException e) {
if (debug)
pr("DEBUG: " + e);
} catch (SecurityException sex) {
if (debug)
pr("DEBUG: " + sex);
} finally {
try {
if (clis != null)
clis.close();
} catch (IOException ex) { } // ignore it
}
|
private static java.io.InputStream | openStream(java.net.URL url)
try {
return (InputStream)
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws IOException {
return url.openStream();
}
});
} catch (PrivilegedActionException e) {
throw (IOException)e.getException();
}
|
private void | pr(java.lang.String str)
getDebugOut().println(str);
|
public javax.mail.PasswordAuthentication | requestPasswordAuthentication(java.net.InetAddress addr, int port, java.lang.String protocol, java.lang.String prompt, java.lang.String defaultUserName)Call back to the application to get the needed user name and password.
The application should put up a dialog something like:
Connecting to <protocol> mail service on host <addr>, port <port>.
<prompt>
User Name: <defaultUserName>
Password:
if (authenticator != null) {
return authenticator.requestPasswordAuthentication(
addr, port, protocol, prompt, defaultUserName);
} else {
return null;
}
|
public synchronized void | setDebug(boolean debug)Set the debug setting for this Session.
Since the debug setting can be turned on only after the Session
has been created, to turn on debugging in the Session
constructor, set the property mail.debug in the
Properties object passed in to the constructor to true. The
value of the mail.debug property is used to
initialize the per-Session debugging flag. Subsequent calls to
the setDebug method manipulate the per-Session
debugging flag and have no affect on the mail.debug
property.
this.debug = debug;
if (debug)
pr("DEBUG: setDebug: JavaMail version " + Version.version);
|
public synchronized void | setDebugOut(java.io.PrintStream out)Set the stream to be used for debugging output for this session.
If out is null, System.out will be used.
Note that debugging output that occurs before any session is created,
as a result of setting the mail.debug system property,
will always be sent to System.out .
this.out = out;
|
public void | setPasswordAuthentication(javax.mail.URLName url, javax.mail.PasswordAuthentication pw)Save a PasswordAuthentication for this (store or transport) URLName.
If pw is null the entry corresponding to the URLName is removed.
This is normally used only by the store or transport implementations
to allow authentication information to be shared among multiple
uses of a session.
if (pw == null)
authTable.remove(url);
else
authTable.put(url, pw);
|
public synchronized void | setProtocolForAddress(java.lang.String addresstype, java.lang.String protocol)Set the default transport protocol to use for addresses of
the specified type. Normally the default is set by the
javamail.default.address.map or
javamail.address.map files or resources.
if (protocol == null)
addressMap.remove(addresstype);
else
addressMap.put(addresstype, protocol);
|
public synchronized void | setProvider(javax.mail.Provider provider)Set the passed Provider to be the default implementation
for the protocol in Provider.protocol overriding any previous values.
if (provider == null) {
throw new NoSuchProviderException("Can't set null provider");
}
providersByProtocol.put(provider.getProtocol(), provider);
props.put("mail." + provider.getProtocol() + ".class",
provider.getClassName());
|