/** * Copyright (c) 1996-2005 Cafesoft, LLC. All Rights Reserved. * * This software is the confidential and proprietary information of * Cafesoft, LLC. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Cafesoft. * * CAFESOFT MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. CAFESOFT SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ package examples.service; import com.cafesoft.core.service.AbstractService; import com.cafesoft.core.service.ServiceConfig; import com.cafesoft.core.service.ServiceException; import com.cafesoft.core.smtp.SmtpClient; import com.cafesoft.core.smtp.UnknownUserException; import java.net.UnknownHostException; import java.io.PrintStream; import java.io.IOException; /** * SmtpTextNotifierService implements a Cams service that sends a * textual message to one or more e-mail addresses. This service implements * the TextNotifierService interface so that it can be looked up * via a Cam service manager by that type. This service can be interchanged * with any other service that implements the TextNotifierService type. *

* Configuration parameters for this service include: *

* * @version $Revision: 1.3 $ $Date: 2005/04/28 23:30:24 $ * @author Norbert K. Kuhnert */ public class SmtpTextNotifierService extends AbstractService implements TextNotifierService { /** The host to which SMTP messages will be sent. */ private String smtpHost = null; /** The e-mail address to which messages will be sent. */ private String smtpTo = null; /** * Create a new SmtpTextNotifierService instance. */ public SmtpTextNotifierService() { } /** * Override the initialize method to check for required config parameters. * * @param config the object through which configuration parameters and * other resources are available. * @exception ServiceException if one or more configuration parameters * are missing. */ public void initialize(ServiceConfig config) throws ServiceException { // Initialize state in the abstract base class super.initialize(config); // Complain if required parameters are missing this.smtpHost = config.getInitParameter("smtp.host"); if ((smtpHost == null) || (smtpHost.trim().length() == 0)) throw new ServiceException("Required parameter: 'smtp.host' " + "is missing or empty"); this.smtpTo = config.getInitParameter("smtp.to"); if ((smtpTo == null) || (smtpTo.trim().length() == 0)) throw new ServiceException("Required parameter: 'smtp.to' " + "is missing or empty"); } /** * Send a textual message. * * @param from the message sender. * @param subject the subject of the message. * @param body the body of the textual message. */ public void sendText(String from, String subject, String body) { // Sending an SMTP message is slow, so create a new Thread and // send the message asynchronously. SmtpClientRunnable r = new SmtpClientRunnable(from, subject, body); Thread t = new Thread(r); t.start(); } /** * Destroy the service. */ public void destroy() { // null local Object references. this.smtpHost = null; this.smtpTo = null; // Invoke the super class destroy method. super.destroy(); } // // inner classes // /** * SmtpClientRunnable implements a Runnable class * for asynchronously sending a message to an SMTP server. */ private class SmtpClientRunnable implements Runnable { private String from; private String subject; private String body; /** * Create a new ManagerLoginNofifierRunnable. * * @param from the message sender. * @param subject the subject of the message. * @param body the body of the textual message. */ public SmtpClientRunnable(String from, String subject, String body) { this.from = from; this.subject = subject; this.body = body; } // // implementing Runnable // /** * Send the text message via SMTP. */ public void run() { try { if (debug) logger.debug(this, "Connecting to SMTP server=" + smtpHost + ", from=" + from + ", to=" + smtpTo + ", subject=" + subject); // Setup the mail transport host SmtpClient c = new SmtpClient(smtpHost, false); c.from(from); c.to(smtpTo); // Send the message PrintStream ps = c.startMessage(); ps.print("To: " + smtpTo + "\n"); ps.print("From: " + from + "\n"); ps.print("Subject: " + subject + "\n"); ps.print("X-Client: com.cafesoft.core.smtp.SmtpClient" + "\n"); ps.print("\n"); ps.print(body); ps.print("\n"); // Close connection with the SMTP server c.closeServer(); if (debug) logger.debug(this, "Successfully sent text message"); } catch (UnknownHostException e) { logger.error(this, "Unknown SMTP server host=" + smtpHost, e); } catch (UnknownUserException e) { logger.error(this, "Unknown message recipient=" + smtpTo, e); } catch (IOException e) { logger.error(this, "Error sending message", e); } } } // End of class: SmtpClientRunnable } // End of class: SmtpTextNotifierService