001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.util;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: import javax.mail.Message;
024: import javax.mail.MessagingException;
025: import javax.mail.SendFailedException;
026: import javax.mail.Session;
027: import javax.mail.Transport;
028: import javax.mail.Address;
029: import javax.mail.internet.InternetAddress;
030: import javax.mail.internet.MimeMessage;
031: import org.apache.commons.lang.StringUtils;
032:
033: public class MailUtil extends Object {
034:
035: private static Log mLogger = LogFactory.getFactory().getInstance(
036: MailUtil.class);
037:
038: // agangolli: Incorporated suggested changes from Ken Blackler.
039:
040: /**
041: * This method is used to send a Message with a pre-defined
042: * mime-type.
043: *
044: * @param from e-mail address of sender
045: * @param to e-mail address(es) of recipients
046: * @param subject subject of e-mail
047: * @param content the body of the e-mail
048: * @param mimeType type of message, i.e. text/plain or text/html
049: * @throws MessagingException the exception to indicate failure
050: */
051: public static void sendMessage(Session session, String from,
052: String[] to, String[] cc, String[] bcc, String subject,
053: String content, String mimeType) throws MessagingException {
054: Message message = new MimeMessage(session);
055:
056: // n.b. any default from address is expected to be determined by caller.
057: if (!StringUtils.isEmpty(from)) {
058: InternetAddress sentFrom = new InternetAddress(from);
059: message.setFrom(sentFrom);
060: if (mLogger.isDebugEnabled())
061: mLogger.debug("e-mail from: " + sentFrom);
062: }
063:
064: if (to != null) {
065: InternetAddress[] sendTo = new InternetAddress[to.length];
066:
067: for (int i = 0; i < to.length; i++) {
068: sendTo[i] = new InternetAddress(to[i]);
069: if (mLogger.isDebugEnabled())
070: mLogger.debug("sending e-mail to: " + to[i]);
071: }
072: message.setRecipients(Message.RecipientType.TO, sendTo);
073: }
074:
075: if (cc != null) {
076: InternetAddress[] copyTo = new InternetAddress[cc.length];
077:
078: for (int i = 0; i < cc.length; i++) {
079: copyTo[i] = new InternetAddress(cc[i]);
080: if (mLogger.isDebugEnabled())
081: mLogger.debug("copying e-mail to: " + cc[i]);
082: }
083: message.setRecipients(Message.RecipientType.CC, copyTo);
084: }
085:
086: if (bcc != null) {
087: InternetAddress[] copyTo = new InternetAddress[bcc.length];
088:
089: for (int i = 0; i < bcc.length; i++) {
090: copyTo[i] = new InternetAddress(bcc[i]);
091: if (mLogger.isDebugEnabled())
092: mLogger.debug("blind copying e-mail to: " + bcc[i]);
093: }
094: message.setRecipients(Message.RecipientType.BCC, copyTo);
095: }
096: message
097: .setSubject((subject == null) ? "(no subject)"
098: : subject);
099: message.setContent(content, mimeType);
100: message.setSentDate(new java.util.Date());
101:
102: // First collect all the addresses together.
103: Address[] remainingAddresses = message.getAllRecipients();
104: int nAddresses = remainingAddresses.length;
105: boolean bFailedToSome = false;
106:
107: SendFailedException sendex = new SendFailedException(
108: "Unable to send message to some recipients");
109:
110: // Try to send while there remain some potentially good addresses
111: do {
112: // Avoid a loop if we are stuck
113: nAddresses = remainingAddresses.length;
114:
115: try {
116: // Send to the list of remaining addresses, ignoring the addresses attached to the message
117: Transport.send(message, remainingAddresses);
118: } catch (SendFailedException ex) {
119: bFailedToSome = true;
120: sendex.setNextException(ex);
121:
122: // Extract the remaining potentially good addresses
123: remainingAddresses = ex.getValidUnsentAddresses();
124: }
125: } while (remainingAddresses != null
126: && remainingAddresses.length > 0
127: && remainingAddresses.length != nAddresses);
128:
129: if (bFailedToSome)
130: throw sendex;
131: }
132:
133: /**
134: * This method is used to send a Text Message.
135: *
136: * @param from e-mail address of sender
137: * @param to e-mail addresses of recipients
138: * @param subject subject of e-mail
139: * @param content the body of the e-mail
140: * @throws MessagingException the exception to indicate failure
141: */
142: public static void sendTextMessage(Session session, String from,
143: String[] to, String[] cc, String[] bcc, String subject,
144: String content) throws MessagingException {
145: sendMessage(session, from, to, cc, bcc, subject, content,
146: "text/plain; charset=utf-8");
147: }
148:
149: /**
150: * This method overrides the sendTextMessage to specify
151: * one receiver and mulitple cc recipients.
152: *
153: * @param from e-mail address of sender
154: * @param to e-mail addresses of recipients
155: * @param subject subject of e-mail
156: * @param content the body of the e-mail
157: * @throws MessagingException the exception to indicate failure
158: */
159: public static void sendTextMessage(Session session, String from,
160: String to, String[] cc, String[] bcc, String subject,
161: String content) throws MessagingException {
162: String[] recipient = null;
163: if (to != null)
164: recipient = new String[] { to };
165:
166: sendMessage(session, from, recipient, cc, bcc, subject,
167: content, "text/plain; charset=utf-8");
168: }
169:
170: /**
171: * This method overrides the sendTextMessage to specify
172: * only one receiver and cc recipients, rather than
173: * an array of recipients.
174: *
175: * @param from e-mail address of sender
176: * @param to e-mail address of recipient
177: * @param cc e-mail address of cc recipient
178: * @param subject subject of e-mail
179: * @param content the body of the e-mail
180: * @throws MessagingException the exception to indicate failure
181: */
182: public static void sendTextMessage(Session session, String from,
183: String to, String cc, String bcc, String subject,
184: String content) throws MessagingException {
185: String[] recipient = null;
186: String[] copy = null;
187: String[] bcopy = null;
188:
189: if (to != null)
190: recipient = new String[] { to };
191: if (cc != null)
192: copy = new String[] { cc };
193: if (bcc != null)
194: bcopy = new String[] { bcc };
195:
196: sendMessage(session, from, recipient, copy, bcopy, subject,
197: content, "text/plain; charset=utf-8");
198: }
199:
200: /**
201: * This method is used to send a HTML Message
202: *
203: * @param from e-mail address of sender
204: * @param to e-mail address(es) of recipients
205: * @param subject subject of e-mail
206: * @param content the body of the e-mail
207: * @throws MessagingException the exception to indicate failure
208: */
209: public static void sendHTMLMessage(Session session, String from,
210: String[] to, String[] cc, String[] bcc, String subject,
211: String content) throws MessagingException {
212: sendMessage(session, from, to, cc, bcc, subject, content,
213: "text/html; charset=utf-8");
214: }
215:
216: /**
217: * This method overrides the sendHTMLMessage to specify
218: * only one sender, rather than an array of senders.
219: *
220: * @param from e-mail address of sender
221: * @param to e-mail address of recipients
222: * @param subject subject of e-mail
223: * @param content the body of the e-mail
224: * @throws MessagingException the exception to indicate failure
225: */
226: public static void sendHTMLMessage(Session session, String from,
227: String to, String cc, String bcc, String subject,
228: String content) throws MessagingException {
229: String[] recipient = null;
230: String[] copy = null;
231: String[] bcopy = null;
232:
233: if (to != null)
234: recipient = new String[] { to };
235: if (cc != null)
236: copy = new String[] { cc };
237: if (bcc != null)
238: bcopy = new String[] { bcc };
239:
240: sendMessage(session, from, recipient, copy, bcopy, subject,
241: content, "text/html; charset=utf-8");
242: }
243:
244: /**
245: * This method overrides the sendHTMLMessage to specify
246: * one receiver and mulitple cc recipients.
247: *
248: * @param from e-mail address of sender
249: * @param to e-mail address of recipient
250: * @param cc e-mail addresses of recipients
251: * @param subject subject of e-mail
252: * @param content the body of the e-mail
253: * @throws MessagingException the exception to indicate failure
254: */
255: public static void sendHTMLMessage(Session session, String from,
256: String to, String[] cc, String[] bcc, String subject,
257: String content) throws MessagingException {
258: String[] recipient = null;
259: if (to != null)
260: recipient = new String[] { to };
261:
262: sendMessage(session, from, recipient, cc, bcc, subject,
263: content, "text/html; charset=utf-8");
264: }
265: }
|