diff --git a/common/source/java/ch/systemsx/cisd/common/mail/IMailClient.java b/common/source/java/ch/systemsx/cisd/common/mail/IMailClient.java new file mode 100644 index 0000000000000000000000000000000000000000..5dba4a0629c65671dc7e45b2e8eb7f618bbc17ac --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/mail/IMailClient.java @@ -0,0 +1,36 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.mail; + +import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; + +/** + * + * + * @author Franz-Josef Elmer + */ +public interface IMailClient +{ + + /** + * Sends a mail with given <var>subject</var> and <var>content</var> to given <var>recipients</var>. + * + * @param recipients list of recipients (of type <code>Message.RecipientType.TO</code>) + */ + public void sendMessage(String subject, String content, String... recipients) throws EnvironmentFailureException; + +} \ No newline at end of file diff --git a/common/source/java/ch/systemsx/cisd/common/mail/MailClient.java b/common/source/java/ch/systemsx/cisd/common/mail/MailClient.java index d72cf6cd6089d972baad16e692d36bf97caf7e93..f6d0347c6182f456b19ce53407c1f05b6f90d8e3 100644 --- a/common/source/java/ch/systemsx/cisd/common/mail/MailClient.java +++ b/common/source/java/ch/systemsx/cisd/common/mail/MailClient.java @@ -31,6 +31,7 @@ import javax.mail.internet.MimeMessage; import org.apache.log4j.Logger; +import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogFactory; @@ -42,7 +43,7 @@ import ch.systemsx.cisd.common.logging.LogFactory; * * @author Christian Ribeaud */ -public final class MailClient extends Authenticator +public final class MailClient extends Authenticator implements IMailClient { /** This system property is not supported by the <i>JavaMail API</i> */ @@ -139,7 +140,8 @@ public final class MailClient extends Authenticator * * @param recipients list of recipients (of type <code>Message.RecipientType.TO</code>) */ - public final void sendMessage(String subject, String content, String... recipients) throws MessagingException + public final void sendMessage(String subject, String content, String... recipients) + throws EnvironmentFailureException { if (operationLog.isInfoEnabled()) { @@ -152,11 +154,18 @@ public final class MailClient extends Authenticator internetAddresses[i] = createInternetAddress(recipients[i]); } MimeMessage msg = new MimeMessage(createSession()); - msg.setFrom(createInternetAddress(from)); - msg.addRecipients(Message.RecipientType.TO, internetAddresses); - msg.setSubject(subject); - msg.setText(content); - Transport.send(msg); + try + { + msg.setFrom(createInternetAddress(from)); + msg.addRecipients(Message.RecipientType.TO, internetAddresses); + msg.setSubject(subject); + msg.setText(content); + Transport.send(msg); + } catch (MessagingException ex) + { + throw new EnvironmentFailureException("Sending e-mail with subject '" + subject + "' to recipients " + + Arrays.asList(recipients) + " failed. Reason: " + ex, ex); + } } //