001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.mail;
018:
019: import java.util.Properties;
020:
021: import javax.activation.DataHandler;
022: import javax.mail.Address;
023: import javax.mail.Authenticator;
024: import javax.mail.Message;
025: import javax.mail.MessagingException;
026: import javax.mail.NoSuchProviderException;
027: import javax.mail.PasswordAuthentication;
028: import javax.mail.Session;
029: import javax.mail.Transport;
030: import javax.mail.internet.AddressException;
031: import javax.mail.internet.InternetAddress;
032: import javax.mail.internet.MimeMessage;
033:
034: import edu.iu.uis.eden.util.ByteArrayDataSource;
035:
036: /**
037: * Maintains a Java Mail session and can be used for sending emails.
038: *
039: * @author ewestfal
040: */
041: public class Mailer {
042:
043: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
044: .getLogger(getClass());
045:
046: private Properties configProperties;
047: private Authenticator authenticator;
048:
049: private Session currentSession;
050:
051: /**
052: * Create a AuthenticatedMailer with the default values for authenticated
053: * use of mail-relay.iu.edu.
054: *
055: * The Kerberos principle and and password should be a system principle
056: * rather than a user principle.
057: *
058: * @param senderAddress
059: * return address for the mail being sent
060: * @param kbPrincipleName
061: * kerberos principle name used to authenticate to mail-relay
062: * @param kbPrinciplePassword
063: * kerberos principle password used to authenticate to mail-relay
064: */
065: public Mailer(Properties configProperties,
066: Authenticator authenticator) {
067: this .configProperties = configProperties;
068: this .authenticator = authenticator;
069: }
070:
071: public Mailer(Properties configProperties) {
072: this (configProperties, null);
073: }
074:
075: public Mailer(Properties configProperties, String username,
076: String password) {
077: this (configProperties, new SimpleAuthenticator(username,
078: password));
079: }
080:
081: /**
082: * A method to configure the Mailer properties. This class will default to a
083: * set of properties but this method allows the calling program to set the
084: * specific properties.
085: *
086: * @param mailConfigProperties
087: * Proeprties object containing configuration information
088: */
089: public void setConfig(Properties configProperties) {
090: this .configProperties = configProperties;
091: }
092:
093: /**
094: * Sends an email to the given recipients.
095: *
096: * @param recipients
097: * list of addresses to which the message is sent
098: * @param subject
099: * subject of the message
100: * @param messageBody
101: * body of the message
102: * @param ccRecipients
103: * list of addresses which are to be cc'd on the message
104: * @param bccRecipients
105: * list of addresses which are to be bcc'd on the message
106: *
107: * @throws AddressException
108: * @throws MessagingException
109: */
110: public void sendMessage(String sender, Address[] recipients,
111: String subject, String messageBody, Address[] ccRecipients,
112: Address[] bccRecipients, boolean htmlMessage)
113: throws AddressException, MessagingException {
114: Session session = getCurrentSession();
115: session.setDebug(LOG.isDebugEnabled());
116: Message message = new MimeMessage(session);
117:
118: // From Address
119: message.setFrom(new InternetAddress(sender));
120:
121: // To Address
122: if (recipients != null && recipients.length > 0) {
123: message.addRecipients(Message.RecipientType.TO, recipients);
124: } else {
125: LOG.warn("No recipients indicated");
126: }
127:
128: // CC Address
129: if (ccRecipients != null && ccRecipients.length > 0) {
130: message.addRecipients(Message.RecipientType.CC,
131: ccRecipients);
132: }
133:
134: // BCC Address
135: if (bccRecipients != null && bccRecipients.length > 0) {
136: message.addRecipients(Message.RecipientType.BCC,
137: bccRecipients);
138: }
139:
140: // The Subject
141: message.setSubject(subject);
142: if (subject == null || "".equals(subject)) {
143: LOG.warn("Empty subject being sent");
144: }
145:
146: // Now the message body.
147:
148: if (htmlMessage) {
149: prepareHtmlMessage(messageBody, message);
150: } else {
151: message.setText(messageBody);
152: if (messageBody == null || "".equals(messageBody)) {
153: LOG.warn("Empty message body being sent.");
154: }
155: }
156:
157: // Finally, send the message!
158: Transport.send(message);
159:
160: }
161:
162: /**
163: * Send a message to the designated recipient with the specified subject and
164: * message. This is a convience class for simple message addressing
165: *
166: * @param recipient
167: * the email address to which the message is sent
168: * @param subject
169: * subject for the message
170: * @param messageBody
171: * body of the message to to be sent
172: * @param ccRecipient
173: * email address of a cc recipient
174: */
175: public void sendMessage(String sender, String recipient,
176: String subject, String messageBody, boolean htmlMessage)
177: throws AddressException, MessagingException {
178: final Address[] NO_RECIPIENTS = null;
179: Address[] recipients = { new InternetAddress(recipient) };
180: sendMessage(sender, recipients, subject, messageBody,
181: NO_RECIPIENTS, NO_RECIPIENTS, htmlMessage);
182: }
183:
184: /**
185: * @return current properties used to configure the mail session
186: */
187: public Properties getConfig() {
188: return configProperties;
189: }
190:
191: public Authenticator getAuthenticator() {
192: return authenticator;
193: }
194:
195: /**
196: * This allows direct access to the Mail Session. While this offers more
197: * flexibility, the AuthenticatedMailer will no longer be responsible for
198: * management of the this session.
199: *
200: * @return get the current session. If current session is null it creates a
201: * new one.
202: */
203: public Session getCurrentSession() throws NoSuchProviderException {
204: if (this .currentSession == null
205: || !this .currentSession.getTransport().isConnected()) {
206: this .currentSession = Session.getInstance(configProperties,
207: authenticator);
208: }
209: return currentSession;
210: }
211:
212: private void prepareHtmlMessage(String messageText, Message message)
213: throws MessagingException {
214: message.setDataHandler(new DataHandler(new ByteArrayDataSource(
215: messageText, "text/html")));
216: }
217:
218: /**
219: * SimpleAuthenticator is used to do simple authentication when the SMTP
220: * server requires it.
221: */
222: private static class SimpleAuthenticator extends
223: javax.mail.Authenticator {
224:
225: private final PasswordAuthentication passwordAuthentication;
226:
227: private SimpleAuthenticator(String username, String password) {
228: this .passwordAuthentication = new PasswordAuthentication(
229: username, password);
230: }
231:
232: public PasswordAuthentication getPasswordAuthentication() {
233: return passwordAuthentication;
234: }
235:
236: }
237:
238: }
|