001: package vqwiki;
002:
003: import org.apache.log4j.Logger;
004:
005: import javax.mail.Message;
006: import javax.mail.MessagingException;
007: import javax.mail.Session;
008: import javax.mail.Transport;
009: import javax.mail.internet.InternetAddress;
010: import javax.mail.internet.MimeMessage;
011: import java.util.Date;
012: import java.util.Properties;
013:
014: /**
015: * Sends mail via SMTP to the specified host. <b>REDISTRIBUTION:</b> you
016: * will either have to hard-code your own SMTP host name into the constructor
017: * function and recompile, or rewrite the Environment class to record
018: * this information in the vqwiki.properties file.
019: *
020: * @author Robert E Brewer
021: * @version 0.1
022: */
023: public class WikiMail {
024:
025: private static final Logger logger = Logger
026: .getLogger(WikiMail.class);
027: private Session session;
028: private static WikiMail instance;
029:
030: /**
031: * Construct the object by opening a JavaMail session. Use getInstance to provide Singleton behavior.
032: */
033: public WikiMail() {
034: Properties props = System.getProperties();
035: props.setProperty("mail.smtp.host", Environment.getInstance()
036: .getStringSetting(Environment.PROPERTY_SMTP_HOST));
037: if (Environment.getInstance().getStringSetting(
038: Environment.PROPERTY_SMTP_USERNAME).equals("")) {
039: session = Session.getInstance(props, null);
040: } else {
041: props.setProperty("mail.smtp.auth", "true");
042: session = Session.getInstance(props,
043: new WikiMailAuthenticator());
044: }
045: }
046:
047: /**
048: * Provide a Singleton instance of the object.
049: */
050: public static WikiMail getInstance() {
051: if (instance == null)
052: instance = new WikiMail();
053: return instance;
054: }
055:
056: /**
057: * Send mail via SMTP. MessagingExceptions are silently dropped.
058: *
059: * @param from the RFC 821 "MAIL FROM" parameter
060: * @param to the RFC 821 "RCPT TO" parameter
061: * @param subject the RFC 822 "Subject" field
062: * @param body the RFC 822 "Body" field
063: */
064: public void sendMail(String from, String to, String subject,
065: String body) {
066: try {
067: MimeMessage message = new MimeMessage(session);
068: InternetAddress internetAddress = new InternetAddress(from);
069: message.setFrom(internetAddress);
070: message
071: .setReplyTo(new InternetAddress[] { internetAddress });
072: message.setRecipient(Message.RecipientType.TO,
073: new InternetAddress(to));
074: message.setSubject(subject);
075: message.setText(body);
076: message.setSentDate(new Date());
077: message.saveChanges();
078: if (!Environment.getInstance().getStringSetting(
079: Environment.PROPERTY_SMTP_USERNAME).equals("")
080: && !!Environment.getInstance().getStringSetting(
081: Environment.PROPERTY_SMTP_PASSWORD).equals(
082: "")) {
083: String username = Environment.getInstance()
084: .getStringSetting(
085: Environment.PROPERTY_SMTP_USERNAME);
086: String password = Environment.getInstance()
087: .getStringSetting(
088: Environment.PROPERTY_SMTP_PASSWORD);
089: String smtphost = Environment.getInstance()
090: .getStringSetting(
091: Environment.PROPERTY_SMTP_HOST);
092:
093: Transport tr = session.getTransport("smtp");
094: tr.connect(smtphost, username, password);
095: tr.sendMessage(message, message.getAllRecipients());
096: tr.close();
097: } else {
098: Transport.send(message);
099: }
100: } catch (MessagingException e) {
101: logger.warn("Mail error", e);
102: }
103: }
104:
105: /**
106: *
107: */
108: public static void init() {
109: instance = null;
110: }
111: }
|