package CorbaMail; import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import org.omg.CORBA.*; import java.net.*; public class MailServant extends CorbaMail._MailImplBase { private com.visigenic.vbroker.orb.ORB orbie; private boolean greenlight = true; //used for mail validation // super - for naming the orb public MailServant(com.visigenic.vbroker.orb.ORB orb, String objname) { super(objname); orbie = orb; } // end super // method being implemented in IDL public String SendMail(String FROM, String MESSAGE, String SUBJECT, String USERINFO) throws CorbaMail.MailError { String TO = "whip1472@www.whip.com"; String HOST = "127.0.0.1"; // loopback IP for localhost String from = FROM; String message = MESSAGE; String subject = SUBJECT; String userinfo = USERINFO; greenlight = true; //used for mail validation if (userinfo == null) { userinfo = "\n No information available about the sender."; } 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 - CorbaMail :"+subject); mess.setContent(message+"\n\n Sender Info:" +userinfo, "text/plain"); mess.setHeader("X-Mailer", "JavaMail API on CJs CorbaMail"); // 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 Corba."); 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) { throw new MailError ("#Your mail was NOT sent. #Something went horribly wrong. #"+e+" "+e.getMessage()+" #POSSIBLE CAUSES for this problem: #1. You provided an invalid FROM address. #2. The mail server may be down or has malfunctioned. #3. One of the java packages needed to run this #application was available when the application was created but could not be found at runtime. #This application needs the javax.activation and javax.mail packages to #run properly. The person administering this application should #check the classpath/settings and the availability of #these java packages."); } // 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; } } //end class