⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messageutilities.java

📁 一个用java写的mail.里面的代码值得我们去研究!学习。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    String xcharset = "UTF-8";    InternetAddress[] xaddresses = encodeAddresses( getDefaultFromString(), xcharset );    return xaddresses[0];  }  public static String  getDefaultFromString() {  // build a string from the user defined From personal and address    String xstring = UserProperties.getProperty( "fromPersonal", null );    String xaddress = UserProperties.getProperty( "fromAddress", null );    if ( xaddress == null ) {      xaddress = UserProperties.getProperty( ".user.name", "unknown" );    }    if ( xstring != null && xstring.length() > 0 ) {      xstring = xstring + " <" + xaddress + ">";    } else {      xstring = xaddress;    }    return xstring;  }  public static InternetAddress  getDefaultReplyTo() throws MessagingException {  // build a string from the user defined ReplyTo personal and address    String xstring = UserProperties.getProperty( "replyToPersonal", null );    String xaddress = UserProperties.getProperty( "replyToAddress", null );    if ( xaddress == null ) {      xaddress = UserProperties.getProperty( ".user.name", "unknown" );    }    if ( xstring != null && xstring.length() > 0 ) {      xstring = xstring + " <" + xaddress + ">";    } else {      xstring = xaddress;    }  // encode the address    String xcharset = "UTF-8";    InternetAddress[] xaddresses = encodeAddresses( xstring, xcharset );    return xaddresses[0];  }//............................................................  public static InternetHeaders  getHeadersWithFrom( Message message ) throws MessagingException {    Header xheader;    InternetHeaders xheaders = new InternetHeaders();    Enumeration xe = message.getAllHeaders();    for ( ; xe.hasMoreElements() ; ) {      xheader = (Header)xe.nextElement();      xheaders.addHeader( xheader.getName(), xheader.getValue() );    }    return xheaders;  }  public static InternetHeaders  getHeaders( Message message ) throws MessagingException {    Header xheader;    InternetHeaders xheaders = new InternetHeaders();    Enumeration xe = message.getAllHeaders();    for ( ; xe.hasMoreElements() ; ) {      xheader = (Header)xe.nextElement();      if ( ! xheader.getName().startsWith( "From " ) ) {        xheaders.addHeader( xheader.getName(), xheader.getValue() );      }    }    return xheaders;  }  public static void  addHeaders( Message message, InternetHeaders headers ) throws MessagingException {    Header xheader;    Enumeration xe = headers.getAllHeaders();    for ( ; xe.hasMoreElements() ; ) {      xheader = (Header)xe.nextElement();      message.addHeader( xheader.getName(), xheader.getValue() ) ;    }  }//............................................................  public static void  attach( MimeMultipart multipart, Vector attachments, String charset )    throws MessagingException {    for ( int xindex = 0 ; xindex < attachments.size() ; xindex++ ) {      Object xobject = attachments.elementAt( xindex );    // attach based on type of object      if ( xobject instanceof Part ) {        attach( multipart, (Part)xobject, charset );      } else if ( xobject instanceof File ) {        attach( multipart, (File)xobject, charset );      } else if ( xobject instanceof String ) {        attach( multipart, (String)xobject, charset, Part.ATTACHMENT );      } else {        throw new MessagingException( "Cannot attach objects of type " +                                      xobject.getClass().getName() );      }    }  }  public static void  attach( MimeMultipart multipart, Part part, String charset ) throws MessagingException {    MimeBodyPart xbody = new MimeBodyPart();    PartDataSource xds = new PartDataSource( part );    DataHandler xdh = new DataHandler( xds );    xbody.setDataHandler( xdh );    int xid = multipart.getCount() + 1;    String xtext;  // UNDONE  //xbody.setContentLanguage( String ); // this could be language from Locale  //xbody.setContentMD5( String md5 ); // don't know about this yet    xtext = part.getDescription();    if ( xtext == null ) {      xtext = "Part Attachment: " + xid;    }    xbody.setDescription( xtext, charset );    xtext = getContentDisposition( part ).getType();    xbody.setDisposition( xtext );    xtext = MessageUtilities.getFileName( part );    if ( xtext == null || xtext.length() < 1 ) {      xtext = "PART" + xid;    }    MessageUtilities.setFileName( xbody, xtext, charset );    multipart.addBodyPart( xbody );  }  public static void  attach( MimeMultipart multipart, File file, String charset ) throws MessagingException {  // UNDONE how to specify the character set of the file???    MimeBodyPart xbody = new MimeBodyPart();    FileDataSource xds = new FileDataSource( file );    DataHandler xdh = new DataHandler( xds );    xbody.setDataHandler( xdh );  // UNDONE  // xbody.setContentLanguage( String ); // this could be language from Locale  // xbody.setContentMD5( String md5 ); // don't know about this yet    xbody.setDescription( "File Attachment: " + file.getName(), charset );    xbody.setDisposition( Part.ATTACHMENT );    MessageUtilities.setFileName( xbody, file.getName(), charset );    multipart.addBodyPart( xbody );  }  public static void  attach( MimeMultipart multipart, String text, String charset, String disposition )    throws MessagingException {    int xid = multipart.getCount() + 1;    String xname = "TEXT" + xid + ".TXT";    MimeBodyPart xbody = new MimeBodyPart();    xbody.setText( text, charset );  // UNDONE  //xbody.setContentLanguage( String ); // this could be language from Locale  //xbody.setContentMD5( String md5 ); // don't know about this yet    xbody.setDescription( "Text Attachment: " + xname, charset );    xbody.setDisposition( disposition );    MessageUtilities.setFileName( xbody, xname, charset );    multipart.addBodyPart( xbody );  }//............................................................  /**   * Decode the contents of the Part into text and attachments.   */  public static StringBuffer  decodeContent( Part part, StringBuffer buffer, Vector attachments, Vector names )    throws MessagingException {    MessageUtilities.subDecodeContent( part, buffer, attachments, names );  // If we did not get any body text, scan on more time  // for a text/plain part that is not 'inline', and use  // that as a proxy...    if ( buffer.length() == 0 && attachments != null ) {      for ( int i = 0, sz = attachments.size() ; i < sz ; ++i ) {        Part p = (Part) attachments.elementAt(i);        ContentType xctype = MessageUtilities.getContentType( p );        if ( xctype.match( "text/plain" ) ) {          MessageUtilities.decodeTextPlain( buffer, p );          attachments.removeElementAt( i );          if ( names != null ) {            names.removeElementAt( i );          }          break;        }      }    }    return buffer;  }  /**   * Given a message that we are replying to, or forwarding,   * @param part The part to decode.   * @param buffer The new message body text buffer.   * @param attachments Vector for new message's attachments.   * @param descriptions Vector for new message's attachment descriptions.   * @returns The buffer being filled in with the body.   */  protected static StringBuffer  subDecodeContent( Part part, StringBuffer buffer, Vector attachments, Vector names )    throws MessagingException {    boolean attachIt = true;  // decode based on content type and disposition    ContentType xctype = MessageUtilities.getContentType( part );    ContentDisposition xcdisposition = MessageUtilities.getContentDisposition( part );    if ( xctype.match( "multipart/*" ) ) {      attachIt = false;      Multipart xmulti = (Multipart)MessageUtilities.getPartContent( part );      int xparts = xmulti.getCount();      for ( int xindex = 0 ; xindex < xparts ; xindex ++ ) {        MessageUtilities.subDecodeContent( xmulti.getBodyPart( xindex ),                                           buffer, attachments, names );      }    } else if ( xctype.match( "text/plain" ) ) {      if ( xcdisposition.match( "inline" ) ) {        attachIt = false;        MessageUtilities.decodeTextPlain( buffer, part );      }    }        if ( attachIt ) {    // UNDONE should simple single line entries be    //        created for other types and attachments?    //    // UNDONE should attachements only be created for "attachments" or all    // unknown content types?      if ( attachments != null ) {        attachments.addElement( part );      }      if ( names != null ) {        names.addElement( MessageUtilities.getPartName( part ) +                          " (" + xctype.getBaseType() + ") " + part.getSize() + " bytes" );      }    }    return buffer;  }  /**   * Get the name of a part.   * The part is interogated for a valid name from the provided file name   * or description.   *   * @see javax.mail.Part   * @param  part The part to interogate   * @return String containing the name of the part   * @exception MessagingException if contents of the part are invalid   */  public static String  getPartName( Part part ) throws MessagingException {    String xdescription = MessageUtilities.getFileName( part );    if ( xdescription == null || xdescription.length() < 1 ) {      xdescription = part.getDescription();    }    if ( ( xdescription == null || xdescription.length() < 1 ) && part instanceof MimePart ) {      xdescription = ((MimePart) part).getContentID();    }    if ( xdescription == null || xdescription.length() < 1 ) {      xdescription = "Message Part";    }    return xdescription;  }  /**   * Decode contents of TEXT/PLAIN message parts into UTF encoded strings.   * Why can't JAVA Mail provide this method?? Who knows!?!?!?   */  public static StringBuffer  decodeTextPlain( StringBuffer buffer, Part part ) throws MessagingException {  // pick off the individual lines of text  // and append to the buffer  //    try {      BufferedReader xreader = MessageUtilities.getTextReader( part );      for ( String xline ; ( xline = xreader.readLine() ) != null ; ) {        buffer.append( xline + '\n' );      }      xreader.close();      return buffer;    } catch( IOException xex ) {      throw new MessagingException( xex.toString() );    }  }  public static BufferedReader  getTextReader( Part part ) throws MessagingException {    try {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -