📄 email.java
字号:
package jws.net;
// Copyright 1997, John Webster Small
// All rights Reserved
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import jws.awt.*;
public class Email implements Serializable
{
protected String from, to, cc, bcc, subject;
protected String date;
protected Vector body;
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 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 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 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 bcc; }
public void setBcc(String bcc)
{
if (bcc == null)
this.bcc = "";
else
this.bcc = bcc;
}
public String getSubject()
{ return 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()
{
SimpleDateFormat df
= new SimpleDateFormat
("EE, dd MMM yyyy HH:mm:ss zz");
Date d = new Date();
date = df.format(new Date());
msgID = String.valueOf(d.getTime());
int i = from.indexOf("@");
if (i >= 0)
msgID = msgID + from.substring(i);
else
msgID = msgID + "@" + from;
}
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);
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) {
if (header != null)
parseHeader(header);
return false;
}
if (lookAhead.startsWith(" ")
|| lookAhead.startsWith("\t")) {
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()
{
if (body == null)
return "";
Enumeration e = body.elements();
StringBuffer ret = new StringBuffer("");
while (e.hasMoreElements())
ret.append((String) e.nextElement() + "\n");
return ret.toString();
}
public void attachBody(String body)
{
try {
BufferedReader lines =
new BufferedReader(new StringReader(body));
String line = lines.readLine();
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 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(); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -