/**
* Copyright (c) 1996-2003 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: *
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