001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/mail/CmsSimpleMail.java,v $
003: * Date : $Date: 2008-02-27 12:05:41 $
004: * Version: $Revision: 1.12 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.mail;
033:
034: import org.opencms.main.CmsLog;
035: import org.opencms.main.CmsRuntimeException;
036: import org.opencms.main.OpenCms;
037: import org.opencms.util.CmsStringUtil;
038:
039: import javax.mail.AuthenticationFailedException;
040: import javax.mail.MessagingException;
041: import javax.mail.SendFailedException;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.mail.SimpleEmail;
045:
046: /**
047: * This class is used to send simple text internet email messages without
048: * attachments.<p>
049: *
050: * It uses the Apache Commons Email API and extends the provided classes
051: * to conveniently generate emails using the OpenCms configuration.<p>
052: *
053: * @author Andreas Zahner
054: *
055: * @version $Revision: 1.12 $
056: *
057: * @since 6.0.0
058: */
059: public class CmsSimpleMail extends SimpleEmail {
060:
061: /** The log object for this class. */
062: private static final Log LOG = CmsLog.getLog(CmsSimpleMail.class);
063:
064: /**
065: * Default constructor of a CmsSimpleMail.<p>
066: *
067: * The mail host name and the mail from address are set to the OpenCms
068: * default values of the configuration.<p>
069: *
070: */
071: public CmsSimpleMail() {
072:
073: // call super constructor
074: super ();
075: // set the host to the default mail host
076: CmsMailHost host = OpenCms.getSystemInfo().getMailSettings()
077: .getDefaultMailHost();
078: setHostName(host.getHostname());
079:
080: // check if username and password are provided
081: String userName = host.getUsername();
082: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userName)) {
083: // authentication needed, set user name and password
084: setAuthentication(userName, host.getPassword());
085: }
086: try {
087: // set default mail from address
088: setFrom(OpenCms.getSystemInfo().getMailSettings()
089: .getMailFromDefault());
090: } catch (MessagingException e) {
091: // default email address is not valid, log error
092: LOG.error(Messages.get().getBundle().key(
093: Messages.LOG_INVALID_SENDER_ADDRESS_0), e);
094: }
095: }
096:
097: /**
098: * Overrides to add a layer for localization of exception / logging.<p>
099: *
100: * Please note that in case of a <code>{@link SendFailedException}</code>
101: * the cause of this message will contain a <code>{@link CmsRuntimeException}</code>
102: * as cause. The information of the <code>SendFaileException</code> should be used outside
103: * to remove recipients (e.g. from the beans that store them) in order to avoid duplicate
104: * sending of emails. The internal cause then just should be rethrown to allow localized
105: * output about the cause.<p>
106: *
107: *
108: * @see org.apache.commons.mail.Email#send()
109: *
110: * @throws MessagingException if something goes wrong
111: * @throws SendFailedException if sending failed, please look at the possibility to use this type to remove
112: * recepients from a mass mail where sending suceeded.
113: */
114: public void send() throws MessagingException, SendFailedException {
115:
116: try {
117: super .send();
118: } catch (SendFailedException sf) {
119: // The specialized exception types (authentication, wrong host) are wrapped in this type:
120: MessagingException me = (MessagingException) sf
121: .getNextException();
122: CmsMailHost host = OpenCms.getSystemInfo()
123: .getMailSettings().getDefaultMailHost();
124: if (me instanceof AuthenticationFailedException) {
125: // wrong user credentials in opencms-system.xml
126: CmsRuntimeException rte = new CmsRuntimeException(
127: Messages.get().container(
128: Messages.ERR_SEND_EMAIL_AUTHENTICATE_2,
129: host.getUsername(), host.getHostname()));
130: sf.initCause(rte);
131: throw sf;
132: }
133: // wrong hostname in opencms-system.xml
134: CmsRuntimeException rte = new CmsRuntimeException(Messages
135: .get().container(
136: Messages.ERR_SEND_EMAIL_HOSTNAME_1,
137: host.getHostname()), me);
138: sf.initCause(rte);
139: throw sf;
140:
141: }
142: }
143: }
|