001: /*
002: * (C) Copyright 2003 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package com.nabhinc.util;
023:
024: import java.util.Date;
025: import java.util.Vector;
026:
027: import javax.activation.DataHandler;
028: import javax.activation.DataSource;
029: import javax.activation.FileDataSource;
030: import javax.mail.Authenticator;
031: import javax.mail.MessagingException;
032: import javax.mail.Multipart;
033: import javax.mail.Session;
034: import javax.mail.Transport;
035: import javax.mail.internet.AddressException;
036: import javax.mail.internet.InternetAddress;
037: import javax.mail.internet.MimeBodyPart;
038: import javax.mail.internet.MimeMessage;
039: import javax.mail.internet.MimeMultipart;
040: import javax.mail.Message;
041:
042: import com.nabhinc.util.DefaultMailAuthenticator;
043:
044: /**
045: * Send Email utility class.
046: *
047: * @author Padmanabh Dabke
048: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
049: */
050: public class EmailUtil {
051: public static final String SMTP_HOST_PROPERTY = "mail.smtp.host";
052: public static final String SMTP_USER_PROPERTY = "mail.smtp.user";
053: public static final String SMTP_PASSWORD_PROPERTY = "stringbeans.smtp.password";
054:
055: //public static final String SMTP_MAIL_AUTHENTICATION = "mail.smtp.auth";
056:
057: /**
058: * Send a plain text email. If the body contains non US-ASCII characters,
059: * it will be encoded using platform's default charset.
060: * If more than one email addresses to be sent to, separate
061: * the addresses with comma.
062: * @param from The Sender email address.
063: * @param to A comma separated recipient email address(es).
064: * @param subject The subject of the email.
065: * @param body The email body/message.
066: * @throws AddressException Thrown if the sender or recipient email address is invalid format
067: * @throws MessagingException
068: */
069: public static void send(String from, String to, String subject,
070: String body) throws AddressException, MessagingException {
071:
072: send(from, to, subject, body, null);
073: }
074:
075: /**
076: * Send an email in format specified in the content's type.
077: * Plain text is assumed if contentType is null.
078: * If more than one email addresses to be sent to, separate
079: * the addresses with comma.
080: * If the body contains non US-ASCII characters,
081: * it will be encoded using platform's default charset.
082: * @param from The Sender email address.
083: * @param to A comma separated of recipient email address(es).
084: * @param subject The subject of the email.
085: * @param body The email message body.
086: * @param contentType Mime type of message body, i.e. text/html, text/plain, application/x-xxxxx. Default is text/plain.
087: * @throws AddressException
088: * @throws MessagingException
089: */
090: public static void send(String from, String to, String subject,
091: String body, String contentType) throws AddressException,
092: MessagingException {
093:
094: Session session = Session.getInstance(System.getProperties(),
095: getAuthenticator());
096:
097: // create a message
098: MimeMessage msg = new MimeMessage(session);
099: if (contentType == null)
100: contentType = "text/plain";
101:
102: //msg.setText(body);
103: msg.setContent(body, contentType + ";charset=UTF-8");
104: msg.setFrom(new InternetAddress(from));
105: msg.setSubject(subject, "UTF-8");
106: // set the Date: header
107: msg.setSentDate(new Date());
108:
109: String[] addresses = StringUtil.split(to, ",");
110: InternetAddress[] address = new InternetAddress[addresses.length];
111: for (int i = 0; i < addresses.length; i++) {
112: address[i] = new InternetAddress(addresses[i]);
113: }
114: msg.setRecipients(Message.RecipientType.TO, address);
115:
116: Transport.send(msg);
117: // send the message
118: //sendMessage(msg, session);
119: }
120:
121: /**
122: * Send a plain text email to a list of email addresses.
123: * If the body contains non US-ASCII characters,
124: * it will be encoded using platform's default charset.
125: * @param from The Sender email address.
126: * @param toList Vector of email addresses
127: * @param subject Email subject
128: * @param body Email message body
129: * @throws AddressException
130: * @throws MessagingException
131: */
132: public static void send(String from, Vector toList, String subject,
133: String body) throws AddressException, MessagingException {
134: send(from, toList, subject, body, null, null);
135: }
136:
137: /**
138: * Send a plain text email with an attachment to a list of email addresses.
139: * If filePath or fileName is null, only plain text email without attachment
140: * will be sent. If the body contains non US-ASCII characters,
141: * it will be encoded using platform's default charset.
142: * @param from The Sender email address.
143: * @param toList Vector of email addresses.
144: * @param subject Email subject.
145: * @param body Email message body.
146: * @param filePath Path to the file to be attached.
147: * @param fileName The file's name to be attached.
148: * @throws AddressException
149: * @throws MessagingException
150: */
151: public static void send(String from, Vector toList, String subject,
152: String body, String filePath, String fileName)
153: throws AddressException, MessagingException {
154: send(from, toList, subject, body, null, null, null);
155: }
156:
157: /**
158: * Send a plain text email with an attachment to a list of email addresses.
159: * If filePath or fileName is null, only plain text email without attachment
160: * will be sent. If the body contains non US-ASCII characters,
161: * it will be encoded using platform's default charset.
162: * @param from The Sender email address.
163: * @param toList Vector of email addresses.
164: * @param subject Email subject.
165: * @param body Email message body.
166: * @param filePath Path to the file to be attached.
167: * @param fileName The file's name to be attached.
168: * @param contentType Mime type of message body, i.e. text/html, text/plain, application/x-xxxxx. Default is text/plain.
169: * @throws AddressException
170: * @throws MessagingException
171: */
172: public static void send(String from, Vector toList, String subject,
173: String body, String filePath, String fileName,
174: String contentType) throws AddressException,
175: MessagingException {
176:
177: Session session = Session.getInstance(System.getProperties(),
178: getAuthenticator());
179:
180: // create a message
181: MimeMessage msg = new MimeMessage(session);
182: if (filePath == null || fileName == null) {
183: if (contentType == null)
184: msg.setText(body);
185: else
186: msg.setContent(body, contentType + ";charset=UTF-8");
187: } else {
188: // create the message part
189: MimeBodyPart messageBodyPart = new MimeBodyPart();
190:
191: //fill message
192: if (contentType == null)
193: messageBodyPart.setContent(body,
194: "text/plain;charset=UTF-8");
195: else
196: messageBodyPart.setContent(body, contentType
197: + ";charset=UTF-8");
198:
199: Multipart multipart = new MimeMultipart();
200: multipart.addBodyPart(messageBodyPart);
201:
202: // Part two is attachment
203: messageBodyPart = new MimeBodyPart();
204: DataSource source = new FileDataSource(filePath);
205: messageBodyPart.setDataHandler(new DataHandler(source));
206: messageBodyPart.setFileName(fileName);
207: multipart.addBodyPart(messageBodyPart);
208:
209: // Put parts in message
210: msg.setContent(multipart);
211:
212: }
213:
214: msg.setFrom(new InternetAddress(from));
215: InternetAddress[] address = null;
216: Vector addressVec = new Vector();
217: for (int i = 0; i < toList.size(); i++) {
218: String addressStr = (String) toList.elementAt(i);
219: if (addressStr != null && (!addressStr.trim().equals(""))) {
220: addressVec.addElement(new InternetAddress(
221: (String) toList.elementAt(i)));
222: }
223: }
224:
225: if (addressVec.size() == 0)
226: return;
227: address = new InternetAddress[addressVec.size()];
228: addressVec.copyInto(address);
229: msg.setRecipients(Message.RecipientType.TO, address);
230: msg.setSubject(subject, "UTF-8");
231:
232: // set the Date: header
233: msg.setSentDate(new Date());
234:
235: Transport.send(msg);
236: //sendMessage(msg, session);
237: }
238:
239: private static Authenticator getAuthenticator() {
240: String passwd = System.getProperty(SMTP_PASSWORD_PROPERTY);
241: String smtpUser = System.getProperty(SMTP_USER_PROPERTY);
242: Authenticator auth = null;
243: if (passwd != null) {
244: auth = new DefaultMailAuthenticator(smtpUser, passwd);
245: }
246: return auth;
247: }
248:
249: private static void sendMessage(Message msg, Session session)
250: throws MessagingException {
251: String password = System.getProperty(SMTP_PASSWORD_PROPERTY);
252:
253: if (password == null) {
254: Transport.send(msg);
255: } else {
256: String smtpHost = System.getProperty(SMTP_HOST_PROPERTY);
257: String smtpUser = System.getProperty(SMTP_USER_PROPERTY);
258: Transport tr = session.getTransport("smtp");
259: tr.connect(smtpHost, smtpUser, password);
260: msg.saveChanges();
261: tr.sendMessage(msg, msg.getAllRecipients());
262: }
263: }
264:
265: }
|