📄 messageutilities.java
字号:
InputStream xis = part.getInputStream(); // transfer decoded only // pick the character set off of the content type ContentType xct = MessageUtilities.getContentType( part ); String xjcharset = xct.getParameter( "charset" ); if ( xjcharset != null ) { xjcharset = MimeUtility.javaCharset( xjcharset ); } else { // not present, assume ASCII character encoding xjcharset = MimeUtility.javaCharset( "ASCII" ); } // now construct a reader from the decoded stream // NOTE // Sun has a HUGE bug in their io code. They use a cute coding trick // that appends the charset to a prefix to create a Class name that // they then attempt to load. The problem is that if the charset is // not one that they "recognize", and the name has something like a // dash character in it, the resulting Class name has an invalid // character in it. This results in an IllegalArgumentException // instead of the UnsupportedEncodingException that we expect. Thus, // we need to catch the undocumented exception. InputStreamReader inReader = null; try { inReader = new InputStreamReader( xis, xjcharset ); } catch ( UnsupportedEncodingException ex ) { inReader = null; } catch ( IllegalArgumentException ex ) { inReader = null; } if ( inReader == null ) { // This is the "bug" case, and we need to do something // that will at least put text in front of the user... xjcharset = MimeUtility.javaCharset( "ASCII" ); inReader = new InputStreamReader( xis, xjcharset ); } BufferedReader xreader = new BufferedReader( inReader ); return xreader; } catch( IOException xex ) { throw new MessagingException( xex.toString() ); } }//............................................................ /** * Get a textual description of a message. * This is a helper method for applications. * * @param msg The message to interogate * @return String containing the description of the message */ public static String getMessageDescription( Message msg ) throws MessagingException { StringBuffer xbuffer = new StringBuffer( 1024 ); MessageUtilities.getPartDescription( msg, xbuffer, "", true ); return xbuffer.toString(); } /** * Get a textual description of a part. * * @param part The part to interogate * @param buff a string buffer for the description * @param prefix a prefix for each line of the description * @param recurse boolean specifying wether to recurse through sub-parts or not * @return StringBuffer containing the description of the part */ public static StringBuffer getPartDescription( Part part, StringBuffer buf, String prefix, boolean recurse ) throws MessagingException { if ( buf == null ) return buf; ContentType xctype = MessageUtilities.getContentType( part ); String xvalue = xctype.toString(); buf.append( prefix ); buf.append( "Content-Type: " ); buf.append( xvalue ); buf.append( '\n' ); xvalue = part.getDisposition(); buf.append( prefix ); buf.append( "Content-Disposition: " ); buf.append( xvalue ); buf.append( '\n' ); xvalue = part.getDescription(); buf.append( prefix ); buf.append( "Content-Description: " ); buf.append( xvalue ); buf.append( '\n' ); xvalue = MessageUtilities.getFileName( part ); buf.append( prefix ); buf.append( "Content-Filename: " ); buf.append( xvalue ); buf.append( '\n' ); if ( part instanceof MimePart ) { MimePart xmpart = (MimePart)part; xvalue = xmpart.getContentID(); buf.append( prefix ); buf.append( "Content-ID: " ); buf.append( xvalue ); buf.append( '\n' ); String[] langs = xmpart.getContentLanguage(); if ( langs != null ) { buf.append( prefix ); buf.append( "Content-Language: " ); for ( int pi = 0 ; pi < langs.length ; ++pi ) { if ( pi > 0 ) buf.append( ", " ); buf.append( xvalue ); } buf.append( '\n' ); } xvalue = xmpart.getContentMD5(); buf.append( prefix ); buf.append( "Content-MD5: " ); buf.append( xvalue ); buf.append( '\n' ); xvalue = xmpart.getEncoding(); buf.append( prefix ); buf.append( "Content-Encoding: " ); buf.append( xvalue ); buf.append( '\n' ); } buf.append( '\n' ); if ( recurse && xctype.match( "multipart/*" ) ) { Multipart xmulti = (Multipart)MessageUtilities.getPartContent( part ); int xparts = xmulti.getCount(); for ( int xindex = 0 ; xindex < xparts ; xindex ++ ) { MessageUtilities.getPartDescription( xmulti.getBodyPart( xindex ), buf, (prefix + " "), true ); } } return buf; } /** * Get the content dispostion of a part. * The part is interogated for a valid content disposition. If the * content disposition is missing, a default disposition is created * based on the type of the part. * * @see javax.mail.Part * @param part The part to interogate * @return ContentDisposition of the part */ public static ContentDisposition getContentDisposition( Part part ) throws MessagingException { String xheaders[] = part.getHeader( "Content-Disposition" ); try { if ( xheaders != null ) { return new ContentDisposition( xheaders[0] ); } } catch ( ParseException xex ) { throw new MessagingException( xex.toString() ); } // set default disposition based on part type if ( part instanceof MimeBodyPart ) { return new ContentDisposition( "attachment" ); } return new ContentDisposition( "inline" ); } /** * A 'safe' version of JavaMail getContentType(), i.e. don't throw exceptions. * The part is interogated for a valid content type. If the content type is * missing or invalid, a default content type of "text/plain" is assumed, * which is suggested by the MIME standard. * * @see javax.mail.Part * @param part The part to interogate * @return ContentType of the part */ public static ContentType getContentType( Part part ) { String xtype = null; try { xtype = part.getContentType(); } catch ( MessagingException xex ) { } if ( xtype == null ) { xtype = "text/plain"; // MIME default content type if missing } ContentType xctype = null; try { xctype = new ContentType( xtype.toLowerCase() ); } catch ( ParseException xex ) { } if ( xctype == null ) { xctype = new ContentType( "text", "plain", null ); } return xctype; } /** * Determin if the message is high-priority. * * @param message the message to examine * @return true if the message is high-priority */ public static boolean isHighPriority( Message message ) throws MessagingException { if ( message instanceof MimeMessage ) { MimeMessage xmime = (MimeMessage)message; String xpriority = xmime.getHeader( "Importance", null ); if ( xpriority != null ) { xpriority = xpriority.toLowerCase(); if ( xpriority.indexOf( "high" ) == 0 ) { return true; } } // X Standard: X-Priority: 1 | 2 | 3 | 4 | 5 (lowest) xpriority = xmime.getHeader( "X-Priority", null ); if ( xpriority != null ) { xpriority = xpriority.toLowerCase(); if ( xpriority.indexOf( "1" ) == 0 || xpriority.indexOf( "2" ) == 0 ) { return true; } } } return false; } /** * Determin if the message is low-priority. * * @param message the message to examine * @return true if the message is low-priority */ public static boolean isLowPriority( Message message ) throws MessagingException { if ( message instanceof MimeMessage ) { MimeMessage xmime = (MimeMessage)message; String xpriority = xmime.getHeader( "Importance", null ); if ( xpriority != null ) { xpriority = xpriority.toLowerCase(); if ( xpriority.indexOf( "low" ) == 0 ) { return true; } } // X Standard: X-Priority: 1 | 2 | 3 | 4 | 5 (lowest) xpriority = xmime.getHeader( "X-Priority", null ); if ( xpriority != null ) { xpriority = xpriority.toLowerCase(); if ( xpriority.indexOf( "4" ) == 0 || xpriority.indexOf( "5" ) == 0 ) { return true; } } } return false; } /** * A 'safe' version of JavaMail getFileName() which doesn't throw exceptions. * Encoded filenames are also decoded if necessary. * Why doesn't JAVA Mail do this? * * @see javax.mail.Part * @param part The part to interogate * @return File name of the part, or null if missing or invalid * */ public static String getFileName( Part part ) { String xname = null; try { xname = part.getFileName(); } catch ( MessagingException xex ) { } // decode the file name if necessary if ( xname != null && xname.startsWith( "=?" ) ) { try { xname = MimeUtility.decodeWord( xname ); } catch ( Exception xex ) { } } return xname; } /** * A better version of setFileName() which will encode the name if necessary. * Why doesn't JAVA Mail do this? * * @param part the part to manipulate * @param name the give file name encoded in UTF (JAVA) * @param charset the encoding character set */ public static void setFileName( Part part, String name, String charset ) throws MessagingException { try { name = MimeUtility.encodeWord( name, charset, null ); } catch ( Exception xex ) { } part.setFileName( name ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -