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

📄 maildirstore.java

📁 mail sent and revesive system jsp+mysql
💻 JAVA
字号:
/*
 * MaildirStore.java
 * Copyright (C) 1999 fengyun <fengyun@gbsource.net>
 */

package fengyun.Fastmail.Maildir;

import java.io.*;
import java.net.*;
import javax.mail.*;
import javax.mail.event.*;
import java.util.Hashtable;
import java.util.Vector;

/**
 * Maildir形式的存储类
 * @author fengyun
 * @version 1.01
 */
public class MaildirStore extends Store {
	
	MaildirConstants CONST = MaildirConstants.getInstance();
	
	static int fetchsize = 1024;
	Hashtable folders = new Hashtable();
	String UserName = null;
	File Maildir = null;
	/**
	 * 构造函数
	 * @param session 当前的邮件Session
	 * @param urlname 邮件Maildir的URL形式
	 */
	public MaildirStore(Session session,URLName urlname) throws MessagingException{
		super(session, urlname);
		if (urlname==null) throw new MessagingException("URLNAME ERROR: Maildir://UserName:Password@/home/mail/UserName/Maildir");
		UserName = urlname.getUsername();
		Maildir = new File(urlname.getFile());
		if (!Maildir.exists()) throw new MessagingException("Maildir is not exists.");
		fetchsize = CONST.fetchsize;
		
		this.listFolders();
	}
	/**
	 * 对Maildir列表
	 */
	public void listFolders() throws MessagingException{
		String [] list = Maildir.list(new MaildirFilenameFilter('/'));				//所有目录文件
		if (list == null || list.length <= 0) throw new MessagingException("Error Maildir Format.");
		folders.clear();
		for(int i = 0;i<list.length;i++) {
			if (hasFolder(list[i])) {
				Folder folder = new MaildirFolder(this,list[i]);
				folders.put(list[i],folder);
			}
		}
	}
	/**
	 * 针对协议的连接方式,Maildir不采用
	 * @return true
	 */
	protected boolean protocolConnect(String host, int port, String username, String password) throws MessagingException {
		return true;
	}
	/**
	 * 返回默认邮件夹
	 * @return Folder 默认邮件夹
	 */
	public Folder getDefaultFolder() throws MessagingException {
		Folder folder = getFolder(CONST.DefaultFolderName);
		return folder;
	}
	/**
	 * 返回用户名
	 * @return String 用户名
	 */
	public String getUserName() {
		return UserName;
	}
	/**
	 * 返回Store的绝对路径
	 * @return String 绝对路径
	 */
	public String getMaildir() {
		return Maildir.getAbsolutePath();
	}
	/**
	 * 此名称的邮件夹是否存在
	 * @param foldername 邮件夹名
	 * @return boolean 是否存在
	 */
	public boolean hasFolder(String foldername) {
		File file = new File(getMaildir() + File.separator + foldername);
		if (file !=null && file.exists()) return true;
		else return false;
	}
	/**
	 * 返回邮件夹
	 * @param subdir 相对于Maildir的相对路径
	 * @return Folder 对应邮件夹
	 */
	public Folder getFolder(String subdir) throws MessagingException { 
		Folder folder = (Folder)folders.get(subdir);
		if (folder == null) {
			if (!hasFolder(subdir)) throw new MessagingException("The Folder isn't exists");
			folders.put(subdir,folder = new MaildirFolder(this,subdir));
		}
		return folder;
	}
	/**
	 * 创建邮件夹
	 * @param subdir 相对于Maildir的相对路径
	 * @return boolean 是否成功创建
	 */
	public boolean makeFolder(String subdir) {
		File file = new File(getMaildir() + File.separator + subdir);
		if (file.exists()) {
			file = null;
			return true;
		}
		else {
			file.mkdir();
			try {
				folders.put(subdir,new MaildirFolder(this,subdir));
			}
			catch(Exception e) {
				e.printStackTrace();
				return false;
			}
			file = null;
			return true;
		}
	}
	/**
	 * 删除邮件夹
	 * @param subdir 相对于Maildir的相对路径
	 * @return boolean 是否成功创建
	 */
	public boolean deleteFolder(String subdir) {
		File file = new File(getMaildir() + File.separator + subdir);
		if (!file.exists()) {
			file = null;
			return false;
		}
		else {
			try {
				folders.remove(getFolder(subdir));
			}
			catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			file.delete();
			file = null;
			return true;
		}
	}
	/**
	 * 根据URLName返回对应邮夹
	 * @return Folder null
	 */
	public Folder getFolder(URLName urlname) throws MessagingException {
        return getFolder(urlname.getFile());
	}
	
	/**
	 * 返回当前Session
	 * @return Session 当前Session
	 */
	public Session getSession() {
		return session;
	}
	/**
	 * 返回信箱大小
	 * @return int 信箱大小
	 */
	public int getSize() {
		return (int)fengyun.Fastmail.util.DU.getSize(Maildir);
	}
}

⌨️ 快捷键说明

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