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

📄 email.java

📁 Java邮箱
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package Email.net;

import javax.mail.internet.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import Email.awt.*;
import Email.net.Base64;

public class Email implements Serializable {
  protected String from, to, cc, bcc, subject;
  protected String date;
  protected Vector body;
  protected Vector attachment;
  protected String attachName;
  protected String msgID, replyTo;
  protected Vector received;
  protected Vector extensions;
  protected Vector other;


  //标记以前的邮件内容
  private Vector cloneOriginal() {
    Vector cloneBody = new Vector();
    cloneBody.addElement("");
    cloneBody.addElement("");
    cloneBody.addElement("----------");
    cloneBody.addElement("> From: " + from);
    cloneBody.addElement("> To: " + to);
    cloneBody.addElement("> Subject: " + subject);
    cloneBody.addElement("> Date: " + date);
    cloneBody.addElement(">");
    Enumeration e = body.elements();
    while ( e.hasMoreElements() )
      cloneBody.addElement( "> " + ( String )e.nextElement() );
    return cloneBody;
  }

  public Email replyTo( boolean includeOriginal ) {
    Email email = new Email();
    email.setTo(from);
    email.setFrom(to);
    email.setSubject("Re: " + subject);
    
    //判断是否添加原来邮件内容,是则在原邮件内容前加上标记
    if ( includeOriginal )
      email.body = cloneOriginal();
    return email;
  }

  private boolean includeOriginalDialog( Frame parent, String title ) {
    final WinDialog wd = new WinDialog(parent,title,true);
    wd.add( "Center", new Label("Include original text?") );
    Panel  p = new Panel();
    Button b = new Button("Yes");
    p.add(b);
    b.addActionListener
      (
        new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            	 wd.dispose(true); 
            }
        }
      );
      
    p.add( b = new Button("No") );
    b.addActionListener
      (
        new ActionListener(){
          public void actionPerformed(ActionEvent e) { 
            wd.dispose(); 
           }
        }
      );
      
    wd.add("South",p);
    wd.pack();
    wd.setVisible(true);
    return wd.ok;
  }

  public Email replyTo( Frame parent ) {
    return replyTo( includeOriginalDialog(parent,"Reply to") );
  }

  public Email replyToAll( boolean includeOriginal ){
    Email email = replyTo( includeOriginal );
    email.setCc( cc );
    if ( includeOriginal )
      email.body = cloneOriginal();
    return email;
  }

  public Email replyToAll(Frame parent) {
    return replyToAll(includeOriginalDialog( parent,"Reply to all") );
  }
  
  //转发,则设置标题的转发标志
  public Email forward() {
    Email email = new Email();
    email.setFrom( to );
    email.setSubject( "Fw: " + subject );
    email.body = cloneOriginal();
    return email;
  }

  /** Stip Internet Message Format address of phrase
   * and/or comments.
   * <P><CODE>
   *  Address
   *  Phrase "<" Address ">"
   * </CODE><P>
   * Recall that comments can be set off in header by
   * parentheses.
   */
  public static String addressee( String address ) {
    StringTokenizer st = new StringTokenizer( address," \t" );
    if ( st.countTokens() > 1 )
      while ( st.hasMoreTokens() ){
        String s = st.nextToken();
        if ( s.startsWith("(") )
          while ( st.hasMoreTokens() )
            if ( st.nextToken().indexOf( ")" ) >= 0 )
              break;
        if ( s.startsWith("<") )
          return s.substring( 1,s.length() - 2 );
      }
    return address;
  }

  public String getFrom() {  return DecodeEmail( from ); }

  public void setFrom( String from )  { this.from = from; }

  private static String[] toStringArray( StringTokenizer st ) { 
    String[] ret = new String[ st.countTokens() ];
    for ( int i = 0; st.hasMoreTokens(); i++ )
         ret[i] = st.nextToken();
    return ret;
  }

  public String[] getTo() {
    return toStringArray( new StringTokenizer(to,",") );   //按","分割字符串
  }

  public String getToAsString() { 
    return DecodeEmail( to ); 
    }

  public void setTo(String to) {
    if (to == null)
      this.to = "";
    else 
      this.to = to;
  }

  public String[] getCc() {
    return toStringArray( new StringTokenizer(cc,",") );
  }

  public String getCcAsString() { 
     return DecodeEmail( cc ); 
  }

  public void setCc(String cc) {
    if (cc == null)
      this.cc = "";
    else 
      this.cc = cc;
  }

  public String[] getBcc() {
    return toStringArray( new StringTokenizer(bcc,",") );
  }

  public String getBccAsString() { 
    return DecodeEmail( bcc ); 
    }

  public void setBcc(String bcc) {
    if (bcc == null)
      this.bcc = "";
    else 
      this.bcc = bcc;
  }

  public String getSubject() {
     return DecodeEmail( subject );
    }

  public void setSubject( String subject ) {
    if ( subject == null )
      this.subject = "";
    else 
      this.subject = subject;
  }

  public String getDate() {		
    if ( date.length() == 0 )
        postMark();  
    return  date;
  }

  public void postMark() {  //邮件如果没有发送时间,则补上时间。以下面的格式
  	//TimeZone tz = TimeZone.getDefault();
    //SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
    //df.setTimeZone( tz );
    Date d = new Date();
    date =   formatDate ( d );              //时间固定格式化
    msgID = String.valueOf(d.getTime() );
    int i = from.indexOf("@");
    if (i >= 0)
      msgID = msgID + from.substring(i);
    else
      msgID = msgID + "@" + from;
      
  }
  
  //时间转化,考虑时区
  public String formatDate( Date fd ){   //时间转化成字符串
  	//Date d = new Date();
  	TimeZone tz = TimeZone.getDefault();
  	//DateFormat	 formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy",lc);
  	DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss",Locale.US );
  	formatter.setTimeZone( tz );
  	String formatdate = formatter.format( fd );
  	return formatdate;
  }
  
  
  public Enumeration getHeaders() {
    Vector headers = new Vector();
    if ( received != null )
      for ( int i = 0; i < received.size(); i++ )
        headers.addElement( "Received: " + received.elementAt(i) );
    if ( to.length() > 0 )
      headers.addElement( "To: " + to );
    if ( from.length() > 0 )
      headers.addElement( "From: " + from );
    if (replyTo.length() > 0)
      headers.addElement( "Reply-To: " + replyTo );
    if ( date.length() == 0 )          // 时间为空,则必须加上收件时间       
         postMark();
    headers.addElement( "Date: " + date );
    headers.addElement( "Message-ID: <" + msgID + ">" );
    if (cc.length() > 0)
      headers.addElement( "Cc: " + cc );
    if (extensions != null)
      for ( int i = 0; i < extensions.size(); i++ )
        headers.addElement( extensions.elementAt(i) );
    if ( other != null )
      for ( int i = 0; i < other.size(); i++ )
        headers.addElement( other.elementAt(i) );
    if ( subject.length() > 0 )
      headers.addElement( "Subject: " + subject );
    return headers.elements();
  }

  public Enumeration getBody() { 
    return body.elements(); 
    }

  public void clear() {
    from = to = cc = bcc = subject = date = "";
    body = null;
    msgID = replyTo = "";
    received = null;
    extensions = null;
    other = null;
    header = null;
  }

  private void parseHeader( String header ) {     //头部分析
    int headerTypeLength = header.indexOf(":");
    String headerType = header.substring( 0,headerTypeLength + 1 );
    String headerTail = header.substring( headerTypeLength + 2 );

⌨️ 快捷键说明

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