Skip to content
Snippets Groups Projects
Commit 3e31fe23 authored by juanf's avatar juanf
Browse files

SSDM-238 : Support SMTP ports other than 25 and other parameters.

SVN: 31541
parent 6f618205
No related branches found
No related tags found
No related merge requests found
......@@ -38,12 +38,15 @@ public final class JavaMailProperties
/** The return email address of the current user. */
public static final String MAIL_FROM = "mail.from";
/** The default host name of the mail server for Transports. */
/** The host name of the mail server for Transports. */
public static final String MAIL_SMTP_HOST = "mail.smtp.host";
/** The default user name to use when connecting to the mail server. */
public static final String MAIL_SMTP_USER = "mail.smtp.user";
/** The host port of the mail server for Transports. */
public static final String MAIL_SMTP_PORT = "mail.smtp.port";
/** The user name to use when connecting to the mail server. */
public static final String MAIL_SMTP_USER = "mail.smtp.user";
/** Whether authentication is needed when connecting to the mail server. */
public static final String MAIL_SMTP_AUTH = "mail.smtp.auth";
......
......@@ -40,7 +40,6 @@ import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
......@@ -48,6 +47,7 @@ import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.filesystem.FileUtilities;
import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.shared.basic.string.StringUtils;
/**
* A small mail client that simplifies the sending of emails using of <i>JavaMail API</i>.
......@@ -73,82 +73,68 @@ public final class MailClient extends Authenticator implements IMailClient
private static final String UNICODE_CHARSET = "utf-8";
private static final Logger operationLog =
LogFactory.getLogger(LogCategory.OPERATION, MailClient.class);
private final String smtpUsername;
private final String smtpPassword;
private final String smtpHost;
private final String from;
private final String testAddress;
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, MailClient.class);
private final Properties properties;
public MailClient(final String from, final String smtpHost)
{
this(from, smtpHost, null, null, null);
properties = new Properties();
properties.put(JavaMailProperties.MAIL_FROM, from);
properties.put(JavaMailProperties.MAIL_SMTP_HOST, smtpHost);
init();
}
public MailClient(MailClientParameters parameters)
{
this(parameters.getFrom(), parameters.getSmtpHost(), parameters.getSmtpUser(), parameters
.getSmtpPassword(), null);
this.properties = parameters.getPropertiesInstance();
init();
}
public MailClient(Properties properties)
{
this(properties.getProperty(JavaMailProperties.MAIL_FROM), properties
.getProperty(JavaMailProperties.MAIL_SMTP_HOST), properties
.getProperty(JavaMailProperties.MAIL_SMTP_USER), properties
.getProperty(MAIL_SMTP_PASSWORD), properties.getProperty(MAIL_TEST_ADDRESS));
}
public MailClient(final String from, final String smtpHost, final String smtpUsername,
final String smtpPassword, final String testAddress)
{
assert from != null;
assert smtpHost != null;
this.from = from;
this.smtpHost = smtpHost;
this.smtpUsername = smtpUsername;
this.smtpPassword = smtpPassword;
this.testAddress = testAddress;
this.properties = properties;
init();
}
private final Properties createProperties()
{
Properties properties = null;
try
private void init() {
//Add some properties that don't need to come form the service
String smtpUsername = properties.getProperty(JavaMailProperties.MAIL_SMTP_USER);
String smtpPassword = properties.getProperty(MailClient.MAIL_SMTP_PASSWORD);
if (StringUtils.isNotBlank(smtpUsername) && StringUtils.isNotBlank(smtpPassword))
{
properties = new Properties(System.getProperties());
} catch (SecurityException ex)
{
properties = new Properties();
properties.put(JavaMailProperties.MAIL_SMTP_AUTH, Boolean.TRUE.toString());
}
if (smtpUsername != null)
properties.put(JavaMailProperties.MAIL_DEBUG,operationLog.isDebugEnabled() ? Boolean.TRUE.toString() : Boolean.FALSE.toString());
properties.put(JavaMailProperties.MAIL_TRANSPORT_PROTOCOL, "smtp");
//Get properties from System
try
{
properties.put(JavaMailProperties.MAIL_SMTP_USER, smtpUsername);
}
if (smtpHost != null)
Properties systemProperties = System.getProperties();
for(String key:systemProperties.stringPropertyNames()) {
if(!properties.containsKey(key)) {
properties.put(key, systemProperties.getProperty(key));
}
}
} catch (SecurityException ex)
{
properties.put(JavaMailProperties.MAIL_SMTP_HOST, smtpHost);
operationLog.error("System properties can't be read", ex);
}
if (StringUtils.isNotBlank(smtpPassword) && StringUtils.isNotBlank(smtpUsername))
{
properties.put(JavaMailProperties.MAIL_SMTP_AUTH, Boolean.TRUE.toString());
//Clear all properties not staring with mail.
for(String key:properties.stringPropertyNames()) {
if(!key.startsWith("mail.")) {
properties.remove(key);
}
}
properties.put(JavaMailProperties.MAIL_DEBUG, operationLog.isDebugEnabled() ? Boolean.TRUE
.toString() : Boolean.FALSE.toString());
properties.put(JavaMailProperties.MAIL_TRANSPORT_PROTOCOL, "smtp");
return properties;
}
@Override
public void sendTestEmail()
{
String testAddress = properties.getProperty(MailClient.MAIL_TEST_ADDRESS);
if (testAddress != null)
{
if (operationLog.isInfoEnabled())
......@@ -169,14 +155,11 @@ public final class MailClient extends Authenticator implements IMailClient
private final Session createSession()
{
Properties properties = createProperties();
if (operationLog.isDebugEnabled())
{
operationLog.debug("Creating mail session with following properties '" + properties
+ "'.");
operationLog.debug("Creating mail session with following properties '" + properties + "'.");
}
boolean mailSmtpAuth =
Boolean.parseBoolean(properties.getProperty(JavaMailProperties.MAIL_SMTP_AUTH));
boolean mailSmtpAuth = Boolean.parseBoolean(properties.getProperty(JavaMailProperties.MAIL_SMTP_AUTH));
Session session = Session.getInstance(properties, mailSmtpAuth ? this : null);
session.setDebug(operationLog.isDebugEnabled());
return session;
......@@ -243,8 +226,8 @@ public final class MailClient extends Authenticator implements IMailClient
}
try
{
return new InternetAddress(addressOrNull.tryGetEmailAddress(), addressOrNull
.tryGetPersonalName());
return new InternetAddress(addressOrNull.tryGetEmailAddress(),
addressOrNull.tryGetPersonalName());
} catch (Exception e)
{
operationLog.error("Could not parse address [" + addressOrNull + "].", e);
......@@ -373,6 +356,8 @@ public final class MailClient extends Authenticator implements IMailClient
InternetAddress replyTo, InternetAddress fromOrNull, InternetAddress[] recipients)
throws EnvironmentFailureException
{
String from = properties.getProperty(JavaMailProperties.MAIL_FROM);
final InternetAddress fromPerMail =
(fromOrNull != null) ? fromOrNull : createInternetAddress(from);
if (operationLog.isInfoEnabled())
......@@ -428,6 +413,8 @@ public final class MailClient extends Authenticator implements IMailClient
private void send(MimeMessage msg) throws MessagingException
{
String smtpHost = properties.getProperty(JavaMailProperties.MAIL_SMTP_HOST);
if (smtpHost.startsWith(FILE_PREFIX))
{
// We don't have a real SMTP server
......@@ -442,6 +429,8 @@ public final class MailClient extends Authenticator implements IMailClient
@SuppressWarnings("unchecked")
private void writeMessageToFile(MimeMessage msg) throws MessagingException
{
String smtpHost = properties.getProperty(JavaMailProperties.MAIL_SMTP_HOST);
File emailFolder = new File(smtpHost.substring(FILE_PREFIX.length()));
if (emailFolder.exists())
{
......@@ -495,6 +484,9 @@ public final class MailClient extends Authenticator implements IMailClient
@Override
protected final PasswordAuthentication getPasswordAuthentication()
{
String smtpUsername = properties.getProperty(JavaMailProperties.MAIL_SMTP_USER);
String smtpPassword = properties.getProperty(MailClient.MAIL_SMTP_PASSWORD);
return new PasswordAuthentication(smtpUsername, smtpPassword);
}
......
......@@ -17,6 +17,7 @@
package ch.systemsx.cisd.common.mail;
import java.io.Serializable;
import java.util.Properties;
/**
* @author Franz-Josef Elmer
......@@ -33,6 +34,8 @@ public class MailClientParameters implements Serializable
private String smtpHost;
private String smtpPort;
private String testAddress;
public final String getFrom()
......@@ -84,7 +87,17 @@ public class MailClientParameters implements Serializable
{
this.smtpHost = smtpHost;
}
public String getSmtpPort()
{
return smtpPort;
}
public void setSmtpPort(String smtpPort)
{
this.smtpPort = smtpPort;
}
public String getTestAddress()
{
return testAddress;
......@@ -94,4 +107,15 @@ public class MailClientParameters implements Serializable
{
this.testAddress = testAddress;
}
public Properties getPropertiesInstance() {
Properties properties = new Properties();
properties.put(JavaMailProperties.MAIL_FROM, (from == null)?"":from);
properties.put(JavaMailProperties.MAIL_SMTP_USER, (smtpUser == null)?"":smtpUser);
properties.put(MailClient.MAIL_SMTP_PASSWORD, (smtpPassword == null)?"":smtpPassword);
properties.put(JavaMailProperties.MAIL_SMTP_HOST, (smtpHost == null)?"":smtpHost);
properties.put(JavaMailProperties.MAIL_SMTP_PORT, (smtpPort == null)?"":smtpPort);
properties.put(MailClient.MAIL_TEST_ADDRESS, (testAddress == null)?"":testAddress);
return properties;
}
}
......@@ -333,11 +333,12 @@
<bean id="mail-client-parameters" class="ch.systemsx.cisd.common.mail.MailClientParameters">
<property name="from" value="${mail.from}"/>
<property name="smtpHost" value="${mail.smtp.host}"/>
<property name="smtpPort" value="${mail.smtp.port}"/>
<property name="smtpUser" value="${mail.smtp.user}"/>
<property name="smtpPassword" value="${mail.smtp.password}"/>
<property name="testAddress" value="${mail.smtp.address}"/>
</bean>
<!--
Maintenance Tasks
-->
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment