Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TextEmailNotifier |
|
| 2.0;2 |
1 | /* | |
2 | * This software is licensed under the Apache License, Version 2.0 | |
3 | * (the "License") agreement; you may not use this file except in compliance with | |
4 | * the License. You may obtain a copy of the License at | |
5 | * | |
6 | * http://www.apache.org/licenses/LICENSE-2.0 | |
7 | * | |
8 | * Unless required by applicable law or agreed to in writing, software | |
9 | * distributed under the License is distributed on an "AS IS" BASIS, | |
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
11 | * See the License for the specific language governing permissions and | |
12 | * limitations under the License. | |
13 | */ | |
14 | package net.admin4j.util.notify; | |
15 | ||
16 | import javax.servlet.FilterConfig; | |
17 | import javax.servlet.ServletConfig; | |
18 | import javax.servlet.ServletException; | |
19 | ||
20 | import net.admin4j.deps.commons.mail.SimpleEmail; | |
21 | import net.admin4j.util.Admin4jRuntimeException; | |
22 | import net.admin4j.util.HostUtils; | |
23 | ||
24 | /** | |
25 | * Will provide an email notification in plain text format. | |
26 | * <p> See BaseEmailNotifier for initialization parameters.</p> | |
27 | * <p>Note: If the logger for this class is DEBUG, the debug setting will | |
28 | * be passed onto the java mail api.</p> | |
29 | * @author D. Ashmore | |
30 | * @since 1.0 | |
31 | * @see BaseEmailNotifier | |
32 | */ | |
33 | public class TextEmailNotifier extends BaseEmailNotifier { | |
34 | ||
35 | 6 | public TextEmailNotifier() {} |
36 | 0 | public TextEmailNotifier(FilterConfig config) throws ServletException { |
37 | 0 | configure(config); |
38 | 0 | } |
39 | ||
40 | 0 | public TextEmailNotifier(ServletConfig config) throws ServletException { |
41 | 0 | configure(config); |
42 | 0 | } |
43 | ||
44 | public void notify(String subject, String message) { | |
45 | try { | |
46 | ||
47 | 6 | if (this.isSuppressEmail()) { |
48 | 0 | logger.debug("Email Notification surpressed: {}", message); |
49 | 0 | return; |
50 | } | |
51 | SimpleEmail email = new SimpleEmail(); | |
52 | email.setHostName(this.getMailServerHost()); | |
53 | email.setMsg(message); | |
54 | ||
55 | // Will invoke logging on javamail if true. | |
56 | email.setDebug(logger.isDebugEnabled()); | |
57 | ||
58 | email.setSubject(subject); | |
59 | email.setFrom(this.getFromEmailAddress()); | |
60 | email.addTo(this.getToEmailAddress(), HostUtils.getHostName()); | |
61 | email.send(); | |
62 | 6 | } catch (Exception e) { |
63 | 6 | throw new Admin4jRuntimeException(e); |
64 | 0 | } |
65 | ||
66 | 0 | } |
67 | ||
68 | } |