Changeset 247 for trunk/src/org/expeditee/agents/mail/MailSession.java
- Timestamp:
- 08/22/08 10:54:44 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/agents/mail/MailSession.java
r242 r247 1 1 package org.expeditee.agents.mail; 2 2 3 import java.awt.Color; 4 import java.awt.Point; 3 5 import java.io.BufferedReader; 4 6 import java.io.InputStream; 5 7 import java.io.InputStreamReader; 8 import java.util.Collection; 6 9 import java.util.Date; 10 import java.util.LinkedList; 7 11 import java.util.Properties; 8 12 … … 18 22 import javax.mail.Transport; 19 23 import javax.mail.Flags.Flag; 24 import javax.mail.event.MessageCountAdapter; 25 import javax.mail.event.MessageCountEvent; 20 26 import javax.mail.internet.InternetAddress; 21 27 import javax.mail.internet.MimeMessage; … … 23 29 import org.expeditee.gui.AttributeValuePair; 24 30 import org.expeditee.gui.Frame; 31 import org.expeditee.gui.FrameCreator; 25 32 import org.expeditee.gui.MessageBay; 33 import org.expeditee.importer.FrameDNDTransferHandler; 34 import org.expeditee.items.Item; 26 35 import org.expeditee.items.Text; 27 36 28 37 public class MailSession { 29 public static boolean _autoConnect = true;38 public static boolean _autoConnect = false; 30 39 31 40 private static MailSession _theMailSession = null; … … 45 54 private String _password; 46 55 47 private String _popServer; 56 private String _mailServer; 57 58 private String _serverType; 48 59 49 60 private Boolean _bConnecting; … … 56 67 _username = null; 57 68 _password = ""; 58 _popServer = null; 69 _mailServer = null; 70 _serverType = null; 59 71 60 72 // Set the settings 61 73 for (Text item : settingsFrame.getBodyTextItems(false)) { 74 if (item.getText().toLowerCase().trim().equals("autoconnect")) { 75 _autoConnect = true; 76 } 62 77 AttributeValuePair avp = new AttributeValuePair(item.getText()); 63 78 if (!avp.hasPair()) … … 81 96 props.setProperty("mail.smtp.auth", "true"); 82 97 } else if (attribute.equals("popserver")) { 83 _popServer = avp.getValue(); 98 _mailServer = avp.getValue(); 99 _serverType = "pop3s"; 100 props.setProperty("mail.pop3.host", _mailServer); 101 } else if (attribute.equals("imapserver")) { 102 _mailServer = avp.getValue(); 103 _serverType = "imaps"; 104 props.setProperty("mail.imap.host", _mailServer); 84 105 } 85 106 } … … 97 118 try { 98 119 // Set up the mail receiver 99 _store = _session.getStore( "pop3s");120 _store = _session.getStore(_serverType); 100 121 101 122 // Connect the mail sender … … 133 154 _bConnecting = true; 134 155 _transport.connect(); 135 MessageBay.overwriteMessage("SMTP server connected"); 156 MessageBay.overwriteMessage("SMTP server connected", 157 Color.green); 136 158 } else { 137 159 MessageBay.warningMessage("SMTP server already connected"); … … 141 163 } 142 164 143 if (_ popServer != null && !_store.isConnected()) {165 if (_mailServer != null && !_store.isConnected()) { 144 166 try { 145 MessageBay.displayMessage("Connecting to POP3 server..."); 146 _store.connect(_popServer, _username, _password); 167 MessageBay.displayMessage("Connecting to " + _mailServer 168 + "..."); 169 _store.connect(_mailServer, _username, _password); 147 170 148 171 // -- Try to get hold of the default folder -- … … 153 176 _folder = _folder.getFolder("INBOX"); 154 177 if (_folder == null) 155 throw new Exception("No POP3INBOX");178 throw new Exception("No INBOX"); 156 179 // -- Open the folder for read only -- 157 _folder.open(Folder.READ_ONLY); 158 MessageBay.overwriteMessage("POP3 server connected"); 159 MessageBay.warningMessage(getUnreadMessageCount() + " unread messages are in your inbox"); 180 _folder.open(Folder.READ_WRITE); 181 _folder.addMessageCountListener(new MessageCountAdapter() { 182 @Override 183 public void messagesAdded(MessageCountEvent e) { 184 MessageBay.displayMessage("New mail message!", 185 Color.green); 186 displayUnreadMailCount(); 187 } 188 }); 189 190 new Thread() { 191 public void run() { 192 for (;;) { 193 try { 194 Thread.sleep(1000); // sleep for freq 195 // milliseconds 196 // This is to force the IMAP server to send us 197 // EXISTS notifications 198 // TODO: Is synchronisation needed? 199 _folder.getMessageCount(); 200 } catch (Exception e) { 201 } 202 } 203 } 204 }.start(); 205 206 MessageBay.overwriteMessage("Connection complete", Color.GREEN); 207 208 displayUnreadMailCount(); 209 160 210 } catch (Exception e) { 161 211 // e.printStackTrace(); 162 MessageBay.errorMessage("Error connecting to POP3 server");212 MessageBay.errorMessage("Error connecting to " + _mailServer); 163 213 } 164 214 } 165 215 _bConnecting = false; 216 } 217 218 private void displayUnreadMailCount() { 219 Text text = new Text(-1, getUnreadCount() + " unread message" 220 + (getUnreadCount() == 1 ? "" : "s"), Color.BLUE, null); 221 text.addAction("getUnreadMail"); 222 MessageBay.displayMessage(text); 166 223 } 167 224 … … 287 344 } 288 345 289 public int getUnreadMessageCount() { 290 // -- Get the message wrappers and process them -- 291 Message[] msgs; 292 int count = 0; 293 try { 294 msgs = _folder.getMessages(); 295 for (int msgNum = 0; msgNum < msgs.length; msgNum++) { 296 if (msgs[msgNum].getFlags().contains(Flag.SEEN)) 297 continue; 298 count++; 299 } 346 public int getUnreadCount() { 347 try { 348 return _folder.getUnreadMessageCount(); 300 349 } catch (MessagingException e) { 301 350 // TODO Auto-generated catch block 302 351 e.printStackTrace(); 303 352 } 304 return count; 305 } 306 307 public String getMail() { 353 return 0; 354 } 355 356 /** 357 * Gets mail and puts a list of mail items with links to the messages 358 * content. TODO: Put reply and forward button on the frame... 359 * 360 * @param flag 361 * @param isPresent 362 * @param frame 363 * @param point 364 * @return 365 */ 366 public String getMailString(Flag flag, Boolean isPresent) { 308 367 StringBuffer sb = new StringBuffer(); 309 368 // -- Get the message wrappers and process them -- … … 312 371 msgs = _folder.getMessages(); 313 372 for (int msgNum = 0; msgNum < msgs.length; msgNum++) { 314 if (msgs[msgNum].getFlags().contains(Flag.SEEN)) 315 continue; 316 // Only get messages that have not been read 317 sb.append(getTextMessage(msgs[msgNum])); 318 sb.append('\n'); 373 374 if (flag == null 375 || msgs[msgNum].getFlags().contains(flag) == isPresent) { 376 if (sb.length() > 0) { 377 sb.append('\n').append('\n').append( 378 "-----------------------------").append('\n') 379 .append('\n'); 380 } 381 // Only get messages that have not been read 382 sb.append(getTextMessage(msgs[msgNum])); 383 } 319 384 } 320 385 } catch (MessagingException e) { 321 386 e.printStackTrace(); 322 387 } 323 sb.deleteCharAt(sb.length() - 1);324 388 return sb.toString(); 389 } 390 391 /** 392 * Gets mail and puts a list of mail items with links to the messages 393 * content. TODO: Put reply and forward button on the frame... 394 * 395 * @param flag 396 * @param isPresent 397 * @param frame 398 * @param point 399 * @return 400 */ 401 public Collection<Item> getMail(Flag flag, Boolean isPresent, Frame frame, 402 Point point) { 403 Collection<Item> mailItems = new LinkedList<Item>(); 404 // -- Get the message wrappers and process them -- 405 Message[] msgs; 406 try { 407 msgs = _folder.getMessages(); 408 for (int msgNum = 0; msgNum < msgs.length; msgNum++) { 409 if (flag == null 410 || msgs[msgNum].getFlags().contains(flag) == isPresent) { 411 Item newItem = readMessage(msgs[msgNum], msgNum, frame, point); 412 if (newItem != null) { 413 mailItems.add(newItem); 414 point.y += newItem.getBoundsHeight(); 415 } 416 } 417 418 } 419 } catch (MessagingException e) { 420 e.printStackTrace(); 421 } 422 return mailItems; 423 } 424 425 private Item readMessage(final Message message, int messageNo, Frame frame, Point point) { 426 String subject; 427 try { 428 subject = message.getSubject(); 429 } catch (MessagingException e) { 430 MessageBay.errorMessage("GetMail error: " + e.getMessage()); 431 return null; 432 } 433 434 final Text source = FrameDNDTransferHandler 435 .importString(subject, point); 436 // Create a frameCreator 437 final FrameCreator frames = new FrameCreator(frame.getFramesetName(), 438 frame.path, messageNo + ". " + subject, false); 439 440 new Thread() { 441 public void run() { 442 try { 443 // Get the header information 444 String from = ((InternetAddress) message.getFrom()[0]) 445 .getPersonal(); 446 if( from != null){ 447 frames.addText("@fromName: " + from, null, null, null, false); 448 } 449 from = ((InternetAddress) message.getFrom()[0]) 450 .getAddress(); 451 frames.addText("@fromAddress: " + from, null, null, null, false); 452 453 // -- Get the message part (i.e. the message itself) -- 454 Part messagePart = message; 455 Object content = messagePart.getContent(); 456 // -- or its first body part if it is a multipart 457 // message -- 458 if (content instanceof Multipart) { 459 messagePart = ((Multipart) content).getBodyPart(0); 460 // System.out.println("[ Multipart Message ]"); 461 } 462 // -- Get the content type -- 463 String contentType = messagePart.getContentType().toLowerCase(); 464 // -- If the content is plain text, we can print it -- 465 // System.out.println("CONTENT:" + contentType); 466 if (contentType.startsWith("text/plain") 467 || contentType.startsWith("text/html")) { 468 InputStream is = messagePart.getInputStream(); 469 BufferedReader reader = new BufferedReader( 470 new InputStreamReader(is)); 471 String thisLine = reader.readLine(); 472 while (thisLine != null) { 473 frames.addText(thisLine, null, null, null, false); 474 thisLine = reader.readLine(); 475 } 476 } 477 message.setFlag(Flag.SEEN, true); 478 479 frames.save(); 480 source.setLink(frames.getName()); 481 } catch (Exception e) { 482 MessageBay.errorMessage("GetMail error: " + e.getMessage()); 483 } 484 } 485 }.start(); 486 return source; 325 487 } 326 488 … … 330 492 public String getTextMessage(Message message) { 331 493 StringBuffer sb = new StringBuffer(); 332 494 333 495 try { 334 496 // Get the header information … … 337 499 if (from == null) 338 500 from = ((InternetAddress) message.getFrom()[0]).getAddress(); 339 sb.append("FROM: " + from) ;501 sb.append("FROM: " + from).append('\n'); 340 502 String subject = message.getSubject(); 341 sb.append("SUBJECT: " + subject) ;503 sb.append("SUBJECT: " + subject).append('\n').append('\n'); 342 504 // -- Get the message part (i.e. the message itself) -- 343 505 Part messagePart = message; … … 351 513 String contentType = messagePart.getContentType(); 352 514 // -- If the content is plain text, we can print it -- 353 // System.out.println("CONTENT:" + contentType);515 // System.out.println("CONTENT:" + contentType); 354 516 if (contentType.startsWith("text/plain") 355 517 || contentType.startsWith("text/html")) { … … 359 521 String thisLine = reader.readLine(); 360 522 while (thisLine != null) { 361 sb.append(thisLine) ;523 sb.append(thisLine).append('\n'); 362 524 thisLine = reader.readLine(); 363 525 } 364 526 } 365 sb.append("-----------------------------");527 message.setFlag(Flag.SEEN, true); 366 528 } catch (Exception ex) { 367 529 ex.printStackTrace(); 368 530 } 531 sb.deleteCharAt(sb.length() - 1); 369 532 return sb.toString(); 370 533 } 534 535 public Folder getFolder() { 536 return _folder; 537 } 371 538 }
Note:
See TracChangeset
for help on using the changeset viewer.