📄 emaillistener.java
字号:
int c;
while ((c = is.read()) != -1) {
System.out.write(c);
}
}
else {
// This is an unknown type
System.out.println(o.toString());
}*/
}
}
private static Folder openFolder(String host, Integer port, Boolean isSSLEnabled, String user, String password,
String folder) {
if (host == null || port == null || isSSLEnabled == null || user == null || password == null || folder == null) {
return null;
}
try {
Properties props = System.getProperties();
props.setProperty("mail.imap.host", host);
props.setProperty("mail.imap.port", String.valueOf(port));
props.setProperty("mail.imap.connectiontimeout", String.valueOf(10 * 1000));
// Allow messages with a mix of valid and invalid recipients to still be sent.
props.setProperty("mail.debug", JiveGlobals.getProperty("plugin.email.listener.debug", "false"));
// Methology from an article on www.javaworld.com (Java Tip 115)
// We will attempt to failback to an insecure connection
// if the secure one cannot be made
if (isSSLEnabled) {
// Register with security provider.
Security.setProperty("ssl.SocketFactory.provider", SSL_FACTORY);
//props.setProperty("mail.imap.starttls.enable", "true");
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imap.socketFactory.fallback", "true");
}
// Get a Session object
Session session = Session.getInstance(props, null);
// Get a Store object
Store store = session.getStore(isSSLEnabled ? "imaps" : "imap");
// Connect
store.connect(host, user, password);
// Open a Folder
Folder newFolder = store.getFolder(folder);
if (newFolder == null || !newFolder.exists()) {
Log.error("Invalid email folder: " + folder);
return null;
}
newFolder.open(Folder.READ_WRITE);
return newFolder;
}
catch (Exception e) {
Log.error("Error while initializing email listener", e);
}
return null;
}
private static void closeFolder(Folder folder, MessageCountAdapter messageListener) {
if (folder != null) {
if (messageListener != null) {
folder.removeMessageCountListener(messageListener);
}
try {
folder.close(false);
}
catch (MessagingException e) {
Log.error("Error closing folder", e);
}
}
}
/**
* Returns the host where the IMAP server is running or <tt>null</tt> if none was defined.
*
* @return the host where the IMAP server is running or null if none was defined.
*/
public String getHost() {
return JiveGlobals.getProperty("plugin.email.listener.host");
}
/**
* Sets the host where the IMAP server is running or <tt>null</tt> if none was defined.
*
* @param host the host where the IMAP server is running or null if none was defined.
*/
public void setHost(String host) {
JiveGlobals.setProperty("plugin.email.listener.host", host);
}
/**
* Returns the port where the IMAP server is listening. By default unsecured connections
* use port 143 and secured ones use 993.
*
* @return port where the IMAP server is listening.
*/
public int getPort() {
return JiveGlobals.getIntProperty("plugin.email.listener.port", isSSLEnabled() ? 993 : 143);
}
/**
* Sets the port where the IMAP server is listening. By default unsecured connections
* use port 143 and secured ones use 993.
*
* @param port port where the IMAP server is listening.
*/
public void setPort(int port) {
JiveGlobals.setProperty("plugin.email.listener.port", Integer.toString(port));
}
/**
* Returns the user to use to connect to the IMAP server. A null value means that
* this property needs to be configured to be used.
*
* @return the user to use to connect to the IMAP server.
*/
public String getUser() {
return JiveGlobals.getProperty("plugin.email.listener.user");
}
/**
* Sets the user to use to connect to the IMAP server. A null value means that
* this property needs to be configured to be used.
*
* @param user the user to use to connect to the IMAP server.
*/
public void setUser(String user) {
JiveGlobals.setProperty("plugin.email.listener.user", user);
}
/**
* Returns the password to use to connect to the IMAP server. A null value means that
* this property needs to be configured to be used.
*
* @return the password to use to connect to the IMAP server.
*/
public String getPassword() {
return JiveGlobals.getProperty("plugin.email.listener.password");
}
/**
* Sets the password to use to connect to the IMAP server. A null value means that
* this property needs to be configured to be used.
*
* @param password the password to use to connect to the IMAP server.
*/
public void setPassword(String password) {
JiveGlobals.setProperty("plugin.email.listener.password", password);
}
/**
* Returns the name of the folder. In some Stores, name can be an absolute path if
* it starts with the hierarchy delimiter. Else it is interpreted relative to the
* 'root' of this namespace.
*
* @return the name of the folder.
*/
public String getFolder() {
return JiveGlobals.getProperty("plugin.email.listener.folder");
}
/**
* Sets the name of the folder. In some Stores, name can be an absolute path if
* it starts with the hierarchy delimiter. Else it is interpreted relative to the
* 'root' of this namespace.
*
* @param folder the name of the folder.
*/
public void setFolder(String folder) {
JiveGlobals.setProperty("plugin.email.listener.folder", folder);
}
/**
* Returns the milliseconds to wait to check for new emails. This frequency
* is used if the IMAP server does not support idle.
*
* @return the milliseconds to wait to check for new emails.
*/
public int getFrequency() {
return JiveGlobals.getIntProperty("plugin.email.listener.frequency", 5 * 60 * 1000);
}
/**
* Sets the milliseconds to wait to check for new emails. This frequency
* is used if the IMAP server does not support idle.
*
* @param frequency the milliseconds to wait to check for new emails.
*/
public void setFrequency(int frequency) {
JiveGlobals.setProperty("plugin.email.listener.frequency", Integer.toString(frequency));
}
/**
* Returns true if SSL is enabled to connect to the server.
*
* @return true if SSL is enabled to connect to the server.
*/
public boolean isSSLEnabled() {
return JiveGlobals.getBooleanProperty("plugin.email.listener.ssl", false);
}
/**
* Sets if SSL is enabled to connect to the server.
*
* @param enabled true if SSL is enabled to connect to the server.
*/
public void setSSLEnabled(boolean enabled) {
JiveGlobals.setProperty("plugin.email.listener.ssl", Boolean.toString(enabled));
}
public Collection<String> getUsers() {
String users = JiveGlobals.getProperty("plugin.email.listener.users");
if (users == null || users.trim().length() == 0) {
Collection<String> admins = new ArrayList<String>();
for (JID jid : XMPPServer.getInstance().getAdmins()) {
admins.add(jid.toString());
}
return admins;
}
return StringUtils.stringToCollection(users);
}
public void setUsers(Collection<String> users) {
JiveGlobals.setProperty("plugin.email.listener.users", StringUtils.collectionToString(users));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -