📄 messageview.java
字号:
package fengyun.Fastmail.beans;
import javax.mail.*;
import java.util.Date;
import java.util.Vector;
import java.io.IOException;
import javax.mail.internet.*;
import java.text.*;
import fengyun.Fastmail.Maildir.MaildirStore;
import fengyun.Fastmail.Maildir.MaildirFolder;
import fengyun.Fastmail.Maildir.MaildirMessage;
import javax.activation.MimeTypeParameterList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* 查看消息
* @author sanware & fengyun
* @version 1.01
*/
public class MessageView {
private static BeansConstants CONST = BeansConstants.getInstance();
private MaildirMessage message = null; //对应的消息
private StringBuffer body = new StringBuffer(); //内容
private String MessageID = null; //消息名
private String PrevMessageID = null; //前一消息名
private String NextMessageID = null; //后一消息名
private boolean hasAttachment = false; //是否拥有附件
private Vector attachList = new Vector(); //附件集
public static final String Separator = "\n";
private String FolderName = ""; //邮件夹名称
private int FolderIndex = 0; //邮件夹索引
private SimpleDateFormat formatter = new SimpleDateFormat (CONST.timeformat); //日期类型
public MessageView(HttpServletRequest request) throws MessagingException {
HttpSession httpsession = request.getSession(false);
String folderid = request.getParameter(CONST.folderid);
String MessageID = request.getParameter(CONST.messageid);
if (folderid == null || MessageID == null || httpsession == null)
throw new MessagingException("ERROR REQUEST");
FolderView folderview = (FolderView)httpsession.getAttribute(CONST.FastmailFolderView);
if (folderview == null) throw new MessagingException("FolderView Error");
MaildirStore store = (MaildirStore)httpsession.getAttribute(CONST.FastmailStore);
if (store == null) throw new MessagingException("MAILDIRSTORE ERROR");
FolderIndex = Integer.parseInt(folderid);
MaildirFolder folder = (MaildirFolder)store.getFolder(folderview.folderlist.getFolderid(FolderIndex));
FolderName = folderview.folderlist.getViewName(FolderIndex);
if (folder == null || FolderName == null) throw new MessagingException("The Folder is not exists()");
message = (MaildirMessage)folder.getMessage(MessageID);
PrevMessageID = folder.getPrevMessageID(MessageID);
NextMessageID = folder.getNextMessageID(MessageID);
try {
setContent(message.getContent());
message.hasAttachment(this.hasAttachment);
}
catch(Exception e) {
body.append("读取信件内容错误");
e.printStackTrace();
}
}
private void setContent(Object obj){
if(obj instanceof Multipart){
try{
Multipart mp = (Multipart)obj;
int count = mp.getCount();
for(int i=0;i<count;i++)
dumpPart(mp.getBodyPart(i));
if (attachList.size()>0) this.hasAttachment = true;
String content = body.toString();
if (content != null) {
String contenttype = null;
if (count > 0)
contenttype = mp.getBodyPart(0).getContentType();
if (contenttype != null && contenttype.indexOf("charset") > 0) {
int cur = contenttype.indexOf("charset");
String charset = contenttype.substring(cur + 9);
charset = charset.substring(0,charset.length() - 1);
if (charset != null && !CONST.DefaultCharSet.equals(charset) && !"us-ascii".equals(charset)) {
body = null;
body = new StringBuffer(new String(content.getBytes(charset),CONST.DefaultCharSet));
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
else if (obj instanceof String) {
body.append((String)obj);
}
else {
body.append("无法识别的信件体");
}
}
private int level = 0;
/**
* DumpPart
* @param p Part
*/
private void dumpPart(Object p) throws MessagingException,IOException {
if (p == null) return;
if (p instanceof String) {
body.append((String)p);
body.append(Separator);
return;
}
if (p instanceof Multipart) {
MimeMultipart mp = (MimeMultipart)p;
level++;
int count = mp.getCount();
for (int i= 0; i < count; i++) {
dumpPart((Object)mp.getBodyPart(i));
}
level--;
return;
}
if (p instanceof BodyPart) {
BodyPart bp = (BodyPart)p;
if (bp == null) return;
String tmpFileName = bp.getFileName();
if (tmpFileName != null && tmpFileName.length()>0) {
attachList.addElement(tmpFileName);
}
else {
if (bp.isMimeType("text/plain")) {
body.append((String)bp.getContent());
body.append(Separator);
}
else if (bp.isMimeType("text/html")) {
attachList.addElement("unknown.html");
}
}
}
}
/**
* 返回内容类型
*/
public String getContentType() throws MessagingException {
if (message.getContentType() == null) {
return "UNKNOWN CONTENTTYPE";
}
else return message.getContentType();
}
/**
* 返回发送时间
*/
public String getSentDate() throws MessagingException {
String strDate = null;
try {
strDate = formatter.format(message.getSentDate());
return strDate;
}
catch(Exception e) {
return "未知日期";
}
}
/**
* 返回消息ID
*/
public String getMessageID() throws MessagingException {
return message.getMessageID();
}
/**
* 返回来自
*/
public String getFrom() throws MessagingException {
if (message.getFrom() != null) {
return message.getFrom()[0].toString();
}
else {
return "";
}
}
/**
* 返回标志位
*/
public char getFlag() throws MessagingException {
return message.getFlag();
}
/**
* 返回转发
*/
public String getCC() throws MessagingException {
if (message.getHeader("Cc")!=null) {
return message.getHeader("Cc")[0];
}
else return "";
}
/**
* 返回发往
*/
public String getTo() throws MessagingException {
if (message.getHeader("To")!=null) {
return message.getHeader("To")[0];
}
else return "";
}
/**
* 返回主题
*/
public String getSubject() throws MessagingException {
if (message.getSubject() != null) {
return message.getSubject();
}
else return "";
}
/**
* 是否拥有附件
*/
public boolean hasAttachment(){
return hasAttachment;
}
/**
* 返回附件集
*/
public Vector getAttachment() {
return attachList;
}
/**
* 是否是第一封消息
*/
public boolean isFirstMessage() {
return getPrevMessageID() == null;
}
public boolean isLastMessage() {
return getNextMessageID() == null;
}
/**
* 上一消息的名称
*/
public String getPrevMessageID() {
return this.PrevMessageID;
}
/**
* 下一消息名称
*/
public String getNextMessageID() {
return this.NextMessageID;
}
/**
* 返回信件体
*/
public String getBody() {
if (body != null) {
String strBody = body.toString();
return strBody == null ? "" : strBody;
}
return "";
}
/**
* 返回所在邮件夹
*/
public String getFolderName() {
return FolderName;
}
/**
* 返回所在邮件夹索引
*/
public int getFolderIndex() {
return FolderIndex;
}
/**
* 返回附件
* @param attachment 附件名称
*/
public AttachmentBodyPart getAttachment(String attachment) throws MessagingException,IOException {
AttachmentBodyPart abp = new AttachmentBodyPart(message.getAttachment(attachment));
return abp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -