本文整理汇总了Java中javax.mail.Store.isConnected方法的典型用法代码示例。如果您正苦于以下问题:Java Store.isConnected方法的具体用法?Java Store.isConnected怎么用?Java Store.isConnected使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.Store
的用法示例。
在下文中一共展示了Store.isConnected方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: closeStoreConnection
import javax.mail.Store; //导入方法依赖的package包/类
/**
* Close connection
* <b>Note</b>Internal method
* @throws MessagingException
*/
public void closeStoreConnection(
boolean storeConnected ) throws MessagingException {
if (storeConnected) {
// the folder is empty when the message is not loaded from IMAP server, but from a file
Folder imapFolder = message.getFolder();
if (imapFolder == null) {
imapFolder = partOfImapFolder; // in case of nested package but still originating from IMAP server
}
if (imapFolder != null) {
Store store = imapFolder.getStore();
if (store != null && store.isConnected()) {
// closing store closes and its folders
log.debug("Closing store (" + store.toString() + ") and associated folders");
store.close();
}
}
}
}
示例2: reconnectStoreIfClosed
import javax.mail.Store; //导入方法依赖的package包/类
/**
* Reconnects if connection is closed.
* <b>Note</b>Internal method
* @return true if store re-connection is performed and this means that close should be closed after the work is done
* @throws MessagingException
*/
public boolean reconnectStoreIfClosed() throws MessagingException {
boolean storeReconnected = false;
// the folder is empty when the message is not loaded from IMAP server, but from a file
Folder imapFolder = message.getFolder();
if (imapFolder == null) {
imapFolder = this.partOfImapFolder;
} else {
partOfImapFolder = imapFolder; // keep reference
}
if (imapFolder != null) {
Store store = imapFolder.getStore();
if (store != null) {
if (!store.isConnected()) {
log.debug("Reconnecting store... ");
store.connect();
storeReconnected = true;
}
// Open folder in read-only mode
if (!imapFolder.isOpen()) {
log.debug("Reopening folder " + imapFolder.getFullName()
+ " in order to get contents of mail message");
imapFolder.open(Folder.READ_ONLY);
}
}
}
return storeReconnected;
}
示例3: getStore
import javax.mail.Store; //导入方法依赖的package包/类
private Store getStore(EmailAccount account) throws MessagingException {
Store store = null;
if (storeConnections == null) {
// Log.d("rgai", "CREATING STORE CONTAINER");
storeConnections = new HashMap<EmailAccount, Store>();
} else {
if (storeConnections.containsKey(account)) {
store = storeConnections.get(account);
// Log.d("rgai", "STORE EXISTS");
}
}
if (store == null || !store.isConnected()) {
// Log.d("rgai", "CREATING STORE || reconnection store");
store = getStore();
storeConnections.put(account, store);
}
return store;
}
示例4: closeStore
import javax.mail.Store; //导入方法依赖的package包/类
/**
* Handy method to do the try catch try of closing a mail store and folder.
* @param mailStore
* @param mailFolder
*/
private void closeStore(final Store mailStore, final Folder mailFolder) {
try {
if (mailFolder != null && mailFolder.isOpen()) {
mailFolder.close(true);
}
} catch (final MessagingException e) {
LogUtils.debugf(this, e, "Unable to close mail folder.");
} finally {
try {
if (mailStore != null && mailStore.isConnected()) {
mailStore.close();
}
} catch (final MessagingException e1) {
LogUtils.debugf(this, e1, "Unable to close message store.");
}
}
}
示例5: ensureConnectedStore
import javax.mail.Store; //导入方法依赖的package包/类
private void ensureConnectedStore(Store store) throws MessagingException {
if (!store.isConnected()) {
LOGGER.debug("connect to sore");
store.connect(configuration.getUserName(), configuration.getPassword());
}
}
示例6: getEmails
import javax.mail.Store; //导入方法依赖的package包/类
@Override
public List<SimpleMailMessage> getEmails()
throws MessagingException, IOException, NamingException {
Session emailSession = null;
Store store = null;
Folder emailFolder = null;
List<SimpleMailMessage> receivedEmails = new ArrayList<SimpleMailMessage>();
try {
if (!StringUtils.isEmpty(mailSettings.getJndiName())) {
InitialContext ic = new InitialContext();
emailSession = (Session) ic.lookup(mailSettings.getJndiName());
store = emailSession.getStore();
store.connect();
}
else {
// Set connection properties
Properties properties = new Properties();
String prefix = "mail.".concat(mailSettings.getProtocol());
properties.put(String.format("%s.host", prefix), mailSettings.getHost());
properties.put(String.format("%s.port", prefix), mailSettings.getPort());
properties.put("mail.store.protocol", mailSettings.getProtocol());
properties.put(String.format("%s.starttls.enable", prefix), mailSettings.getStarttlsEnabled());
// Create the session and the object Store to get the emails
emailSession = Session.getDefaultInstance(properties);
store = emailSession.getStore();
store.connect(mailSettings.getUsername(), mailSettings.getPassword());
}
// Get the folder that contains the emails and open it
emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
// Get emails that haven't been read
Message[] messagesReceived = emailFolder.getMessages();
for (Message message : messagesReceived) {
Object content = message.getContent();
String body = null;
if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
BodyPart bodyPart = multipart.getBodyPart(0);
if (bodyPart != null) {
body = bodyPart.getContent().toString();
}
}
else if (content instanceof String) {
body = (String) content;
}
SimpleMailMessage email = new SimpleMailMessage();
email.setSubject(message.getSubject());
email.setText(body);
email.setFrom(message.getFrom()[0].toString());
email.setSentDate(message.getSentDate());
receivedEmails.add(email);
}
}
finally {
// Close objects Folder and Store
if (emailFolder != null && emailFolder.isOpen()) {
emailFolder.close(false);
}
if (store != null && store.isConnected()) {
store.close();
}
}
return receivedEmails;
}
示例7: testEmailConfiguration
import javax.mail.Store; //导入方法依赖的package包/类
/**
* API to test the email configuration.
*
* @param emailConfigData {@link EmailConfigurationData}
* @return true or false based on the connection made successfully with the email setting passed
* @throws MessagingException {@link MessagingException}
* @throws AuthenticationFailedException {@link AuthenticationFailedException}
*/
public static boolean testEmailConfiguration(final EmailConfigurationData emailConfigData) throws MessagingException,
AuthenticationFailedException {
LOGGER.info("Enter method testEmailConfiguration.");
boolean isBValidConfig = true;
Store store = null;
if (emailConfigData != null) {
String serverType = emailConfigData.getServerType();
LOGGER.debug("Server type :: " + serverType);
if (emailConfigData.getIsSSL()) {
if (IUtilCommonConstants.POP3_CONFIG.equalsIgnoreCase(serverType)) {
store = connectWithPOP3SSL(emailConfigData);
} else if (IUtilCommonConstants.IMAP_CONFIG.equalsIgnoreCase(serverType)) {
store = connectWithIMAPSSL(emailConfigData);
} else {
LOGGER.error("Error in Server Type Configuration, only imap/pop3 is allowed.");
throw new MessagingException("Error in Server Type Configuration, only imap/pop3 is allowed.");
}
} else {
store = connectWithNonSSL(emailConfigData);
}
}
if (store == null || !store.isConnected()) {
LOGGER.error("Not able to establish connection. Please check settings.");
isBValidConfig = false;
}
LOGGER.info("Exiting method testEmailConfiguration.");
return isBValidConfig;
}
示例8: connect
import javax.mail.Store; //导入方法依赖的package包/类
public synchronized static boolean connect (Store _store,
String _protocol,
String _host,
int _port,
String _user, String _password) {
ConnectionPoint cp = getConnPoint(_store,
_protocol,
_host,
_port,
_user, _password);
boolean connected = false;
try {
Store store = cp.getStore();
if (!store.isConnected()) {
cp.getStore().connect(_host, _port, _user, _password );
}
++cp.connectionCount;
LOGGER.debug("Connecting store: connectionCount = " + cp.connectionCount);
connected = true;
}
catch (Exception e) {
connected = false;
if (LogThrottle.isReady("Syncstore1",240)) {
LOGGER.warn("Could not connect..." + e.getMessage());
}
LOGGER.debug(ExceptionUtils.getStackTrace(e));
}
return connected;
}
示例9: checkMessages
import javax.mail.Store; //导入方法依赖的package包/类
protected void checkMessages(Store store, Session session) throws MessagingException {
if (!store.isConnected()) {
store.connect();
}
// open the default folder
Folder folder = store.getDefaultFolder();
if (!folder.exists()) {
throw new MessagingException("No default (root) folder available");
}
// open the inbox
folder = folder.getFolder(INBOX);
if (!folder.exists()) {
throw new MessagingException("No INBOX folder available");
}
// get the message count; stop if nothing to do
folder.open(Folder.READ_WRITE);
int totalMessages = folder.getMessageCount();
if (totalMessages == 0) {
folder.close(false);
return;
}
// get all messages
Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
profile.add(FetchProfile.Item.FLAGS);
profile.add("X-Mailer");
folder.fetch(messages, profile);
// process each message
for (Message message: messages) {
// process each un-read message
if (!message.isSet(Flags.Flag.SEEN)) {
long messageSize = message.getSize();
if (message instanceof MimeMessage && messageSize >= maxSize) {
Debug.logWarning("Message from: " + message.getFrom()[0] + "not received, too big, size:" + messageSize + " cannot be more than " + maxSize + " bytes", module);
// set the message as read so it doesn't continue to try to process; but don't delete it
message.setFlag(Flags.Flag.SEEN, true);
} else {
this.processMessage(message, session);
if (Debug.verboseOn()) Debug.logVerbose("Message from " + UtilMisc.toListArray(message.getFrom()) + " with subject [" + message.getSubject() + "] has been processed." , module);
message.setFlag(Flags.Flag.SEEN, true);
if (Debug.verboseOn()) Debug.logVerbose("Message [" + message.getSubject() + "] is marked seen", module);
// delete the message after processing
if (deleteMail) {
if (Debug.verboseOn()) Debug.logVerbose("Message [" + message.getSubject() + "] is being deleted", module);
message.setFlag(Flags.Flag.DELETED, true);
}
}
}
}
// expunge and close the folder
folder.close(true);
}
示例10: getFlagChangesOfMessages
import javax.mail.Store; //导入方法依赖的package包/类
private MessageListResult getFlagChangesOfMessages(TreeSet<MessageListElement> loadedMessages) throws MessagingException {
List<MessageListElement> emails = new LinkedList<MessageListElement>();
Store store = getStore(account);
if (store == null || !store.isConnected()) {
return new MessageListResult(emails, MessageListResult.ResultType.ERROR);
}
IMAPFolder imapFolder = (IMAPFolder)store.getFolder(accountFolder.folder);
imapFolder.open(Folder.READ_ONLY);
UIDFolder uidFolder = imapFolder;
long smallestUID = getSmallestUID(loadedMessages);
Message messages[] = imapFolder.getMessagesByUID(smallestUID, UIDFolder.LASTUID);
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message unseenMsgs[] = imapFolder.search(ft, messages);
addMessagesToListAs(uidFolder, emails, unseenMsgs, loadedMessages, false);
// searching for seen messages
List<Message> seenMessages = new ArrayList<Message>(Arrays.asList(messages));
for (int k = 0; k < unseenMsgs.length; k++) {
String unseenUid = Long.toString(uidFolder.getUID(unseenMsgs[k]));
for (int l = 0; l < seenMessages.size(); ) {
String seenUid = Long.toString(uidFolder.getUID(seenMessages.get(l)));
if (seenUid.equals(unseenUid)) {
seenMessages.remove(l);
} else {
l++;
}
}
}
addMessagesToListAs(uidFolder, emails, seenMessages.toArray(new Message[seenMessages.size()]), loadedMessages, true);
imapFolder.close(false);
return new MessageListResult(emails, MessageListResult.ResultType.FLAG_CHANGE);
}
示例11: close
import javax.mail.Store; //导入方法依赖的package包/类
public static void close(final Store store) {
try {
if (store != null && store.isConnected()) {
store.close();
}
} catch (final Exception e) {
// ignore
}
}
示例12: checkStoreForTestConnection
import javax.mail.Store; //导入方法依赖的package包/类
protected void checkStoreForTestConnection(final Store store) {
if (!store.isConnected()) {
IMAPUtils.close(store);
throw new RuntimeException("Store not connected");
}
if (!store.getURLName().getUsername().toLowerCase().startsWith("es_imapriver_unittest")) {
IMAPUtils.close(store);
throw new RuntimeException("User " + store.getURLName().getUsername() + " belongs not to a valid test mail connection");
}
}
示例13: getMessagesFromGreenMailFolder
import javax.mail.Store; //导入方法依赖的package包/类
/**
* Method implementing to get the messages in the given folder.
*
* @param folderName Name of the folder to get mails.
* @return Array of messages which contains inside the given folder.
* @throws MessagingException MessagingException when fail to read the message from given folder.
*/
private Message[] getMessagesFromGreenMailFolder(String folderName) throws MessagingException {
Properties properties = new Properties();
properties.put(EmailTestConstant.MAIL_IMAP_PORT, EmailTestConstant.MAIL_IMAP_PORT_VALUE);
Session session = Session.getInstance(properties);
Store store = session.getStore(STORE_TYPE);
store.connect(LOCALHOST, USER_NAME, PASSWORD);
Folder folder = store.getFolder(folderName);
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
if (store.isConnected()) {
store.close();
}
return messages;
}
示例14: getTestMessages
import javax.mail.Store; //导入方法依赖的package包/类
private List<Message> getTestMessages(final String account, final String password) throws Exception {
final Properties props = new Properties();
// if (protocol.endsWith("s")) {
// props.put("mail.pop3.starttls.enable", Boolean.TRUE);
// props.put("mail.imap.starttls.enable", Boolean.TRUE);
// }
props.setProperty("mail.imaps.socketFactory.class", DummySSLSocketFactory.class.getName());
props.setProperty("mail.pop3s.socketFactory.class", DummySSLSocketFactory.class.getName());
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.imaps.socketFactory.fallback", "false");
props.setProperty("mail.pop3s.socketFactory.fallback", "false");
final String timeout = "15000";
props.setProperty("mail.imap.connectiontimeout", timeout);
props.setProperty("mail.imaps.connectiontimeout", timeout);
props.setProperty("mail.pop3.connectiontimeout", timeout);
props.setProperty("mail.pop3s.connectiontimeout", timeout);
props.setProperty("mail.imap.timeout", timeout);
props.setProperty("mail.imaps.timeout", timeout);
props.setProperty("mail.pop3.timeout", timeout);
props.setProperty("mail.pop3s.timeout", timeout);
Store store = null;
try {
final Session session = Session.getInstance(props, null);
session.setDebug(verbose);
store = session.getStore(ServerSetup.PROTOCOL_IMAP);
store.connect(imapSetup.getBindAddress(), imapSetup.getPort(), account, password);
final Folder rootFolder = store.getFolder("INBOX");
final List<Message> t = getMessages(rootFolder);
final List<Message> clonedMsgs = new ArrayList<Message>();
for (Message msg : t) {
MimeMessage clonedMsg = copy(session, msg);
clonedMsgs.add(clonedMsg);
}
return clonedMsgs;
} finally {
if (store != null && store.isConnected()) {
store.close();
}
}
}