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

📄 email.java

📁 Java邮箱
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    if ( headerType.equalsIgnoreCase("From:") )
      from = headerTail;
    else if ( headerType.equalsIgnoreCase("To:") )
      to = headerTail;
    else if ( headerType.equalsIgnoreCase("Cc:") )
      cc = headerTail;
    else if ( headerType.equalsIgnoreCase("Bcc:") )
      bcc = headerTail;
    else if ( headerType.equalsIgnoreCase("Subject:") )
      subject = headerTail;
    else if ( headerType.equalsIgnoreCase("Date:") )
      date = headerTail;
    else if ( headerType.equalsIgnoreCase("Reply-to:") )
      replyTo = headerTail;
    else if ( headerType.equalsIgnoreCase("Message-ID:") )
      msgID = headerTail;
    else if ( headerType.equalsIgnoreCase("Received:") ){
      if (received == null)
        received = new Vector();
      received.addElement( headerTail );
    }
    else if ( headerType.startsWith("X-") )  {
      if ( extensions == null )
        extensions = new Vector();
      extensions.addElement( header );
    }
    else  {
      if ( other == null )
        other = new Vector();
      other.addElement( header );
    }
  }

  private transient String header;

  /**
   * @param lookAhead next message line
   * @return - returns true as long as the end of
   * all headers has not been reached,
   * i.e. lookAhead.length() == 0.
   */
   
  public boolean insertHeaderLine( String lookAhead ) {
    if ( lookAhead.length() == 0 ) {                               //头文件完毕,分析header
      if ( header != null )
        parseHeader( header );    
      return false;
    }
    if ( lookAhead.startsWith(" ") || lookAhead.startsWith("\t") ){ //把信息连接到header后
      if ( header != null )
        header = header + " " + lookAhead.substring(1);
    }
    else  {
      if ( header != null )
        parseHeader( header );
      header = lookAhead;
    }
    return true;
  }

  public void attachBody( Vector body ) { 
    this.body = body; 
    }

  public String getBodyAsString() {
  	Base64  bs64 = new Base64();
    if ( body == null ) return "";
    Enumeration e = body.elements();
    StringBuffer ret = new StringBuffer("");
    while ( e.hasMoreElements() )// {
       //String aa = ( String ) e.nextElement() + "\n" ;
       //ret.append( ( String ) e.nextElement() + "\n" );
       //System.out.println("aa = " + aa );
       //ret.append(aa);
    //}
      ret.append( (String) e.nextElement() + "\n" );
      
      //parseEmail( ret.toString() );
      return ret.toString();
      //解析邮件内容代码
      //String str = getFromBASE64( ret.toString() );
      //return str;              //解析邮件内容,此处只考虑了BASE64编码
 
  }

  public void attachBody( String body ) {
    try  {
      BufferedReader lines = new BufferedReader( new StringReader( body ) );
      String line = lines.readLine();
      //System.out.println("line = " + line );
      if ( line != null ) {
        this.body = new Vector();
        while ( line != null )  {
          this.body.addElement( line );
          line = lines.readLine();
          }
        }
      else
        this.body = null;
      }
    catch (Exception e) {
      new WinFrame( "Attach email text",e );
      }
  }
  
 public String getAttachments()  { return attachName; } 
  
 public void attachment( Vector attach ) { 
    this.attachment = attach; 
    }

 public void attachment( String attachName ){       //读取附件
   try {
   	  String str ;
   	  BufferedReader br = new BufferedReader( new FileReader(attachName) );
   	  this.attachment = new Vector();
   	  while (  (str = br.readLine() ) != null  ) {  //readLine()依序读取每一行字符串
            System.out.println("str = " + str );
            this.attachment.addElement(str);
			}
   	  
   }
   catch(Exception e){
   	  System.err.println( e.toString() );
   }
 	
 }


  public void parse( String message ) {
    try  {
      clear();
      BufferedReader lines = new BufferedReader( new StringReader( message ) );
      String line;
      while ( ( line = lines.readLine() ) != null )
        if ( !insertHeaderLine( line ) )
          break;
      // extract body;
      if ( ( line = lines.readLine() ) != null )  {
        body = new Vector();
        while (line != null) {
          body.addElement( line );
          line = lines.readLine();
         }
       }
      else
        body = null;
    }
    catch (Exception e) {
      new WinFrame("Crack email into headers, etc.",e);
    }
  }

  public Email( String message ) {
     parse(message); 
  }
  public Email(){ 
     clear(); 
  }
  
  //解码
  public String DecodeEmail( String str ) {
		if ( str.indexOf("=?x-unknown?") >= 0 ) {
			str = str.replaceAll("x-unknown", "GBK"); // 将编码方式的信息由x-unkown改为gbk
		}
		try {
			str = MimeUtility.decodeText( str );        //再重新解码
		}
		catch (Exception e) {
			System.err.println( e.toString() );
		}
		return str;
	}
  
  
  //解BASE64解码(邮件正文部分)
  public static String getFromBASE64( String s ) {
     Base64  bs64 = new Base64();                      //编码解码
     String  str = "";                                 //解码后代码
     String  baseStr = "";                             //需解码的BASE64代码
     String  Conten_str = "";                          //标记代码
     String  lastStr = "";                             //分解后上一行代码,默认为空
     StringTokenizer st = new StringTokenizer( s, "\n" );  
     while( st.hasMoreTokens() ) {
      	 String thisStr = st.nextToken(); 
      	 //System.out.println( thisStr );
      	 if( !lastStr.equals("") && bs64.isBase64( thisStr ) ) {      //当前行是BASE64编码
      	     baseStr += thisStr; 
      	  }  
      	 else if( !lastStr.equals("") && thisStr != null && bs64.isBase64(lastStr) && !bs64.isBase64(thisStr) ){  //上一行是BASE64代码,当前行不是
      	     try { 
            	 byte[] b = bs64.decode( baseStr.getBytes("ISO8859-1") ); 
            	 str += new String(b) ;   
                }
             catch (Exception en) { 
                 System.out.println( en.toString() );
                }  
             str += thisStr;
             baseStr = "";
      	  } 
      	 else
      	 	 str += thisStr + "\n";                 //当前行不是BASE64编码 
      	 lastStr = thisStr;	 
      }
       
      return str;
   } 
  
  
   //解析邮件
   public static void parseEmail(String s){
   	  StringTokenizer st = new StringTokenizer(s,"\n");   //字符串分割
   	  String thisStr = "";                                //当前行代码
   	  String lastStr = "";                                //上一行代码                          
   	  while( st.hasMoreTokens() ){
   	  	 thisStr = st.nextToken();
   	  	 System.out.println(thisStr);
   	  	 lastStr = thisStr;                               
   	  	 System.out.println( "." + lastStr );
   	   } 	
  	 
    }
    
  
	public String ContentType( String str ) {//从邮件读取Subject

		String cType = "";

		if ( str.startsWith("Content-Type: ") ) {

			cType = str.substring(8);

		}

		return str;

	 }
  
  
  
  
   
   public static String getBASE64( String s ) {
     Base64  bs64 = new Base64();                      //编码解码
     String  str = "";                                 //解码后代码
     String  baseStr = "";                             //需解码的BASE64代码
     String  Conten_str = "";                          //标记代码
     String  lastStr = "";                             //分解后上一行代码,默认为空
     StringTokenizer st = new StringTokenizer( s, "\n" );  
     while( st.hasMoreTokens() ) {
      	 String thisStr = st.nextToken(); 
      	 //System.out.println( thisStr );
      	 if( !lastStr.equals("") && bs64.isBase64( thisStr ) ) {      //当前行是BASE64编码
      	     baseStr += thisStr; 
      	  }  
      	 else if( !lastStr.equals("") && thisStr != null && bs64.isBase64(lastStr) && !bs64.isBase64(thisStr) ){  //上一行是BASE64代码,当前行不是
      	     try { 
            	 byte[] b = bs64.decode( baseStr.getBytes("ISO8859-1") ); 
            	 str += new String(b) ;   
                }
             catch (Exception en) { 
                 System.out.println( en.toString() );
                }  
             str += thisStr;
             baseStr = "";
      	  } 
      	 else
      	 	 str += thisStr + "\n";                 //当前行不是BASE64编码 
      	 lastStr = thisStr;	 
      }
       
      return str;
   }  
    
   
}

⌨️ 快捷键说明

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