use of org.opennms.netmgt.config.javamail.UserAuth in project opennms by OpenNMS.
the class JavaReadMailer method retrieveMessages.
/**
* <p>retrieveMessages</p>
*
* @return a {@link java.util.List} object.
* @throws org.opennms.javamail.JavaMailerException if any.
*/
public List<Message> retrieveMessages() throws JavaMailerException {
Message[] msgs;
Folder mailFolder = null;
final ReadmailHost readmailHost = getReadmailHost(m_config);
final UserAuth userAuth = getUserAuth(m_config);
try {
m_store = m_session.getStore(readmailHost.getReadmailProtocol().getTransport());
m_store.connect(readmailHost.getHost(), (int) readmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
mailFolder = m_store.getFolder(m_config.getMailFolder());
mailFolder.open(Folder.READ_WRITE);
msgs = mailFolder.getMessages();
} catch (NoSuchProviderException e) {
throw new JavaMailerException("No provider matching:" + readmailHost.getReadmailProtocol().getTransport() + " from config:" + m_config.getName(), e);
} catch (MessagingException e) {
throw new JavaMailerException("Problem reading messages from configured mail store", e);
}
return new ArrayList<Message>(Arrays.asList(msgs));
}
use of org.opennms.netmgt.config.javamail.UserAuth in project opennms by OpenNMS.
the class JavaSendMailer method configureProperties.
/**
* Configure properties.
* <p>This method uses a properties file reader to pull in opennms styled javamail properties and sets
* the actual javamail properties. This is here to preserve the backwards compatibility but configuration
* will probably change soon.</p>
* <p>FIXME definitely will change soon, will be deprecated.</p>
*
* @param sendmailConfigDefinedProps the sendmail configuration defined properties
* @param useJmProps a boolean representing the handling of the deprecated javamail-configuration.properties file.
*/
private void configureProperties(Properties sendmailConfigDefinedProps, boolean useJmProps) {
// this loads the properties from the old style javamail-configuration.properties
// TODO: deprecate this
Properties props = null;
try {
props = JavaMailerConfig.getProperties();
/* These strange properties from javamail-configuration.properties need to be translated into actual javax.mail properties
* FIXME: The precedence of the properties file vs. the SendmailConfiguration should probably be addressed here
* FIXME: if using a valid sendmail config, it probably doesn't make sense to use any of these properties
*/
if (useJmProps) {
m_config.setDebug(PropertiesUtils.getProperty(props, "org.opennms.core.utils.debug", m_config.isDebug()));
if (m_config.getSendmailHost() != null) {
final SendmailHost sendmailHost = m_config.getSendmailHost();
sendmailHost.setHost(PropertiesUtils.getProperty(props, "org.opennms.core.utils.mailHost", sendmailHost.getHost()));
sendmailHost.setPort(PropertiesUtils.getProperty(props, "org.opennms.core.utils.smtpport", sendmailHost.getPort()));
}
if (m_config.getSendmailProtocol() != null) {
final SendmailProtocol sendmailProtocol = m_config.getSendmailProtocol();
sendmailProtocol.setMailer(PropertiesUtils.getProperty(props, "org.opennms.core.utils.mailer", sendmailProtocol.getMailer()));
sendmailProtocol.setTransport(PropertiesUtils.getProperty(props, "org.opennms.core.utils.transport", sendmailProtocol.getTransport()));
sendmailProtocol.setMessageContentType(PropertiesUtils.getProperty(props, "org.opennms.core.utils.messageContentType", sendmailProtocol.getMessageContentType()));
sendmailProtocol.setCharSet(PropertiesUtils.getProperty(props, "org.opennms.core.utils.charset", sendmailProtocol.getCharSet()));
sendmailProtocol.setMessageEncoding(PropertiesUtils.getProperty(props, "org.opennms.core.utils.encoding", sendmailProtocol.getMessageEncoding()));
sendmailProtocol.setStartTls(PropertiesUtils.getProperty(props, "org.opennms.core.utils.starttls.enable", sendmailProtocol.isStartTls()));
sendmailProtocol.setQuitWait(PropertiesUtils.getProperty(props, "org.opennms.core.utils.quitwait", sendmailProtocol.isQuitWait()));
sendmailProtocol.setSslEnable(PropertiesUtils.getProperty(props, "org.opennms.core.utils.smtpssl.enable", sendmailProtocol.isSslEnable()));
}
if (m_config.getUserAuth() != null) {
final UserAuth userAuth = m_config.getUserAuth();
userAuth.setUserName(PropertiesUtils.getProperty(props, "org.opennms.core.utils.authenticateUser", userAuth.getUserName()));
userAuth.setPassword(PropertiesUtils.getProperty(props, "org.opennms.core.utils.authenticatePassword", userAuth.getPassword()));
}
if (m_config.getSendmailMessage() != null) {
final SendmailMessage sendmailMessage = m_config.getSendmailMessage();
sendmailMessage.setFrom(PropertiesUtils.getProperty(props, "org.opennms.core.utils.fromAddress", sendmailMessage.getFrom()));
}
m_config.setUseJmta(PropertiesUtils.getProperty(props, "org.opennms.core.utils.useJMTA", m_config.isUseJmta()));
m_config.setUseAuthentication(PropertiesUtils.getProperty(props, "org.opennms.core.utils.authenticate", m_config.isUseAuthentication()));
}
} catch (IOException e) {
LOG.info("configureProperties: could not load javamail.properties, continuing for is no longer required", e);
}
// this sets any javamail properties that were set in the SendmailConfig object
if (props == null) {
props = new Properties();
}
props.putAll(sendmailConfigDefinedProps);
if (m_config.getSendmailProtocol() != null) {
final SendmailProtocol sendmailProtocol = m_config.getSendmailProtocol();
if (!props.containsKey("mail.smtp.starttls.enable")) {
props.setProperty("mail.smtp.starttls.enable", String.valueOf(sendmailProtocol.isStartTls()));
}
if (!props.containsKey("mail.smtp.quitwait")) {
props.setProperty("mail.smtp.quitwait", String.valueOf(sendmailProtocol.isQuitWait()));
}
if (!props.containsKey("mail.smtp.quitwait")) {
props.setProperty("mail.smtp.quitwait", String.valueOf(sendmailProtocol.isQuitWait()));
}
if (sendmailProtocol.isSslEnable()) {
if (!props.containsKey("mail.smtps.auth")) {
props.setProperty("mail.smtps.auth", String.valueOf(m_config.isUseAuthentication()));
}
if (!props.containsKey("mail.smtps.socketFactory.class")) {
props.setProperty("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
if (!props.containsKey("mail.smtps.socketFactory.port") && m_config.getSendmailHost() != null) {
props.setProperty("mail.smtps.socketFactory.port", String.valueOf(m_config.getSendmailHost().getPort()));
}
}
}
if (!props.containsKey("mail.smtp.auth")) {
props.setProperty("mail.smtp.auth", String.valueOf(m_config.isUseAuthentication()));
}
if (!props.containsKey("mail.smtp.port") && m_config.getSendmailHost() != null) {
props.setProperty("mail.smtp.port", String.valueOf(m_config.getSendmailHost().getPort()));
}
}
use of org.opennms.netmgt.config.javamail.UserAuth in project opennms by OpenNMS.
the class JavaReadMailerTest method createGoogleReadMailer.
private JavaReadMailer createGoogleReadMailer(String gmailAccount, String gmailPassword) throws JavaMailerException {
ReadmailConfig config = new ReadmailConfig();
config.setDebug(true);
config.setDeleteAllMail(false);
config.setMailFolder("INBOX");
ReadmailHost readmailHost = new ReadmailHost();
readmailHost.setHost("imap.gmail.com");
readmailHost.setPort(993);
ReadmailProtocol readmailProtocol = new ReadmailProtocol();
readmailProtocol.setSslEnable(true);
readmailProtocol.setStartTls(false);
readmailProtocol.setTransport("imaps");
readmailHost.setReadmailProtocol(readmailProtocol);
config.setReadmailHost(readmailHost);
UserAuth userAuth = new UserAuth();
userAuth.setPassword(gmailPassword);
userAuth.setUserName(gmailAccount);
config.setUserAuth(userAuth);
JavaReadMailer mailer = new JavaReadMailer(config, true);
return mailer;
}
use of org.opennms.netmgt.config.javamail.UserAuth in project opennms by OpenNMS.
the class JavaReadMailerTest method createSendMailer.
private JavaSendMailer createSendMailer(String gmailAccount, String gmailPassword) throws JavaMailerException {
SendmailConfig config = new SendmailConfig();
config.setAttemptInterval(1000l);
config.setDebug(true);
config.setName("test");
SendmailMessage sendmailMessage = new SendmailMessage();
sendmailMessage.setBody("service is down");
sendmailMessage.setFrom("bamboo.opennms@gmail.com");
sendmailMessage.setSubject("Notice #1234: service down");
sendmailMessage.setTo("bamboo.opennms@gmail.com");
config.setSendmailMessage(sendmailMessage);
SendmailHost host = new SendmailHost();
host.setHost("smtp.gmail.com");
host.setPort(465);
config.setSendmailHost(host);
SendmailProtocol protocol = new SendmailProtocol();
protocol.setSslEnable(true);
protocol.setTransport("smtps");
config.setSendmailProtocol(protocol);
config.setUseAuthentication(true);
config.setUseJmta(false);
UserAuth auth = new UserAuth();
auth.setUserName(gmailAccount);
auth.setPassword(gmailPassword);
config.setUserAuth(auth);
return new JavaSendMailer(config);
}
use of org.opennms.netmgt.config.javamail.UserAuth in project opennms by OpenNMS.
the class JavaMailAckReaderIT method createSendMailer.
private JavaSendMailer createSendMailer(String gmailAccount, String gmailPassword) throws JavaMailerException {
SendmailConfig config = new SendmailConfig();
config.setAttemptInterval(1000l);
config.setDebug(true);
config.setName("test");
SendmailMessage sendmailMessage = new SendmailMessage();
sendmailMessage.setBody("service is down");
sendmailMessage.setFrom("bamboo.opennms@gmail.com");
sendmailMessage.setSubject("Notice #1234: service down");
sendmailMessage.setTo("bamboo.opennms@gmail.com");
config.setSendmailMessage(sendmailMessage);
SendmailHost host = new SendmailHost();
host.setHost("smtp.gmail.com");
host.setPort(465);
config.setSendmailHost(host);
SendmailProtocol protocol = new SendmailProtocol();
protocol.setSslEnable(true);
protocol.setTransport("smtps");
config.setSendmailProtocol(protocol);
config.setUseAuthentication(true);
config.setUseJmta(false);
UserAuth auth = new UserAuth();
auth.setUserName(gmailAccount);
auth.setPassword(gmailPassword);
config.setUserAuth(auth);
return new JavaSendMailer(config);
}
Aggregations