use of javax.mail.Store in project mailim by zengsn.
the class EmailUtil method checkEmail.
public static boolean checkEmail(String email, String pwd) {
String host = getDefaultAddr(email);
String user = getUsername(email);
Store store = login(host, user, pwd);
if (store == null)
return false;
else
return true;
}
use of javax.mail.Store in project common by zenlunatics.
the class MailLists method fetchAndSend.
// --------------------------------------------------------------------------
@AdminTask({ "list", "account name", "host", "password" })
public synchronized void fetchAndSend(String list, String account_name, String host, String password, DBConnection db) throws MessagingException {
Session session = Session.getInstance(new Properties(), null);
Folder folder = null;
Store store = null;
try {
store = session.getStore(m_store_protocol);
try {
store.connect(host, account_name, password);
} catch (AuthenticationFailedException e) {
if (e.toString().indexOf("temporarily unavailable") == -1)
try {
TimeUnit.SECONDS.sleep(15);
store.connect(host, account_name, password);
} catch (AuthenticationFailedException e1) {
if (e1.toString().indexOf("temporarily unavailable") == -1)
sendError(list, "fetchAndSend", "store.connect", null, e1);
return;
} catch (InterruptedException e1) {
sendError(list, "fetchAndSend", "store.connect", null, e1);
return;
}
else
return;
} catch (MailConnectException e) {
m_site.log(e);
if (host.equals("172.21.0.1") || host.equals("sonoracohousing.com"))
return;
}
if (store.isConnected()) {
folder = store.getFolder("INBOX");
try {
folder.open(Folder.READ_WRITE);
} catch (MessagingException e) {
if (e.toString().indexOf("* BYE") != -1)
return;
throw e;
}
Message[] messages = folder.getMessages();
long now = new Date().getTime();
for (Message message : messages) {
if (message.getHeader("X-ignore") != null || message.getHeader("auto-submitted") != null || message.getHeader("Auto-Submitted") != null || message.getHeader("X-Autoreply") != null || message.getHeader("X-Autorespond") != null) {
message.setFlag(Flags.Flag.DELETED, true);
continue;
}
Date received_date = message.getReceivedDate();
long minutes = (now - received_date.getTime()) / 60000;
if (minutes > 30)
sendError(list, "fetchAndSend", "message " + minutes + " minutes old", null, null);
Address[] recipients = message.getRecipients(RecipientType.TO);
if (recipients != null) {
String to = recipients[0].toString();
if (to == null)
System.out.println("to is null");
else if (to.indexOf('+') != -1)
message.setFlag(Flags.Flag.DELETED, true);
}
if (handleMessage(list, message, true, db))
message.setFlag(Flags.Flag.DELETED, true);
}
}
} catch (MessagingException e) {
if (e.toString().indexOf("Connection dropped by server?") == -1 && e.toString().indexOf("BYE JavaMail") == -1)
m_site.log(e);
// sendError(list, "fetchAndSend", null, e);
} finally {
try {
if (folder != null && folder.isOpen())
folder.close(true);
} catch (Exception e) {
sendError(list, "fetchAndSend", "closing folder", null, e);
}
store.close();
}
}
use of javax.mail.Store in project Openfire by igniterealtime.
the class POP3AuthProvider method authenticate.
@Override
public void authenticate(String username, String password) throws UnauthorizedException {
if (username == null || password == null) {
throw new UnauthorizedException();
}
if (username.contains("@")) {
// Check that the specified domain matches the server's domain
int index = username.indexOf("@");
String domain = username.substring(index + 1);
if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
username = username.substring(0, index);
}
} else {
// Unknown domain. Return authentication failed.
throw new UnauthorizedException();
}
Log.debug("POP3AuthProvider.authenticate(" + username + ", ******)");
// If cache is enabled, see if the auth is in cache.
if (authCache != null && authCache.containsKey(username)) {
String hash = authCache.get(username);
if (StringUtils.hash(password).equals(hash)) {
return;
}
}
Properties mailProps = new Properties();
mailProps.setProperty("mail.debug", String.valueOf(debugEnabled));
Session session = Session.getInstance(mailProps, null);
Store store;
try {
store = session.getStore(useSSL ? "pop3s" : "pop3");
} catch (NoSuchProviderException e) {
Log.error(e.getMessage(), e);
throw new UnauthorizedException(e);
}
try {
if (authRequiresDomain) {
store.connect(host, port, username + "@" + domain, password);
} else {
store.connect(host, port, username, password);
}
} catch (Exception e) {
Log.error(e.getMessage(), e);
throw new UnauthorizedException(e);
}
if (!store.isConnected()) {
throw new UnauthorizedException("Could not authenticate user");
}
try {
store.close();
} catch (Exception e) {
// Ignore.
}
// If cache is enabled, add the item to cache.
if (authCache != null) {
authCache.put(username, StringUtils.hash(password));
}
// See if the user exists in the database. If not, automatically create them.
UserManager userManager = UserManager.getInstance();
try {
userManager.getUser(username);
} catch (UserNotFoundException unfe) {
String email = username + "@" + (domain != null ? domain : host);
try {
Log.debug("POP3AuthProvider: Automatically creating new user account for " + username);
// Create user; use a random password for better safety in the future.
// Note that we have to go to the user provider directly -- because the
// provider is read-only, UserManager will usually deny access to createUser.
UserManager.getUserProvider().createUser(username, StringUtils.randomString(8), null, email);
} catch (UserAlreadyExistsException uaee) {
// Ignore.
}
}
}
use of javax.mail.Store in project ats-framework by Axway.
the class ImapStorage method getFolder.
public ImapFolder getFolder(SearchTerm searchTerm) throws RbvStorageException {
try {
if (searchTerm.getClass().getName().equals(ImapFolderSearchTerm.class.getName())) {
ImapFolderSearchTerm imapSearchTerm = (ImapFolderSearchTerm) searchTerm;
Store store = session.getStore("imap");
return new ImapFolder(store, imapServerHost, imapSearchTerm.getFolderName(), imapSearchTerm.getUserName(), imapSearchTerm.getPassword());
} else if (searchTerm.getClass().getName().equals(ImapEncryptedFolderSearchTerm.class.getName())) {
ImapEncryptedFolderSearchTerm imapSearchTerm = (ImapEncryptedFolderSearchTerm) searchTerm;
Store store = session.getStore("imap");
return new ImapEncryptedFolder(store, imapServerHost, imapSearchTerm.getFolderName(), imapSearchTerm.getUserName(), imapSearchTerm.getPassword(), imapSearchTerm.getEncryptor());
} else {
throw new RbvStorageException("Search term " + searchTerm.getClass().getSimpleName() + " is not supported");
}
} catch (NoSuchProviderException e) {
throw new RuntimeException("Unable to get IMAP store for host " + imapServerHost, e);
}
}
use of javax.mail.Store in project ats-framework by Axway.
the class MimePackage method closeStoreConnection.
/**
* 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) {
// in case of nested package but still originating from IMAP server
imapFolder = partOfImapFolder;
}
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();
}
}
}
}
Aggregations