/* ***************************************************************************** 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. ***************************************************************************** */ import java.rmi.*; import javax.mail.*; import java.lang.SecurityManager; import java.rmi.server.*; import java.rmi.registry.*; import javax.mail.internet.*; import java.net.*; public class RMIMailServer extends UnicastRemoteObject implements RMIMail { String TO = "whip1472@www.whip.com"; String HOST = "127.0.0.1"; private boolean greenlight = true; // for mail validation purposes public RMIMailServer() throws RemoteException { } // nothing special done with Super // The actual SendMail method public String SendMail(String FROM, String MESSAGE, String SUBJECT, String USERINFO) throws AddressException, MessagingException { String from = FROM; String message = MESSAGE; String subject = SUBJECT; String userinfo = USERINFO; greenlight = true; if (userinfo == null) { userinfo = "No information available about the sender."; } // Now, let's do some simple mail validation before trying to send try { // **** First let's do some simple mail validation to prevent the unfriendly looking // MessagingException or AddressException that could be thrown by the JavaMail API. // If something major is wrong with the email, these checks will probably catch it // first and display a more friendly error message. 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."; } } // Now that all the checks have been conducted, let's check the status of the greenlight // boolean. If it is false, mail will not be sent. Otherwise, mail will be sent as // normal. if (getGreenLight() == false) { // do nothing - the error string will have already been // sent back to the client. } else { // let's send the mail // First constructing the message from the 4 strings provided in SendMail contructor Session sess = Session.getDefaultInstance(System.getProperties(),null); MimeMessage mess = new MimeMessage(sess); InternetAddress t = new InternetAddress(TO); InternetAddress f = new InternetAddress(from); mess.addRecipients(Message.RecipientType.TO,new InternetAddress[]{t}); mess.setFrom(f); mess.setSubject("CJs Site Comments - RMIMail :"+subject); mess.setContent(message+"\n\n Sender Info:" +userinfo, "text/plain"); mess.setHeader("X-Mailer", "JavaMail API on CJs RMIMail"); // The following header lines may not be visible in all mail clients. For instance, // Netscape mail requires a "view source" in order to see these headers. mess.addHeaderLine("Email message sent using the JavaMail API on CJs IIS website with RMI."); mess.addHeaderLine("For more useful Java stuff, visit ./ButchWhipAppeal/"); // -- setting mail Priority for "high" in case the user is reporting site problems mess.setHeader("x-priority", "1"); mess.setHeader("x-msmail-priority", "high"); // now that our message has been created, let's send it Transport.send(mess); } // end if-else } catch (Throwable e) { return "An Error Occurred: "+e+"\n"+e.getMessage(); } // This is the String the user will see only if the mail is sent successfully. Otherwise, // one of the error strings earlier in the code will be returned instead. return "\nEnd of Mail Session: \nYour mail successfully sent to the site owner."; } //end SendMail method // methods used for setting the greenlight boolean public void setGreenLight(boolean b) { greenlight = b; this.greenlight = greenlight; } public boolean getGreenLight() { return greenlight; } // Main Method -- Let's bind the object public static void main (String args[]) { RMIMailServer ms; try { /* if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } */ LocateRegistry.createRegistry(999); ms = new RMIMailServer(); Naming.rebind("//whip1472:999/MailServer",ms); System.out.println("\n RMI Success!: The object has been created and bound under the name MailServer on port 999"); } catch (Exception e) { System.out.println("An Error Occurred in Main method of RMIMailServer: "+e+e.getMessage()); System.exit(0); } } // end Main } // end Class