📄 mailuser.java
字号:
package ch08.section08;
import javax.mail.*;
import javax.servlet.http.*;
/**
* This class encapsulates a mail user and is used by the
* SimpleMailReader servlet.
*/
public class MailUser
implements HttpSessionBindingListener {
Session m_session;
boolean m_loggedIn;
Store m_store;
Folder m_inbox;
String m_storeProtocol;
String m_storeHost;
String m_transportProtocol;
String m_transportHost;
String m_user;
String m_fromName;
/**
* Creates a new SimpleMailUser
*/
public MailUser() {
// Create a default session
java.util.Properties props = System.getProperties();
m_session = Session.getDefaultInstance(props, null);
}
/**
* Get the current session
* @return The current session
*/
public Session getSession() {
return m_session;
}
/**
* Gets the current inbox
* @return The inbox
*/
public Folder getInbox() {
return m_inbox;
}
/**
* Returns true if the current user is logged in
* @return true if the current user is logged in
*/
public boolean isLoggedIn() {
return m_loggedIn;
}
/**
* Gets the store protocol
* @return The store protocol, such as POP3
*/
public String getStoreProtocol() {
return m_storeProtocol;
}
/**
* Gets the store host
* @return The store host name
*/
public String getStoreHost() {
return m_storeHost;
}
/**
* Gets the transport protocol
* @return The transport protocol, such as SMTP
*/
public String getTransportProtocol() {
return m_transportProtocol;
}
/**
* Gets the transport host
* @return The transport host name
*/
public String getTransportHost() {
return m_transportHost;
}
/**
* Gets the current user name
* @return The current user
*/
public String getUser() {
return m_user;
}
/**
* Sets the last from address entered by the user
* @param name The from address name
*/
public void setFromName(String name) {
m_fromName = name;
}
/**
* Gets the last from address entered by the user
* @return The from address name
*/
public String getFromName() {
return m_fromName;
}
/**
* Returns the names of the currently available transport
* protocols
* @return An array of transport names
*/
public String[] getTransportNames() {
return getProtocolNames(Provider.Type.TRANSPORT);
}
/**
* Returns the names of the currently available store
* protocols
* @return An array of store names
*/
public String[] getStoreNames() {
return getProtocolNames(Provider.Type.STORE);
}
/**
* Logs on to the specified Mail store
* @param store The store protocol
* @param storeHost The store host name
* @param transport The transport protocol
* @param transportHost The transport host name
* @param user The user name
* @param password The user password
*/
public void login(String store, String storeHost,
String transport, String transportHost,
String user, String password) throws MessagingException {
try {
// Create a new session
close();
java.util.Properties props = System.getProperties();
props.put("mail.host", storeHost);
m_session = Session.getDefaultInstance(props, null);
// Create a new URLName
URLName url = new URLName(store, storeHost, -1,
"INBOX", user, password);
m_store = m_session.getStore(url);
m_store.connect();
m_inbox = m_store.getFolder("INBOX");
m_inbox.open(Folder.READ_WRITE);
// Set properties
m_storeProtocol = store;
m_storeHost = storeHost;
m_transportProtocol = transport;
m_transportHost = transportHost;
m_user = user;
m_loggedIn = true;
}
finally {
// If there is some failure make sure that everything
// is closed down properly
if (!m_loggedIn) {
if (m_store != null) {
m_store.close();
m_store = null;
}
}
}
}
/**
* Returns the name of the given type of protocol
* @return An array of names
*/
protected String[] getProtocolNames(Provider.Type type) {
// Use a vector to temporarily store the provider objects
java.util.Vector v = new java.util.Vector();
// Get a list of all of the current providers
Provider p[] = m_session.getProviders();
if (p != null) {
for (int i = 0; i < p.length; i++) {
// Only get the types requested
if (p[i].getType() == type) {
// Store it in our vector
v.addElement(p[i]);
}
}
}
// Now create an array of names from the providers that
// we put in our vector
String s[] = new String[v.size()];
for (int i = 0; i < v.size(); i++) {
Provider prov = (Provider) v.elementAt(i);
s[i] = prov.getProtocol();
}
return s;
}
/**
* Called when this object is bound into a session.
* @param event The event
*/
public void valueBound(HttpSessionBindingEvent event) {
// Nothing to do when the value is bound
}
/**
* Called when this object is unbound from a session
* @param event The event
*/
public void valueUnbound(HttpSessionBindingEvent event) {
// Close the store if necessary
close();
}
/**
* Close all connections
*/
public void close() {
m_loggedIn = false;
// Close the inbox and store if necessary
try {
if (m_inbox != null) {
// Close without expunging deleted messages
m_inbox.close(true);
}
if (m_store != null) {
m_store.close();
}
}
catch (MessagingException ex) {
// Ignore errors on close
}
m_inbox = null;
m_store = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -