use of javax.mail.NoSuchProviderException 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.NoSuchProviderException in project jodd by oblac.
the class SmtpServer method createSession.
/**
* {@inheritDoc}
*/
public SendMailSession createSession() {
Properties sessionProperties = createSessionProperties();
if (additionalProperties != null) {
sessionProperties.putAll(additionalProperties);
}
Session mailSession = Session.getInstance(sessionProperties, authenticator);
Transport mailTransport;
try {
mailTransport = getTransport(mailSession);
} catch (NoSuchProviderException nspex) {
throw new MailException(nspex);
}
return new SendMailSession(mailSession, mailTransport);
}
use of javax.mail.NoSuchProviderException in project jodd by oblac.
the class ImapServer method createSession.
/**
* {@inheritDoc}
*/
public ReceiveMailSession createSession() {
Session session = Session.getInstance(sessionProperties, authenticator);
Store store;
try {
store = getStore(session);
} catch (NoSuchProviderException nspex) {
throw new MailException("Failed to create IMAP session", nspex);
}
return new ReceiveMailSession(session, store);
}
use of javax.mail.NoSuchProviderException 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 {
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.NoSuchProviderException in project opennms by OpenNMS.
the class JavaMailer method sendMessage.
/**
* Send message.
*
* @param message a {@link javax.mail.Message} object.
* @throws org.opennms.javamail.JavaMailerException if any.
*/
public void sendMessage(Message message) throws JavaMailerException {
Transport t = null;
try {
t = getSession().getTransport(getTransport());
LOG.debug("for transport name '{}' got: {}@{}", getTransport(), t.getClass().getName(), Integer.toHexString(t.hashCode()));
LoggingTransportListener listener = new LoggingTransportListener();
t.addTransportListener(listener);
if (t.getURLName().getProtocol().equals("mta")) {
// JMTA throws an AuthenticationFailedException if we call connect()
LOG.debug("transport is 'mta', not trying to connect()");
} else if (isAuthenticate()) {
LOG.debug("authenticating to {}", getMailHost());
t.connect(getMailHost(), getSmtpPort(), getUser(), getPassword());
} else {
LOG.debug("not authenticating to {}", getMailHost());
t.connect(getMailHost(), getSmtpPort(), null, null);
}
t.sendMessage(message, message.getAllRecipients());
listener.assertAllMessagesDelivered();
} catch (NoSuchProviderException e) {
LOG.error("Couldn't get a transport: {}", e, e);
throw new JavaMailerException("Couldn't get a transport: " + e, e);
} catch (MessagingException e) {
LOG.error("Java Mailer messaging exception: {}", e, e);
throw new JavaMailerException("Java Mailer messaging exception: " + e, e);
} finally {
try {
if (t != null && t.isConnected()) {
t.close();
}
} catch (MessagingException e) {
throw new JavaMailerException("Java Mailer messaging exception on transport close: " + e, e);
}
}
}
Aggregations