/* * @(#)EJBMailDaemonEJB.java 1.00 3/3/2000 */ /* ***************************************************************************** Copyright 2000 by Erica Andrews (cylikon@hotmail.com). All rights reserved. ./ButchWhipAppeal/ ./IcedPinky/ NO portion of this software may be used in ANY way by anyone other than myself without my express written consent. If it is downloaded for testing, it must be deleted from your system within 24 hours. Any other use constitutes piracy and fraudulent misuse of this software, punishable in a court of law. Copyright 2000 by Erica Andrews. All rights reserved. ***************************************************************************** */ package cylikon.EJBMail; import javax.ejb.*; import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; /** * EJBMailDaemonEJB class. * @author Li'L CJ * @version 1.00 03/03/2000 */ public class EJBMailDaemonEJB implements SessionBean { public String TO = "whip1472@www.whip.com"; // a domain name only existant on my computer public String HOST = "127.0.0.1"; // loopback localhost private boolean greenlight = true; /** * A container invokes this method to start * the life of the session object. */ public void ejbCreate() throws CreateException { } /** * A container invokes this method before it ends * the life of the session object. */ public void ejbRemove() { } /** * The activate method is called when the instance * is activated from its "passive" state. */ public void ejbActivate() { // This method won't get called for stateless bean } /** * The passivate method is called before the instance * enters the "passive" state. */ public void ejbPassivate() { // This method won't get called for stateless bean } /** * Set the associated session context. */ public void setSessionContext(SessionContext ctx) { } // actual Mail method public String SendMail(String from, String subject, String message, String userinfo) { String FROM = from; String SUBJECT = subject; String MESSAGE = message; String USERINFO = userinfo; boolean greenlight = true; // used for simple FROM mail validation if (USERINFO == null) { USERINFO = "No information available about the sender."; } // validation and sending the message // doing some very simple message validation // do we have a valid address to send TO? // do we have a valid address to send FROM? // do we a message in the body of the email? if (TO == null || FROM == null || MESSAGE == null) { setGreenLight(false); // if any of these fields are blank NO greenlight, the mail will not be sent if (TO == null) { return "No TO address specified. We can't send this mail.";} // will not happen if (FROM == null) { return "No FROM address provided. We cannot send this mail. Try again, please.";} if (MESSAGE == null) { return "No MESSAGE provided. No empty emails, please. Type your message, then try again."; } } // all FROM addresses should contain a period (.) and an @ symbol. // Also, no mail should have an empty FROM string, so let's check for that as well if (FROM != null) { if (FROM.indexOf("@") == -1 ) { setGreenLight(false); // mail won't be sent return "Sorry, the FROM address you provided is invalid.\nThis mail cannot be sent."; } if (FROM.indexOf(".") == -1) { setGreenLight(false); // mail won't be sent return "Sorry, the FROM address you provided is invalid.\nThis mail cannot be sent."; } if (FROM.equals("")) { setGreenLight(false); // mail won't be sent return "Sorry, the FROM address you provided is invalid.\nThis mail cannot be sent."; } } // checking for empty messages if (MESSAGE != null) { if (MESSAGE.equals("")) { setGreenLight(false); // mail won't be sent return "No MESSAGE provided.\nNo empty emails, please.\nType your message, then try again."; } } // if the greenlight boolean equals false for any reason, the mail will not be sent... if (getGreenLight() == false) { // do nothing } else { // if we have the basics, let's try to send the mail try { // constructing the message Session s = Session.getDefaultInstance(System.getProperties(),null); MimeMessage m = new MimeMessage(s); InternetAddress t = new InternetAddress(TO); InternetAddress f = new InternetAddress(FROM); m.addRecipients(Message.RecipientType.TO, new InternetAddress[]{t }); // TO addresss m.setFrom(f); // the FROM address m.setSubject("CJs IIS Site Comments: "+SUBJECT); // message subject m.setContent(MESSAGE+"\n\n Sender Info:"+USERINFO, "text/plain"); // message body m.setHeader("X-Mailer", "JavaMail API on EJB"); m.addHeaderLine("Email message sent using the JavaMail API on CJs IIS website with Enterpise JavaBeans."); m.addHeaderLine("For more useful Java stuff, visit ./ButchWhipAppeal/"); // -- setting mail Priority for "high" in case the user is reporting site problems m.setHeader("x-priority", "1"); m.setHeader("x-msmail-priority", "high"); Transport.send(m); // error handling } catch (AddressException ae) { return "Server Error: Problem sending mail...\n" +ae+"\n"+ae.getMessage()+"\nYou may have provided an invalid FROM address.";} catch (MessagingException me) { return "Server Error: Problem sending mail...\n" +me+"\n"+me.getMessage()+"\nThe mail server may be down.";} //end of Try-Catch } // end of If-Else statement return "\nEnd of Mail Session:\nMessage successfully sent to the site owner. "; // will only be returned if the mail was sent successfully. Otherwise, error Strings will be returned. } // end SendMail method // greenlight boolean handlers public void setGreenLight(boolean b) { greenlight = b; this.greenlight = greenlight; } public boolean getGreenLight() { return greenlight; } } // end of Class