001: /**
002: *
003: * Bonita
004: * Copyright (C) 1999 Bull S.A.
005: * Bull 68 route de versailles 78434 Louveciennes Cedex France
006: * Further information: bonita@objectweb.org
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
021: * USA
022: *
023: *
024: --------------------------------------------------------------------------
025: * $Id: MailNotification.java,v 1.4 2006/04/24 06:39:34 brice Exp $
026: *
027: --------------------------------------------------------------------------
028: */package hero.util;
029:
030: import java.io.Serializable;
031:
032: import javax.mail.Address;
033: import javax.mail.Session;
034: import javax.mail.Transport;
035: import javax.mail.internet.InternetAddress;
036: import javax.mail.internet.MimeMessage;
037: import java.util.Vector;
038:
039: public final class MailNotification implements Serializable,
040: java.lang.Cloneable {
041:
042: private Session session;
043: public final static String CHARACTER_ENCODING = "UTF-8";
044:
045: public void initMailService() {
046: try {
047: BonitaServiceLocator serviceLocator = BonitaServiceLocator
048: .getInstance();
049: session = (Session) serviceLocator
050: .getMailSession(BonitaServiceLocator.Services.MAIL_SERVICE);
051: } catch (BonitaServiceException e) {
052: e.printStackTrace();
053: }
054: }
055:
056: public void sendMail(String toAccount, String title, String body)
057: throws Exception {
058: try {
059: this .initMailService();
060:
061: MimeMessage m = new MimeMessage(session);
062: m.setFrom();
063: Address[] to = new InternetAddress[] { new InternetAddress(
064: toAccount) };
065: m.setRecipients(javax.mail.Message.RecipientType.TO, to);
066: m.setSubject(title);
067: m.setSentDate(new java.util.Date());
068: m.setContent(body, "text/plain;charset="
069: + CHARACTER_ENCODING);
070: Transport.send(m);
071: } catch (javax.mail.MessagingException e) {
072: System.out.println("mail-service.xml configuration error: "
073: + e);
074: }
075: }
076:
077: public void sendMail(Vector toAccount, String title, String body)
078: throws Exception {
079: try {
080: this .initMailService();
081:
082: MimeMessage m = new MimeMessage(session);
083: m.setFrom();
084: for (int i = 0; i < toAccount.size(); i++) {
085: Address to = new InternetAddress((String) toAccount
086: .elementAt(i));
087: m.addRecipient(javax.mail.Message.RecipientType.TO, to);
088: }
089: m.setSubject(title);
090: m.setSentDate(new java.util.Date());
091: m.setContent(body, "text/plain;charset="
092: + CHARACTER_ENCODING);
093: Transport.send(m);
094: } catch (javax.mail.MessagingException e) {
095: System.out.println("mail-service.xml configuration error: "
096: + e);
097: }
098: }
099:
100: public MailNotification() {
101: }
102:
103: public Object clone() throws java.lang.CloneNotSupportedException {
104: return super.clone();
105: }
106:
107: }
|