use of javax.mail.Session in project zm-mailbox by Zimbra.
the class DataSourceManager method test.
/*
* Tests connecting to a data source. Do not actually create the
* data source.
*/
public static void test(DataSource ds) throws ServiceException {
ZimbraLog.datasource.info("Testing: %s", ds);
try {
DataImport di = getInstance().getDataImport(ds, true);
di.test();
if (ds.isSmtpEnabled()) {
Session session = JMSession.getSession(ds);
Transport smtp = session.getTransport();
String emailAddress = ds.getEmailAddress();
if (smtp instanceof SmtpTransport) {
test((SmtpTransport) smtp, emailAddress);
} else {
test((SMTPTransport) smtp, emailAddress);
}
}
ZimbraLog.datasource.info("Test succeeded: %s", ds);
} catch (ServiceException x) {
ZimbraLog.datasource.info("Test failed: %s", ds, x);
throw x;
} catch (Exception e) {
ZimbraLog.datasource.info("Test failed: %s", ds, e);
throw ServiceException.INVALID_REQUEST("Datasource test failed", e);
}
}
use of javax.mail.Session in project Activiti by Activiti.
the class JndiEmailTest method setUp.
@BeforeClass
public void setUp() {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.provider.class", MockEmailTransport.class.getName());
props.put("mail.smtp.class", MockEmailTransport.class.getName());
props.put("mail.smtp.provider.vendor", "test");
props.put("mail.smtp.provider.version", "0.0.0");
Provider provider = new Provider(Type.TRANSPORT, "smtp", MockEmailTransport.class.getName(), "test", "1.0");
Session mailSession = Session.getDefaultInstance(props);
SimpleNamingContextBuilder builder = null;
try {
mailSession.setProvider(provider);
builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
builder.bind("java:comp/env/Session", mailSession);
} catch (NamingException e) {
logger.error("Naming error in email setup", e);
} catch (NoSuchProviderException e) {
logger.error("provider error in email setup", e);
}
}
use of javax.mail.Session in project zm-mailbox by Zimbra.
the class JMSession method getSmtpSession.
/**
* Returns the JavaMail SMTP {@link Session} with settings from the given
* account and its domain.
*/
public static Session getSmtpSession(Account account) throws MessagingException {
Domain domain = null;
if (account != null) {
try {
domain = Provisioning.getInstance().getDomain(account);
} catch (ServiceException e) {
ZimbraLog.smtp.warn("Unable to look up domain for account %s.", account.getName(), e);
}
}
Session session = getSmtpSession(domain);
if (account != null && account.isSmtpEnableTrace()) {
session.setDebug(true);
}
return session;
}
use of javax.mail.Session in project zm-mailbox by Zimbra.
the class JMSession method getSession.
public static Session getSession(DataSource ds) throws ServiceException {
String smtpHost = ds.getSmtpHost();
int smtpPort = ds.getSmtpPort();
boolean isAuthRequired = ds.isSmtpAuthRequired();
String smtpUser = ds.getSmtpUsername();
String smtpPass = ds.getDecryptedSmtpPassword();
if (DataSourceAuthMechanism.XOAUTH2.name().equalsIgnoreCase(ds.getAuthMechanism())) {
smtpPass = ds.getDecryptedOAuthToken();
}
boolean useSSL = ds.isSmtpConnectionSecure();
if (smtpHost == null || smtpHost.length() == 0) {
throw ServiceException.FAILURE("null smtp host", null);
}
if (smtpPort <= 0) {
throw ServiceException.FAILURE("invalid smtp port", null);
}
if (isAuthRequired && (smtpUser == null || smtpUser.length() == 0 || smtpPass == null || smtpPass.length() == 0)) {
throw ServiceException.FAILURE("missing smtp username or password", null);
}
long timeout = LC.javamail_smtp_timeout.longValue() * Constants.MILLIS_PER_SECOND;
String localhost = LC.zimbra_server_hostname.value();
Properties props = new Properties();
Session session;
props.put("mail.smtp.socketFactory", SocketFactories.defaultSocketFactory());
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl.socketFactory", SocketFactories.defaultSSLSocketFactory());
props.setProperty("mail.smtp.ssl.socketFactory.fallback", "false");
props.put("mail.smtps.ssl.socketFactory", SocketFactories.defaultSSLSocketFactory());
props.setProperty("mail.smtps.ssl.socketFactory.fallback", "false");
if (useSSL) {
props.setProperty("mail.transport.protocol", "smtps");
props.setProperty("mail.smtps.connectiontimeout", Long.toString(timeout));
props.setProperty("mail.smtps.timeout", Long.toString(timeout));
props.setProperty("mail.smtps.localhost", localhost);
props.setProperty("mail.smtps.sendpartial", "true");
props.setProperty("mail.smtps.host", smtpHost);
props.setProperty("mail.smtps.port", smtpPort + "");
if (isAuthRequired) {
props.setProperty("mail.smtps.auth", "true");
props.setProperty("mail.smtps.user", smtpUser);
props.setProperty("mail.smtps.password", smtpPass);
if (DataSourceAuthMechanism.XOAUTH2.name().equalsIgnoreCase(ds.getAuthMechanism())) {
addOAuth2Properties(smtpPass, props, "smtps");
}
session = Session.getInstance(props, new SmtpAuthenticator(smtpUser, smtpPass));
} else {
session = Session.getInstance(props);
}
session.setProtocolForAddress("rfc822", "smtps");
} else {
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.connectiontimeout", Long.toString(timeout));
props.setProperty("mail.smtp.timeout", Long.toString(timeout));
props.setProperty("mail.smtp.localhost", localhost);
props.setProperty("mail.smtp.sendpartial", "true");
props.setProperty("mail.smtp.host", smtpHost);
props.setProperty("mail.smtp.port", smtpPort + "");
if (LC.javamail_smtp_enable_starttls.booleanValue()) {
props.setProperty("mail.smtp.starttls.enable", "true");
// props.put("mail.smtp.socketFactory.class", TlsSocketFactory.getInstance());
}
if (isAuthRequired) {
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.user", smtpUser);
props.setProperty("mail.smtp.password", smtpPass);
if (DataSourceAuthMechanism.XOAUTH2.name().equalsIgnoreCase(ds.getAuthMechanism())) {
addOAuth2Properties(smtpPass, props, "smtp");
}
session = Session.getInstance(props, new SmtpAuthenticator(smtpUser, smtpPass));
} else {
session = Session.getInstance(props);
}
session.setProtocolForAddress("rfc822", "smtp");
}
if (LC.javamail_smtp_debug.booleanValue()) {
session.setDebug(true);
}
JMSession.setProviders(session);
return session;
}
use of javax.mail.Session in project zm-mailbox by Zimbra.
the class JMSession method getSmtpSession.
/**
* Returns a new JavaMail {@link Session} that has the latest SMTP settings
* from LDAP. Settings are retrieved from the local server and overridden by
* the domain.
*
* @param domain the domain, or {@code null} to use server settings
*/
private static Session getSmtpSession(Domain domain) throws MessagingException {
Server server;
try {
server = Provisioning.getInstance().getLocalServer();
} catch (ServiceException e) {
throw new MessagingException("Unable to initialize JavaMail session", e);
}
Properties props = getJavaMailSessionProperties(server, domain);
Session session = Session.getInstance(props);
setProviders(session);
if (LC.javamail_smtp_debug.booleanValue()) {
session.setDebug(true);
}
return session;
}
Aggregations