KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > mail > CmsSimpleMail


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/mail/CmsSimpleMail.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.10 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.mail;
33
34 import org.opencms.main.CmsLog;
35 import org.opencms.main.CmsRuntimeException;
36 import org.opencms.main.OpenCms;
37 import org.opencms.util.CmsStringUtil;
38
39 import javax.mail.AuthenticationFailedException JavaDoc;
40 import javax.mail.MessagingException JavaDoc;
41 import javax.mail.SendFailedException JavaDoc;
42
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.mail.SimpleEmail;
45
46 /**
47  * This class is used to send simple text internet email messages without
48  * attachments.<p>
49  *
50  * It uses the Apache Commons Email API and extends the provided classes
51  * to conveniently generate emails using the OpenCms configuration.<p>
52  *
53  * @author Andreas Zahner
54  *
55  * @version $Revision: 1.10 $
56  *
57  * @since 6.0.0
58  */

59 public class CmsSimpleMail extends SimpleEmail {
60
61     /** The log object for this class. */
62     private static final Log LOG = CmsLog.getLog(CmsSimpleMail.class);
63
64     /**
65      * Default constructor of a CmsSimpleMail.<p>
66      *
67      * The mail host name and the mail from address are set to the OpenCms
68      * default values of the configuration.<p>
69      *
70      */

71     public CmsSimpleMail() {
72
73         // call super constructor
74
super();
75         // set the host to the default mail host
76
CmsMailHost host = OpenCms.getSystemInfo().getMailSettings().getDefaultMailHost();
77         setHostName(host.getHostname());
78
79         // check if username and password are provided
80
String JavaDoc userName = host.getUsername();
81         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userName)) {
82             // authentication needed, set user name and password
83
setAuthentication(userName, host.getPassword());
84         }
85         try {
86             // set default mail from address
87
setFrom(OpenCms.getSystemInfo().getMailSettings().getMailFromDefault());
88         } catch (MessagingException JavaDoc e) {
89             // default email address is not valid, log error
90
LOG.error(Messages.get().getBundle().key(Messages.LOG_INVALID_SENDER_ADDRESS_0), e);
91         }
92     }
93
94     /**
95      * Overrides to add a layer for localization of exception / logging.<p>
96      *
97      * Please note that in case of a <code>{@link SendFailedException}</code>
98      * the cause of this message will contain a <code>{@link CmsRuntimeException}</code>
99      * as cause. The information of the <code>SendFaileException</code> should be used outside
100      * to remove recipients (e.g. from the beans that store them) in order to avoid duplicate
101      * sending of emails. The internal cause then just should be rethrown to allow localized
102      * output about the cause.<p>
103      *
104      *
105      * @see org.apache.commons.mail.Email#send()
106      *
107      * @throws MessagingException if something goes wrong
108      * @throws SendFailedException if sending failed, please look at the possibility to use this type to remove
109      * recepients from a mass mail where sending suceeded.
110      */

111     public void send() throws MessagingException JavaDoc, SendFailedException JavaDoc {
112
113         try {
114             super.send();
115         } catch (SendFailedException JavaDoc sf) {
116             // The specialized exception types (authentication, wrong host) are wrapped in this type:
117
MessagingException JavaDoc me = (MessagingException JavaDoc)sf.getNextException();
118             CmsMailHost host = OpenCms.getSystemInfo().getMailSettings().getDefaultMailHost();
119             if (me instanceof AuthenticationFailedException JavaDoc) {
120                 // wrong user credentials in opencms-system.xml
121
CmsRuntimeException rte = new CmsRuntimeException(Messages.get().container(
122                     Messages.ERR_SEND_EMAIL_AUTHENTICATE_2,
123                     host.getUsername(),
124                     host.getHostname()));
125                 sf.initCause(rte);
126                 throw sf;
127             }
128             // wrong hostname in opencms-system.xml
129
CmsRuntimeException rte = new CmsRuntimeException(Messages.get().container(
130                 Messages.ERR_SEND_EMAIL_HOSTNAME_1,
131                 host.getHostname()), me);
132             sf.initCause(rte);
133             throw sf;
134
135         }
136     }
137 }
138
Popular Tags