1 /** 2 * This Java Source Code and its associated Class files are 3 * <P> 4 * (c) 1998 Battelle Pacific Northwest National Laboratories 5 * <P> 6 * For further information, contact : 7 * <P> 8 * Chris Parkinson 9 * Environmental Molecular Sciences Laboratory 10 * Battelle Pacific Northwest National Laboratories 11 * Richland 12 * WA 99352 13 * USA 14 */ 15 16 17 import java.awt.*; 18 import java.io.*; 19 import java.net.*; 20 import java.util.*; 21 import java.text.*; 22 23 /** 24 * 25 * Communicate Mail class is the abstract class that provide basic 26 * SMPT (for outgoing) and POP3 (for incoming) communication methods. 27 * 28 * This class is subclasses by the SendMail and ReceiveMail classes, which make 29 * use of these general methods. 30 * <p> 31 * 32 * @version 2.0 August 1998 33 * @author Chris Parkinson 34 */ 35 public abstract class CommunicateMail extends CommunicateGUI 36 { 37 38 private int mailPort; //SMPT PORT - should usually be 25 39 private String mailServer; //Host name or IP address 40 41 private Socket socket; //Socket to talk to host 42 private BufferedReader dataInput; //Receive from sockets 43 private PrintWriter dataOutput; //Send to socket 44 45 46 //////////////////////////////////////////////////////// 47 // 48 // Constructor 49 // 50 //////////////////////////////////////////////////////// 51 /** 52 * Construct an empty communicator 53 */ CommunicateMail(Frame parentFrame)54 public CommunicateMail(Frame parentFrame) { 55 super(parentFrame); 56 } 57 58 //////////////////////////////////////////////////////// 59 // 60 // Set and Get the mail port number 61 // 62 //////////////////////////////////////////////////////// 63 /** 64 * Set the mail port number. The default and usual port to use for 65 * SMTP connections is 25, and for POP3 is 110 66 * <P> 67 * @param portNumber the port to use for SMTP or POP3 communcations. 68 */ setMailPort(int portNumber)69 public void setMailPort(int portNumber) { 70 mailPort = portNumber; 71 } 72 73 /** 74 * Get the mail port number. The default and usual port to use for 75 * SMTP connections is 25, and for POP3 is 110 76 * <P> 77 * @return the port to use for SMTP or POP3 communcations. 78 */ getMailPort()79 public int getMailPort() { 80 return mailPort; 81 } 82 83 84 //////////////////////////////////////////////////////// 85 // 86 // Set the mail server (a string of name or number) 87 // 88 //////////////////////////////////////////////////////// 89 /** 90 * Set the mail server for either POP3 or SMTP 91 * <P> 92 * @param the name (or IP address) of the outgoing SMTP mail server or 93 * incoming POP3 server. 94 */ setMailServer(String server)95 public void setMailServer(String server) { 96 mailServer = new String(server); 97 } 98 99 /** 100 * Get the mail server for either POP3 or SMTP 101 * <P> 102 * @return the name (or IP address) of the outgoing SMTP mail server or 103 * incoming POP3 server. 104 */ getMailServer()105 public String getMailServer() { 106 return mailServer; 107 } 108 109 110 111 112 113 114 115 116 117 118 119 //////////////////////////////////////////////////////// 120 // 121 // Send a message to the mail server 122 // 123 //////////////////////////////////////////////////////// 124 /** 125 * Send a single message to the server. The message automatically has 126 * a CRLF pair added to the end of the string. 127 * <P> 128 * @param command the command to send 129 * @param data optional data to send with the command (message = command + ' ' + data) 130 */ sendToServer( String command, String data, boolean report)131 public void sendToServer( String command, String data, boolean report) { 132 String message = new String(command); 133 if (data!=null)message = message + " " + data; 134 if (report)setProgress(message); 135 try { 136 dataOutput.print(message + "\r\n"); 137 dataOutput.flush(); 138 } 139 catch(Exception e) { setError("Could not write to mail server"); } 140 } 141 142 143 //////////////////////////////////////////////////////// 144 // 145 // Receive a message from the server and check it 146 // against our anticipated code 147 // 148 //////////////////////////////////////////////////////// 149 /** 150 * Receive a message from the server, and check the response from the server 151 * using the given 'expectedResponse'. If the response is as expected, this will 152 * return the whole input line. If not it returns NULL and throws an error 153 * <P> 154 * @param expectedResponse the expected reponse string 155 * @return the message received, or NULL if error 156 */ receiveFromServer(String expectedResponse)157 public String receiveFromServer(String expectedResponse) { 158 String input = getServerLine(true); 159 if (input==null)input=""; 160 if (input.startsWith(expectedResponse))return input; 161 setError(input); 162 return null; 163 } 164 165 166 //////////////////////////////////////////////////////// 167 // 168 // Read a Line of Data from the server 169 // 170 //////////////////////////////////////////////////////// 171 /** 172 * Read a line of data from the server, optionally waiting for a line 173 * or returning if one is not ready. 174 * <P> 175 * @param waitForReply ... if true this method will block until a line of data 176 * is ready to read from the server (or a time out occurs). If this is false and there 177 * is no data ready to be read, the method will return a NULL. 178 */ getServerLine(boolean waitForReply)179 public String getServerLine(boolean waitForReply) { 180 try { 181 if (waitForReply==false && dataInput.ready()==false)return null; 182 String result = dataInput.readLine(); 183 return result; 184 }catch(Exception e) { 185 if (waitForReply)setError("Could not read from mail server"); 186 } 187 return null; 188 } 189 190 191 192 193 194 //////////////////////////////////////////////////////// 195 // 196 // Open up a connection to the server 197 // 198 //////////////////////////////////////////////////////// 199 /** 200 * Open Connection to the Server. Tries to open a connection to the 201 * port and host as already set. 202 * <P> 203 */ openConnection()204 public void openConnection() { 205 try{ 206 socket = new Socket(mailServer, mailPort); 207 socket.setSoTimeout(1000*10); 208 dataInput = new BufferedReader(new InputStreamReader(socket.getInputStream())); 209 dataOutput = new PrintWriter(socket.getOutputStream()); 210 } 211 catch (Exception e){ 212 setError("Can't connect to mail server " + e.toString()); 213 } 214 } 215 216 //////////////////////////////////////////////////////// 217 // 218 // Close connection to server 219 // 220 //////////////////////////////////////////////////////// 221 /** 222 * Close Connection to the Server 223 */ closeConnection()224 public void closeConnection() { 225 try { 226 dataOutput.close(); 227 dataInput.close(); 228 socket.close(); 229 }catch(Exception e) {} 230 } 231 232 233 234 235 236 237 238 239 //////////////////////////////////////////////////////// 240 // 241 // Convert large string into a Multi Line string array 242 // 243 //////////////////////////////////////////////////////// 244 /** 245 * Convert a large message into a multi-line message suitable 246 * for use in the email system. 247 * Whenever a CR/LF is found in the message, it is split into a new 248 * line. If a line exists with just a single '.' in it, it is converted 249 * to a '. ' so that it is not recognised as an END OF MESSAGE signal. 250 * <P> 251 * @param message the multiline message to split 252 * @return an array of String 253 */ toMultiLine(String message)254 public String[] toMultiLine(String message) { 255 Vector lineList = new Vector(); 256 try { 257 StringReader reader = new StringReader(message); 258 BufferedReader lineReader = new BufferedReader(reader); 259 String aLine=null; 260 do { 261 aLine = lineReader.readLine(); 262 if (aLine!=null) { 263 if (aLine.equals("."))aLine = ". "; 264 lineList.addElement(aLine); 265 } 266 }while(aLine!=null); 267 } catch(Exception e) {} 268 String[] results = new String[lineList.size()]; 269 lineList.copyInto(results); 270 return results; 271 } 272 273 //////////////////////////////////////////////////////// 274 // 275 // Tokenize a String into component words 276 // 277 //////////////////////////////////////////////////////// 278 /** 279 * Convert a line of text into words, split by white spaces 280 * <P> 281 * @param message the multiword message to split 282 * @return an array of String words 283 */ toMultiWord(String message)284 public String[] toMultiWord(String message) { 285 if (message==null)message=""; 286 287 Vector wordList = new Vector(); 288 StringTokenizer st = new StringTokenizer(message); 289 while (st.hasMoreTokens()) { 290 wordList.addElement(st.nextToken()); 291 } 292 String[] results = new String[wordList.size()]; 293 wordList.copyInto(results); 294 return results; 295 } 296 297 298 299 300 301 302 303 304 305 306 } 307 308