1 38 39 import java.io.*; 40 import java.util.*; 41 import java.text.*; 42 43 import javax.servlet.*; 44 import javax.servlet.http.*; 45 import javax.mail.*; 46 import javax.mail.internet.*; 47 import javax.activation.*; 48 49 50 68 public class JavaMailServlet extends HttpServlet implements SingleThreadModel { 69 String protocol = "imap"; 70 String mbox = "INBOX"; 71 72 73 80 public void doPost(HttpServletRequest req, HttpServletResponse res) 81 throws ServletException, IOException { 82 83 HttpSession ssn = req.getSession(true); 85 86 String send = req.getParameter("send"); 87 String host = req.getParameter("hostname"); 88 String user = req.getParameter("username"); 89 String passwd = req.getParameter("password"); 90 URLName url = new URLName(protocol, host, -1, mbox, user, passwd); 91 92 ServletOutputStream out = res.getOutputStream(); 93 res.setContentType("text/html"); 94 out.println("<html><body bgcolor=\"#CCCCFF\">"); 95 96 if (send != null) { 97 send(req, res, out, ssn); 99 100 } else { 101 103 MailUserData mud = new MailUserData(url); 105 ssn.putValue("javamailservlet", mud); 106 107 try { 108 Properties props = System.getProperties(); 109 props.put("mail.smtp.host", host); 110 Session session = Session.getDefaultInstance(props, null); 111 session.setDebug(false); 112 Store store = session.getStore(url); 113 store.connect(); 114 Folder folder = store.getDefaultFolder(); 115 if (folder == null) 116 throw new MessagingException("No default folder"); 117 118 folder = folder.getFolder(mbox); 119 if (folder == null) 120 throw new MessagingException("Invalid folder"); 121 122 folder.open(Folder.READ_WRITE); 123 int totalMessages = folder.getMessageCount(); 124 Message[] msgs = folder.getMessages(); 125 FetchProfile fp = new FetchProfile(); 126 fp.add(FetchProfile.Item.ENVELOPE); 127 folder.fetch(msgs, fp); 128 129 System.out.println("Login from: " + store.getURLName()); 131 132 mud.setSession(session); 134 mud.setStore(store); 135 mud.setFolder(folder); 136 137 out.print("<center>"); 139 out.print("<font face=\"Arial,Helvetica\" font size=+3>"); 140 out.println("<b>Welcome to JavaMail!</b></font></center><p>"); 141 142 out.println("<table width=\"50%\" border=0 align=center>"); 144 out.print("<tr><td width=\"75%\" bgcolor=\"#ffffcc\">"); 146 out.print("<font face=\"Arial,Helvetica\" font size=-1>"); 147 out.println("<b>FolderName</b></font></td><br>"); 148 out.print("<td width=\"25%\" bgcolor=\"#ffffcc\">"); 150 out.print("<font face=\"Arial,Helvetica\" font size=-1>"); 151 out.println("<b>Messages</b></font></td><br>"); 152 out.println("</tr>"); 153 out.print("<tr><td width=\"75%\" bgcolor=\"#ffffff\">"); 155 out.print("<a HREF=\"" + HttpUtils.getRequestURL(req) + "\">" + 156 "Inbox" + "</a></td><br>"); 157 out.println("<td width=\"25%\" bgcolor=\"#ffffff\">" + 159 totalMessages + "</td>"); 160 out.println("</tr>"); 161 out.println("</table"); 162 } catch (Exception ex) { 163 out.println(ex.toString()); 164 } finally { 165 out.println("</body></html>"); 166 out.close(); 167 } 168 } 169 } 170 171 172 175 public void doGet (HttpServletRequest req, HttpServletResponse res) 176 throws ServletException, IOException { 177 178 HttpSession ses = req.getSession(false); ServletOutputStream out = res.getOutputStream(); 180 MailUserData mud = getMUD(ses); 181 182 if (mud == null) { 183 res.setContentType("text/html"); 184 out.println("<html><body>Please Login (no session)</body></html>"); 185 out.close(); 186 return; 187 } 188 189 if (!mud.getStore().isConnected()) { 190 res.setContentType("text/html"); 191 out.println("<html><body>Not Connected To Store</body></html>"); 192 out.close(); 193 return; 194 } 195 196 197 201 String msgStr = req.getParameter("message"); 203 String logout = req.getParameter("logout"); 204 String compose = req.getParameter("compose"); 205 String part = req.getParameter("part"); 206 int msgNum = -1; 207 int partNum = -1; 208 209 if (msgStr != null) { 211 msgNum = Integer.parseInt(msgStr); 213 214 if (part == null) { 215 res.setContentType("text/html"); 217 displayMessage(mud, req, out, msgNum); 218 219 } else if (part != null) { 220 partNum = Integer.parseInt(part); 222 displayPart(mud, msgNum, partNum, out, res); 223 } 224 225 } else if (compose != null) { 226 compose(mud, res, out); 228 229 } else if (logout != null) { 230 try { 232 mud.getFolder().close(false); 233 mud.getStore().close(); 234 ses.invalidate(); 235 out.println("<html><body>Logged out OK</body></html>"); 236 } catch (MessagingException mex) { 237 out.println(mex.toString()); 238 } 239 240 } else { 241 displayHeaders(mud, req, out); 243 } 244 } 245 246 247 private void displayMessage(MailUserData mud, HttpServletRequest req, 248 ServletOutputStream out, int msgNum) 249 throws IOException { 250 251 out.println("<html>"); 252 out.println("<HEAD><TITLE>JavaMail Servlet</TITLE></HEAD>"); 253 out.println("<BODY bgcolor=\"#ccccff\">"); 254 out.print("<center><font face=\"Arial,Helvetica\" "); 255 out.println("font size=\"+3\"><b>"); 256 out.println("Message " + (msgNum+1) + " in folder " + 257 mud.getStore().getURLName() + 258 "/INBOX</b></font></center><p>"); 259 260 try { 261 Message msg = mud.getFolder().getMessage(msgNum); 262 263 displayMessageHeaders(mud, msg, out); 265 266 Object o = msg.getContent(); 268 269 if (msg.isMimeType("text/plain")) { 271 out.println("<pre>"); 272 out.println((String )o); 273 out.println("</pre>"); 274 } else if (msg.isMimeType("multipart/*")) { 276 Multipart mp = (Multipart)o; 277 int cnt = mp.getCount(); 278 for (int i = 0; i < cnt; i++) { 279 displayPart(mud, msgNum, mp.getBodyPart(i), i, req, out); 280 } 281 } else { 282 out.println(msg.getContentType()); 283 } 284 285 } catch (MessagingException mex) { 286 out.println(mex.toString()); 287 } 288 289 out.println("</BODY></html>"); 290 out.close(); 291 } 292 293 299 private void displayPart(MailUserData mud, int msgNum, Part part, 300 int partNum, HttpServletRequest req, 301 ServletOutputStream out) 302 throws IOException { 303 304 if (partNum != 0) 305 out.println("<p><hr>"); 306 307 try { 308 309 String sct = part.getContentType(); 310 if (sct == null) { 311 out.println("invalid part"); 312 return; 313 } 314 ContentType ct = new ContentType(sct); 315 316 if (partNum != 0) 317 out.println("<b>Attachment Type:</b> " + 318 ct.getBaseType() + "<br>"); 319 320 if (ct.match("text/plain")) { 321 out.println("<pre>"); 323 out.println((String )part.getContent()); 324 out.println("</pre>"); 325 326 } else { 327 String s; 329 if ((s = part.getFileName()) != null) 330 out.println("<b>Filename:</b> " + s + "<br>"); 331 s = null; 332 if ((s = part.getDescription()) != null) 333 out.println("<b>Description:</b> " + s + "<br>"); 334 335 out.println("<a HREF=\"" + 336 HttpUtils.getRequestURL(req) + 337 "?message=" + 338 msgNum + "&part=" + 339 partNum + "\">Display Attachment</a>"); 340 } 341 } catch (MessagingException mex) { 342 out.println(mex.toString()); 343 } 344 } 345 346 352 private void displayPart(MailUserData mud, int msgNum, 353 int partNum, ServletOutputStream out, 354 HttpServletResponse res) 355 throws IOException { 356 357 Part part = null; 358 359 try { 360 Message msg = mud.getFolder().getMessage(msgNum); 361 362 Multipart mp = (Multipart)msg.getContent(); 363 part = mp.getBodyPart(partNum); 364 365 String sct = part.getContentType(); 366 if (sct == null) { 367 out.println("invalid part"); 368 return; 369 } 370 ContentType ct = new ContentType(sct); 371 372 res.setContentType(ct.getBaseType()); 373 InputStream is = part.getInputStream(); 374 int i; 375 while ((i = is.read()) != -1) 376 out.write(i); 377 out.flush(); 378 out.close(); 379 } catch (MessagingException mex) { 380 out.println(mex.toString()); 381 } 382 } 383 384 388 private void displayMessageHeaders(MailUserData mud, Message msg, 389 ServletOutputStream out) 390 throws IOException { 391 392 try { 393 out.println("<b>Date:</b> " + msg.getSentDate() + "<br>"); 394 395 Address[] fr = msg.getFrom(); 396 if (fr != null) { 397 boolean tf = true; 398 out.print("<b>From:</b> "); 399 for (int i = 0; i < fr.length; i++) { 400 out.print(((tf) ? " " : ", ") + getDisplayAddress(fr[i])); 401 tf = false; 402 } 403 out.println("<br>"); 404 } 405 406 Address[] to = msg.getRecipients(Message.RecipientType.TO); 407 if (to != null) { 408 boolean tf = true; 409 out.print("<b>To:</b> "); 410 for (int i = 0; i < to.length; i++) { 411 out.print(((tf) ? " " : ", ") + getDisplayAddress(to[i])); 412 tf = false; 413 } 414 out.println("<br>"); 415 } 416 417 Address[] cc = msg.getRecipients(Message.RecipientType.CC); 418 if (cc != null) { 419 boolean cf = true; 420 out.print("<b>CC:</b> "); 421 for (int i = 0; i < cc.length; i++) { 422 out.print(((cf) ? " " : ", ") + getDisplayAddress(cc[i])); 423 cf = false; 424 } 425 out.println("<br>"); 426 } 427 428 out.print("<b>Subject:</b> " + 429 ((msg.getSubject() !=null) ? msg.getSubject() : "") + 430 "<br>"); 431 432 } catch (MessagingException mex) { 433 out.println(msg.toString()); 434 } 435 } 436 437 441 private void displayHeaders(MailUserData mud, 442 HttpServletRequest req, 443 ServletOutputStream out) 444 throws IOException { 445 446 SimpleDateFormat df = new SimpleDateFormat("EE M/d/yy"); 447 448 out.println("<html>"); 449 out.println("<HEAD><TITLE>JavaMail Servlet</TITLE></HEAD>"); 450 out.println("<BODY bgcolor=\"#ccccff\"><hr>"); 451 out.print("<center><font face=\"Arial,Helvetica\" font size=\"+3\">"); 452 out.println("<b>Folder " + mud.getStore().getURLName() + 453 "/INBOX</b></font></center><p>"); 454 455 out.println("<font face=\"Arial,Helvetica\" font size=\"+3\"><b>"); 457 out.println("<a HREF=\"" + 458 HttpUtils.getRequestURL(req) + 459 "?logout=true\">Logout</a>"); 460 out.println("<a HREF=\"" + 461 HttpUtils.getRequestURL(req) + 462 "?compose=true\" target=\"compose\">Compose</a>"); 463 out.println("</b></font>"); 464 out.println("<hr>"); 465 466 out.print("<table cellpadding=1 cellspacing=1 "); out.println("width=\"100%\" border=1>"); 470 out.println("<tr><td width=\"25%\" bgcolor=\"ffffcc\">"); 472 out.println("<font face=\"Arial,Helvetica\" font size=\"+1\">"); 473 out.println("<b>Sender</b></font></td>"); 474 out.println("<td width=\"15%\" bgcolor=\"ffffcc\">"); 476 out.println("<font face=\"Arial,Helvetica\" font size=\"+1\">"); 477 out.println("<b>Date</b></font></td>"); 478 out.println("<td bgcolor=\"ffffcc\">"); 480 out.println("<font face=\"Arial,Helvetica\" font size=\"+1\">"); 481 out.println("<b>Subject</b></font></td></tr>"); 482 483 try { 484 Folder f = mud.getFolder(); 485 int msgCount = f.getMessageCount(); 486 Message m = null; 487 for (int i = 1; i <= msgCount; i++) { 489 m = f.getMessage(i); 490 491 if (m.isSet(Flags.Flag.DELETED)) 493 continue; 494 495 out.println("<tr valigh=middle>"); 497 out.print("<td width=\"25%\" bgcolor=\"ffffff\">"); 498 out.println("<font face=\"Arial,Helvetica\">" + 499 ((m.getFrom() != null) ? 500 m.getFrom()[0].toString() : 501 "" ) + 502 "</font></td>"); 503 504 out.print("<td nowrap width=\"15%\" bgcolor=\"ffffff\">"); 506 out.println("<font face=\"Arial,Helvetica\">" + 507 df.format((m.getSentDate()!=null) ? 508 m.getSentDate() : m.getReceivedDate()) + 509 "</font></td>"); 510 511 out.print("<td bgcolor=\"ffffff\">"); 513 out.println("<font face=\"Arial,Helvetica\">" + 514 "<a HREF=\"" + 515 HttpUtils.getRequestURL(req) + 516 "?message=" + 517 i + "\">" + 518 ((m.getSubject() != null) ? 519 m.getSubject() : 520 "<i>No Subject</i>") + 521 "</a>" + 522 "</font></td>"); 523 out.println("</tr>"); 524 } 525 } catch (MessagingException mex) { 526 out.println("<tr><td>" + mex.toString() + "</td></tr>"); 527 mex.printStackTrace(); 528 } 529 530 out.println("</table>"); 531 out.println("</BODY></html>"); 532 out.flush(); 533 out.close(); 534 } 535 536 540 private void compose(MailUserData mud, HttpServletResponse res, 541 ServletOutputStream out) 542 throws IOException { 543 544 res.setContentType("text/html"); 545 out.println(composeForm); 546 out.close(); 547 } 548 549 552 private void send(HttpServletRequest req, HttpServletResponse res, 553 ServletOutputStream out, HttpSession ssn) 554 throws IOException { 555 556 String to = req.getParameter("to"); 557 String cc = req.getParameter("cc"); 558 String subj = req.getParameter("subject"); 559 String text = req.getParameter("text"); 560 561 try { 562 MailUserData mud = getMUD(ssn); 563 if (mud == null) 564 throw new Exception ("trying to send, but not logged in"); 565 566 Message msg = new MimeMessage(mud.getSession()); 567 InternetAddress[] toAddrs = null, ccAddrs = null; 568 569 if (to != null) { 570 toAddrs = InternetAddress.parse(to, false); 571 msg.setRecipients(Message.RecipientType.TO, toAddrs); 572 } else 573 throw new MessagingException("No \"To\" address specified"); 574 575 if (cc != null) { 576 ccAddrs = InternetAddress.parse(cc, false); 577 msg.setRecipients(Message.RecipientType.CC, ccAddrs); 578 } 579 580 if (subj != null) 581 msg.setSubject(subj); 582 583 URLName u = mud.getURLName(); 584 msg.setFrom(new InternetAddress(u.getUsername() + "@" + 585 u.getHost())); 586 587 if (text != null) 588 msg.setText(text); 589 590 Transport.send(msg); 591 592 out.println("<h1>Message sent successfully</h1></body></html>"); 593 out.close(); 594 595 } catch (Exception mex) { 596 out.println("<h1>Error sending message.</h1>"); 597 out.println(mex.toString()); 598 out.println("<br></body></html>"); 599 } 600 } 601 602 603 private String getDisplayAddress(Address a) { 605 String pers = null; 606 String addr = null; 607 if (a instanceof InternetAddress && 608 ((pers = ((InternetAddress)a).getPersonal()) != null)) { 609 610 addr = pers + " "+"<"+((InternetAddress)a).getAddress()+">"; 611 } else 612 addr = a.toString(); 613 614 return addr; 615 } 616 617 private MailUserData getMUD(HttpSession ses) throws IOException { 620 MailUserData mud = null; 621 622 if (ses == null) { 623 return null; 624 } else { 625 if ((mud = (MailUserData)ses.getValue("javamailservlet")) == null){ 626 return null; 627 } 628 } 629 return mud; 630 } 631 632 633 public String getServletInfo() { 634 return "A mail reader servlet"; 635 } 636 637 641 private static String composeForm = "<HTML><HEAD><TITLE>JavaMail Compose</TITLE></HEAD><BODY BGCOLOR=\"#CCCCFF\"><FORM ACTION=\"/servlet/JavaMailServlet\" METHOD=\"POST\"><input type=\"hidden\" name=\"send\" value=\"send\"><P ALIGN=\"CENTER\"><B><FONT SIZE=\"4\" FACE=\"Verdana, Arial, Helvetica\">JavaMail Compose Message</FONT></B><P><TABLE BORDER=\"0\" WIDTH=\"100%\"><TR><TD WIDTH=\"16%\" HEIGHT=\"22\"> <P ALIGN=\"RIGHT\"><B><FONT FACE=\"Verdana, Arial, Helvetica\">To:</FONT></B></TD><TD WIDTH=\"84%\" HEIGHT=\"22\"><INPUT TYPE=\"TEXT\" NAME=\"to\" SIZE=\"30\"> <FONT SIZE=\"1\" FACE=\"Verdana, Arial, Helvetica\"> (separate addresses with commas)</FONT></TD></TR><TR><TD WIDTH=\"16%\"><P ALIGN=\"RIGHT\"><B><FONT FACE=\"Verdana, Arial, Helvetica\">CC:</FONT></B></TD><TD WIDTH=\"84%\"><INPUT TYPE=\"TEXT\" NAME=\"cc\" SIZE=\"30\"> <FONT SIZE=\"1\" FACE=\"Verdana, Arial, Helvetica\"> (separate addresses with commas)</FONT></TD></TR><TR><TD WIDTH=\"16%\"><P ALIGN=\"RIGHT\"><B><FONT FACE=\"Verdana, Arial, Helvetica\">Subject:</FONT></B></TD><TD WIDTH=\"84%\"><INPUT TYPE=\"TEXT\" NAME=\"subject\" SIZE=\"55\"></TD></TR><TR><TD WIDTH=\"16%\"> </TD><TD WIDTH=\"84%\"><TEXTAREA NAME=\"text\" ROWS=\"15\" COLS=\"53\"></TEXTAREA></TD></TR><TR><TD WIDTH=\"16%\" HEIGHT=\"32\"> </TD><TD WIDTH=\"84%\" HEIGHT=\"32\"><INPUT TYPE=\"SUBMIT\" NAME=\"Send\" VALUE=\"Send\"><INPUT TYPE=\"RESET\" NAME=\"Reset\" VALUE=\"Reset\"></TD></TR></TABLE></FORM></BODY></HTML>"; 642 643 } 644 645 646 650 class MailUserData { 651 URLName url; 652 Session session; 653 Store store; 654 Folder folder; 655 656 public MailUserData(URLName urlname) { 657 url = urlname; 658 } 659 660 public URLName getURLName() { 661 return url; 662 } 663 664 public Session getSession() { 665 return session; 666 } 667 668 public void setSession(Session s) { 669 session = s; 670 } 671 672 public Store getStore() { 673 return store; 674 } 675 676 public void setStore(Store s) { 677 store = s; 678 } 679 680 public Folder getFolder() { 681 return folder; 682 } 683 684 public void setFolder(Folder f) { 685 folder = f; 686 } 687 } 688 | Popular Tags |