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

📄 messagelist.java

📁 mail sent and revesive system jsp+mysql
💻 JAVA
字号:
package fengyun.Fastmail.beans;

import java.util.*;
import java.text.*;
import javax.mail.*;
import fengyun.Fastmail.Maildir.MaildirFolder;
import fengyun.Fastmail.Maildir.MaildirMessage;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;

/**
 * 获取消息列表,分页处理
 * @author sanware & fengyun
 * @version 1.01
 */

public class MessageList {
	
	private static BeansConstants CONST = BeansConstants.getInstance();
	
	private int MessageCount = 0;
	private int PageCount = 0;
	private int PageSize = CONST.DefaultPageSize;
	private int Page = 1;
	private int Start = 0;
	private int End = 0;
	private int range = -1;
	
	private MaildirMessage[] messages = null;
	private String FolderName = null;
	private int FolderIndex = 0;
	private SimpleDateFormat formatter = new SimpleDateFormat (CONST.dateformat);

	/**
	 * 构造消息列表
	 */
	public MessageList(HttpServletRequest request) throws MessagingException {
		HttpSession httpsession = request.getSession(false);
		String folderid = request.getParameter(CONST.folderid);
		String strPage = request.getParameter(CONST.page);

		if (folderid == null || httpsession == null) {
			folderid = String.valueOf(CONST.revbox);
		}
		try {
			FolderIndex = Integer.parseInt(folderid);
			httpsession.setAttribute(CONST.folderid,folderid);
		}
		catch(Exception e) {
			throw new MessagingException("Error Folder Index" + e.getMessage());
		}
		try {
			Page = Integer.parseInt(strPage);
		}
		catch(Exception e) {
			Page = 1;
		}

		Store store = (Store)httpsession.getAttribute(CONST.FastmailStore);
		FolderView folderview = (FolderView)httpsession.getAttribute(CONST.FastmailFolderView);
		MaildirFolder folder = (MaildirFolder)store.getFolder(folderview.folderlist.getFolderid(FolderIndex));
		FolderName = folderview.folderlist.getViewName(FolderIndex);

		if (store == null || folderview == null) 
			throw new MessagingException("FolderView ERROR:It should be inited");
		
		MessageCount = folder.getMessageCount();

		PageCount = (MessageCount - 1) / PageSize + 1;

		if (Page < 1) Page = 1;
		if (Page > PageCount) Page = PageCount;
			
		Start = (Page-1) * PageSize;
		End = Math.min(MessageCount,Page * PageSize) - 1;
		
		messages = null;
		if (MessageCount == 0)  {
			Start = 0;
			End = 0;
			messages = new MaildirMessage[0];
		} else {
			messages = (MaildirMessage[])folder.getMessages(Start,End);
		}
	}
	
	/**
	 * 返回当前页数
	 */
	public int getCurrentPage() { return Page; }
	/**
	 * 返回起点
	 */
	public int getStart() { return Start + 1; }
	/**
	 * 返回终点
	 */
	public int getEnd() { return End + 1; }
	/**
	 * 返回总页数
	 */
	public int getPageCount() { return PageCount; }
	/**
	 * 返回消息总数
	 */
	public int getMessageCount() { return MessageCount;}
	
	/**
	 * 是否第一页
	 */
	public boolean isFirstPage() {
		return (Page == 1);
	}
	/**
	 * 是否最后一页
	 */
	public boolean isLastPage() {
		return (Page == PageCount);
	}
	/**
	 * 返回邮件夹显示名称
	 */
	public String getFolderName() {
		return FolderName;
	}
	/**
	 * 返回邮件夹索引
	 */
	public int getFolderIndex() {
		return FolderIndex;
	}
	/**
	 * 获取的消息数
	 */
	public int getMessageNumber() throws MessagingException {
		if (messages == null) throw new MessagingException("Please Fetch Message First");
		return messages.length;
	}
	/**
	 * 检查是否已经获取消息,消息索引是否正确
	 */
	public boolean check(int index) {
		return (messages != null) && index >= 0 && index < messages.length;
	}
	/**
	 * 返回标志位
	 * @param index 在消息集中的索引
	 */
	public char getFlag(int index) throws MessagingException {
		if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
		return messages[index].getFlag();
	}
	public long getSize(int index) throws MessagingException {
		if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
		return messages[index].getSize();
	}
	/**
	 * 返回发送日期
	 * @param index 索引
	 * @return String 发送日期 
	 */
	public String getSentDate(int index) throws MessagingException {
		if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
		String sentDate = null;
		sentDate = formatter.format(messages[index].getSentDate());
		if (sentDate == null) throw new MessagingException("ERROR SENDDATE");
		return sentDate;
	}
	/**
	 * 返回消息来自
	 * @param index 索引
	 */
	public String getFrom(int index) throws MessagingException {
		if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
		String from = null;
		Address[] address = messages[index].getFrom();
		
		if (address == null) throw new MessagingException("ERROR FROM");
		
		if (address.length > 0) from = address[0].toString(); 
		else from = "(no sender)";
		return from;
	}
	/**
	 * 返回消息主题
	 * @param index 索引
	 */
	public String getSubject(int index) throws MessagingException {
		if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
	    String subject = messages[index].getSubject();
	    if (subject == null || subject.equals("")) subject = "(no subject)";
		if (subject.length() > 30) {
			subject = subject.substring(0,28) + "..";
		}
	    return subject;
	}
	/**
	 * 返回消息ID
	 * @param index 索引
	 */
	public String getMessageID(int index) throws MessagingException {
		if (!check(index)) throw new MessagingException("Have not fetched the messages or Error index");
		return messages[index].getMessageID();
	}
}

⌨️ 快捷键说明

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