javamailgateway.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,639 行 · 第 1/5 页
JAVA
1,639 行
* @param message the JavaMail message. * @return String the subject of the message. */ protected String getSubject(Message message) { try { String[] subjects = message.getHeader("Subject"); String subject; if (subjects != null && subjects.length > 0) { subject = subjects[0]; return getRFC2047DecodedString(subject); } else { String err = "Subject header is unavailable or its value is absent"; throw new MessagingException(err); } } catch (MessagingException me) { System.err.println("Error retrieving subject: " + me.toString()); return ""; } } /** * Utility method to decode strings that may have rfc 2047 encoded words * in them. * * @param str the string to decode. * @return the decoded string. * @throws ParseException if an error occurs parsing the string. */ protected String getRFC2047DecodedString(String str) throws ParseException { String charset = "ISO8859_1"; if (str == null) { return ""; } if (str.indexOf("=?") != -1 && str.indexOf("?=") != -1) { StringBuffer temp = new StringBuffer(str.length()); String encodedWord = ""; int i = 0, last = 0; try { while (str.indexOf("=?", i) != -1 && i < str.length()) { int first = str.indexOf("=?", i); if (first != i) { // White space between adjacent 'encoded-word's // is not displayed. for (int x = last; x < first; x++) { if (str.charAt(x) != ' ' || i == 0) { temp.append(str.substring(last, first)); break; } } } int c = 0; i = first + 2; while (c < 2 && str.indexOf("?", i) != -1) { i = str.indexOf("?", i) + 1; c++; } last = str.indexOf("?=", i); if (last > 0 && last <= str.length()) { last += 2; encodedWord = str.substring(first, last); // handle common b0rken charset - I think this is correct int firstQ = encodedWord.indexOf('?'); int nextQ = encodedWord.indexOf('?', firstQ + 1); if (firstQ >= 0 && nextQ > firstQ) { charset = encodedWord.substring(firstQ + 1, nextQ); if (charset.equalsIgnoreCase("hz-gb-2312")) { charset = "gb2312"; encodedWord = "=?" + charset + encodedWord.substring(nextQ); } } // A common problem is that spaces are left unencoded // where they should be encoded as per the character // set being used. We'll be lenient with this failure to // respect the specification. if (encodedWord.indexOf(' ') != -1) { // get the encoding method used String encoding = encodedWord.substring(firstQ, firstQ+1); int index; // for each occurrence, replace the broken space // character with it's encoded equivalent while ((index = encodedWord.indexOf(' ')) != -1) { String encodedChar = getEncodedSpace(charset, encoding.toUpperCase()); encodedWord = encodedWord.substring(0, index) + encodedChar + ((index < encodedWord.length() - 1) ? encodedWord.substring(index + 1) : ""); } } // urgh - Undeclared exceptions are a pain try { temp.append(MimeUtility.decodeWord(encodedWord)); } catch (StringIndexOutOfBoundsException e) { System.err.println("Exception thrown decoding " + encodedWord); } i = last; } else { // broken encoding throw new ParseException(); } } if (last < str.length()) { temp.append(str.substring(last)); } str = temp.toString(); } catch (ParseException e) { System.err.println("ParseException thrown, encodedWord was " + encodedWord + ", string was " + str); } catch (UnsupportedEncodingException e) { System.err.println("UnsupportedEncodingException thrown, encodedWord was " + encodedWord + ", encoding was " + charset); // reset the encoding charset = "ISO8859_1"; } } // flip encoding try { str = new String(str.getBytes(charset), "ISO8859_1"); } catch (IOException e) { System.err.println("Exception thrown translating character sets for string " + str); } return str; } protected String getEncodedSpace(String charset, String encoding) throws UnsupportedEncodingException { String encoded = ""; byte[] bytes = " ".getBytes(MimeUtility.javaCharset(charset)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream os; if ("B".equals(encoding)) { os = new BEncoderStream(baos); } else { os = new QEncoderStream(baos, true); } try { os.write(bytes); os.close(); } catch (IOException ioe) { /* ignore */ } byte[] encodedBytes = baos.toByteArray(); for (int i = 0; i < encodedBytes.length; i++) { encoded += (char) encodedBytes[i]; } return encoded; } /** * Returns the date a message was created or received, or the current date * if date parsing fails. POP3 is notorious for improperly formatted date * fields and doesn't provide a separate received date field. In many cases * JavaMail fails to parse these dates, so we attempt to use our own * parsers as backup.<p> * * If the parsed date is in the future, we return the current date. * * @param message a JavaMail message. * @return Date the sent (or received) date of the message. * @throws MessagingException if an error occurred trying to retrieve the date. */ protected Date getDate(Message message) throws MessagingException { Date date = null; message = (MimeMessage) message; // First, let JavaMail try to parse the date. try { date = message.getSentDate(); // If it failed, try the received date if (date == null) { date = message.getReceivedDate(); } } catch (MessagingException me) { /* ignore */ } catch (NullPointerException npe) { /* ignore */ } // Rarely, there will be a date that JavaMail fails to parse. In this // case, we'll attempt to parse the date ourselves using some of the // more common date formats that JavaMail doesn't recognize. if (date == null) { String[] headers = message.getHeader("Date"); // If no headers exist, there is no chance we can parse ourselves so // return the current date. if (headers == null || headers[0] == null) { return new Date(); } String header = headers[0].trim(); // Parse date in form: Tues Jan 17 21:10:14 1978 try { if (format1 == null) { format1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"); format1.setLenient(true); } date = format1.parse(header); } catch (java.text.ParseException e1) { } // Parse date in form: Wednesday, June 27, 2001 if (date == null) { try { if (format2 == null) { format2 = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); format2.setLenient(true); } date = format2.parse(header); } catch (java.text.ParseException e2) { } } // Parse date in form: 11/23/97 11:20 PM if (date == null) { try { if (format3 == null) { format3 = new SimpleDateFormat("dd/MM/yy hh:mm a"); format3.setLenient(true); } date = format3.parse(header); } catch (java.text.ParseException e3) { } } // Parse date in form: 1999-03-06 17:22:05 if (date == null) { try { if (format4 == null) { format4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); format4.setLenient(true); } date = format4.parse(header); } catch (java.text.ParseException e4) { } } // Parse date in form: Fri, 17 May, 1974 09:13:57 if (date == null) { try { if (format5 == null) { format5 = new SimpleDateFormat("EEE, dd MMM, yyyy HH:mm:ss"); format5.setLenient(true); } date = format5.parse(header); } catch (java.text.ParseException e5) { } } // Parse date in form: November 15, 1978 08:13 PM if (date == null) { try { if (format6 == null) { format6 = new SimpleDateFormat("MMMM dd, yyyy HH:mm a"); format6.setLenient(true); } date = format6.parse(header); } catch (java.text.ParseException e6) { } } } if (date == null || date.compareTo(new Date()) > 0) { // All parsing failed or date was in the future // return current date return new Date(); } else { // return parsed date return date; } } /** * Retrieve the messageID using the JavaMail provided method. * * @param message the JavaMail message. * @return String the messageID. * @throws MessagingException if an error occurred trying to retrieve the * messageID. */ protected String getMessageID(Message message) throws MessagingException { String messageID = ((MimeMessage) message).getMessageID(); if (messageID != null && !messageID.equals("")) { return messageID; } else { String err = "Message-ID header is unavailable or its value is absent"; throw new MessagingException(err); } } /** * Returns the the parent message id's of a message. Currently, two headers * are given for threading purposes, In-Reply-To: and References: * In-Reply-To contains the msg id's of all the messages for which * this message is a reply (And for which is almost universally only one), * References is used to identify a thread of conversation.<p> * * See RFC 822/2822 for a full explanation. Note that this implementation * doesn't handle CFWS, particularly the Comment part. Shoot me. * (RFC 2822 Section 3.6.4) * * @param message the JavaMail message. * @return String the parent message id (empty if not fo
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?