001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.mail;
017:
018: import java.util.Date;
019: import java.util.Properties;
020: import javax.mail.Message;
021: import javax.mail.MessagingException;
022: import javax.mail.Session;
023: import javax.mail.Transport;
024: import javax.mail.internet.InternetAddress;
025: import javax.mail.internet.MimeMessage;
026: import org.jamwiki.Environment;
027: import org.jamwiki.utils.WikiLogger;
028:
029: /**
030: * Sends mail via SMTP to the specified host. <b>REDISTRIBUTION:</b> you
031: * will either have to hard-code your own SMTP host name into the constructor
032: * function and recompile, or rewrite the Environment class to record
033: * this information in the jamwiki.properties file.
034: */
035: public class WikiMail {
036:
037: private static final WikiLogger logger = WikiLogger
038: .getLogger(WikiMail.class.getName());
039: private final Session session;
040: private static WikiMail instance;
041:
042: /**
043: * Construct the object by opening a JavaMail session. Use getInstance to provide Singleton behavior.
044: */
045: public WikiMail() {
046: Properties props = System.getProperties();
047: props.setProperty("mail.smtp.host", Environment
048: .getValue(Environment.PROP_EMAIL_SMTP_HOST));
049: if (Environment.getValue(Environment.PROP_EMAIL_SMTP_USERNAME)
050: .equals("")) {
051: session = Session.getInstance(props, null);
052: } else {
053: session = Session.getInstance(props,
054: new WikiMailAuthenticator());
055: }
056: }
057:
058: /**
059: * Provide a Singleton instance of the object.
060: */
061: public static WikiMail getInstance() {
062: if (instance == null) {
063: instance = new WikiMail();
064: }
065: return instance;
066: }
067:
068: /**
069: * Send mail via SMTP. MessagingExceptions are silently dropped.
070: *
071: * @param from the RFC 821 "MAIL FROM" parameter
072: * @param to the RFC 821 "RCPT TO" parameter
073: * @param subject the RFC 822 "Subject" field
074: * @param body the RFC 822 "Body" field
075: */
076: public void sendMail(String from, String to, String subject,
077: String body) {
078: try {
079: MimeMessage message = new MimeMessage(session);
080: InternetAddress internetAddress = new InternetAddress(from);
081: message.setFrom(internetAddress);
082: message
083: .setReplyTo(new InternetAddress[] { internetAddress });
084: message.setRecipient(Message.RecipientType.TO,
085: new InternetAddress(to));
086: message.setSubject(subject);
087: message.setText(body);
088: message.setSentDate(new Date());
089: message.saveChanges();
090: Transport.send(message);
091: } catch (MessagingException e) {
092: logger.warning("Mail error", e);
093: }
094: }
095:
096: /**
097: *
098: */
099: public static void init() {
100: instance = null;
101: }
102: }
|