?? pop3message.java
字號(hào):
/*
* POP3Message.java
* Copyright (C) 1999 dog <dog@dog.net.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* You may retrieve the latest version of this library from
* http://www.dog.net.uk/knife/
*/
package dog.mail.pop3;
import java.io.*;
import java.util.*;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
/**
* The message class implementing the POP3 mail protocol.
*
* @author dog@dog.net.uk
* @version 1.1
*/
public class POP3Message extends MimeMessage {
/**
* Creates a POP3 message.
* This is called by the POP3Store.
*/
protected POP3Message(POP3Folder folder, InputStream in, int msgnum) throws MessagingException {
super(folder, msgnum);
if (!(in instanceof ByteArrayInputStream) && !(in instanceof BufferedInputStream))
in = new BufferedInputStream(in);
headers = new InternetHeaders(in);
try {
int fetchsize = POP3Store.fetchsize;
byte bytes[];
if (in instanceof ByteArrayInputStream) {
fetchsize = in.available();
bytes = new byte[fetchsize];
int len = in.read(bytes, 0, fetchsize);
} else {
ByteArrayOutputStream out = new ByteArrayOutputStream();
bytes = new byte[fetchsize];
int len;
while ((len = in.read(bytes, 0, fetchsize))!=-1)
out.write(bytes, 0, len);
bytes = out.toByteArray();
}
content = bytes;
} catch(IOException e) {
throw new MessagingException("I/O error", e);
}
}
/**
* Returns the from address.
*/
public Address[] getFrom() throws MessagingException {
Address[] a = getAddressHeader("From");
if (a==null) a = getAddressHeader("Sender");
return a;
}
/**
* Returns the recipients' addresses.
*/
public Address[] getRecipients(RecipientType type) throws MessagingException {
if (type==RecipientType.NEWSGROUPS) {
String key = getHeader("Newsgroups", ",");
if (key==null) return null;
return NewsAddress.parse(key);
} else {
return getAddressHeader(getHeaderKey(type));
}
}
/**
* Returns the reply-to address.
*/
public Address[] getReplyTo() throws MessagingException {
Address[] a = getAddressHeader("Reply-To");
if (a==null) a = getFrom();
return a;
}
/**
* Returns an array of addresses for the specified header key.
*/
protected Address[] getAddressHeader(String key) throws MessagingException {
String header = getHeader(key, ",");
if (header==null) return null;
try {
return InternetAddress.parse(header);
} catch (AddressException e) {
String message = e.getMessage();
if (message!=null && message.indexOf("@domain")>-1)
try {
return parseAddress(header, ((POP3Store)folder.getStore()).getHostName());
} catch (AddressException e2) {
throw new MessagingException("Invalid address: "+header, e);
}
throw e;
}
}
/**
* Makes a pass at parsing internet addresses.
*/
protected Address[] parseAddress(String in, String defhost) throws AddressException {
Vector v = new Vector();
for (StringTokenizer st = new StringTokenizer(in, ","); st.hasMoreTokens(); ) {
String s = st.nextToken().trim();
try {
v.addElement(new InternetAddress(s));
} catch (AddressException e) {
int index = s.indexOf('>');
if (index>-1) { // name <address>
StringBuffer buffer = new StringBuffer();
buffer.append(s.substring(0, index));
buffer.append('@');
buffer.append(defhost);
buffer.append(s.substring(index));
v.addElement(new InternetAddress(buffer.toString()));
} else {
index = s.indexOf(" (");
if (index>-1) { // address (name)
StringBuffer buffer = new StringBuffer();
buffer.append(s.substring(0, index));
buffer.append('@');
buffer.append(defhost);
buffer.append(s.substring(index));
v.addElement(new InternetAddress(buffer.toString()));
} else // address
v.addElement(new InternetAddress(s+"@"+defhost));
}
}
}
Address[] a = new Address[v.size()]; v.copyInto(a);
return a;
}
/**
* Returns the header key for the specified RecipientType.
*/
protected String getHeaderKey(RecipientType type) throws MessagingException {
if (type==RecipientType.TO)
return "To";
if (type==RecipientType.CC)
return "Cc";
if (type==RecipientType.BCC)
return "Bcc";
if (type==RecipientType.NEWSGROUPS)
return "Newsgroups";
throw new MessagingException("Invalid recipient type: "+type);
}
// -- Need to override these since we are read-only --
/**
* POP3 messages are read-only.
*/
public void setFrom(Address address) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void addFrom(Address aaddress[]) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void setRecipients(javax.mail.Message.RecipientType recipienttype, Address aaddress[]) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void addRecipients(javax.mail.Message.RecipientType recipienttype, Address aaddress[]) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void setReplyTo(Address aaddress[]) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void setSubject(String s, String s1) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void setSentDate(Date date) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void setDisposition(String s) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void setContentID(String s) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void setContentMD5(String s) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void setDescription(String s, String s1) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
/**
* POP3 messages are read-only.
*/
public void setDataHandler(DataHandler datahandler) throws MessagingException {
throw new IllegalWriteException("POP3Message is read-only");
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -