java邮件开发工具类

属性文件:
#各大邮件服务器地址大全, receive结尾的键代表接收服务器地址, send结尾的键代表发送服务器地址
#网易126邮箱 
host.126.receive=pop3.126.com
host.126.send=smtp.126.com
#网易163免费邮箱 
host.163.receive=pop.163.com
host.163.send=smtp.163.com
#网易163VIP邮箱 
host.163.vip.receive=pop.vip.163.com
host.163.vip.send=smtp.vip.163.com
#网易188财富邮箱 
host.188.receive=pop.188.com
host.188.send=smtp.188.com
#网易yeah.net邮箱 
host.yeah.receive=pop.yeah.net
host.yeah.send=smtp.yeah.net
#网易netease.com邮箱 
host.netease.receive=pop.netease.com
host.netease.send=smtp.netease.com
#新浪收费邮箱 
host.sina.vip.receive=pop3.vip.sina.com
host.sina.vip.send=smtp.vip.sina.com
#新浪免费邮箱 
host.sina.receive=pop3.sina.com.cn
host.sina.send=smtp.sina.com.cn
#搜狐邮箱 
host.sohu.receive=pop3.sohu.com
host.sohu.send=smtp.sohu.com
#21cn快感邮箱 
host.21cn.vip.receive=vip.21cn.com
host.21cn.vip.send=vip.21cn.com
#21cn经济邮箱 
host.21cn.receive=pop.21cn.com
host.21cn.send=smtp.21cn.com
#tom邮箱
host.tom.receive=pop.tom.com
host.tom.send=smtp.tom.com
#263邮箱
host.263.receive=263.net
host.263.send=smtp.263.net
#中华网邮箱
host.china.receive=pop.china.com
host.china.send=smtp.china.com
#雅虎邮箱
host.yahoo.receive=pop.mail.yahoo.com
host.yahoo.send=smtp.mail.yahoo.com
#Gmail邮箱
host.gmail.receive=pop.gmail.com
host.gmail.send=smtp.gmail.com
#QQ邮箱
host.qq.receive=pop.qq.com
host.qq.send=smtp.qq.com

工具类:
package org.liufei.email;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 从属性文件中读取邮件服务器地址
 * 
 * @author 刘飞
 * 
 */
public final class MailHost {
	private static Logger logger = Logger.getLogger(MailHost.class.getName());

	private static final String HOST_CONFIG = "/mailhost.properties";
	private static Properties HOST = new Properties();

	static {
		try {
			HOST.load(ConfigHelper.getResourceAsStream(HOST_CONFIG));
			if (HOST == null)
				HOST = ConfigHelper.getConfigProperties(HOST_CONFIG);
		} catch (FileNotFoundException e) {
			logger.log(Level.WARNING, e.getLocalizedMessage());
		} catch (IOException e) {
			logger.log(Level.WARNING, e.getLocalizedMessage());
		}
	}

	private static String get(String key) {
		return HOST.getProperty(key, "");
	}

	private static final String host_126_receive = "host.126.receive";
	private static final String host_126_send = "host.126.send";

	public static String get126ReceiveHost() {
		return get(host_126_receive);
	}

	public static String get126SendHost() {
		return get(host_126_send);
	}

	private static final String host_163_receive = "host.163.receive";
	private static final String host_163_send = "host.163.send";

	public static String get163ReceiveHost() {
		return get(host_163_receive);
	}

	public static String get163SendHost() {
		return get(host_163_send);
	}

	private static final String host_163_vip_receive = "host.163.vip.receive";
	private static final String host_163_vip_send = "host.163.vip.send";

	public static String get163VipReceiveHost() {
		return get(host_163_vip_receive);
	}

	public static String get163VipSendHost() {
		return get(host_163_vip_send);
	}

	private static final String host_188_receive = "host.188.receive";
	private static final String host_188_send = "host.188.send";

	public static String get188ReceiveHost() {
		return get(host_188_receive);
	}

	public static String get188SendHost() {
		return get(host_188_send);
	}

	private static final String host_yeah_receive = "host.yeah.receive";
	private static final String host_yeah_send = "host.yeah.send";

	public static String getYeahReceiveHost() {
		return get(host_yeah_receive);
	}

	public static String getYeahSendHost() {
		return get(host_yeah_send);
	}

	private static final String host_netease_receive = "host.netease.receive";
	private static final String host_netease_send = "host.netease.send";

	public static String getNeteaseReceiveHost() {
		return get(host_netease_receive);
	}

	public static String getNeteaseSendHost() {
		return get(host_netease_send);
	}

	private static final String host_sina_vip_receive = "host.sina.vip.receive";
	private static final String host_sina_vip_send = "host.sina.vip.send";

	public static String getSinaVipReceiveHost() {
		return get(host_sina_vip_receive);
	}

	public static String getSinaVipSendHost() {
		return get(host_sina_vip_send);
	}

	private static final String host_sina_receive = "host.sina.receive";
	private static final String host_sina_send = "host.sina.send";

	public static String getSinaReceiveHost() {
		return get(host_sina_receive);
	}

	public static String getSinaSendHost() {
		return get(host_sina_send);
	}

	private static final String host_sohu_receive = "host.sohu.receive";
	private static final String host_sohu_send = "host.sohu.send";

	public static String getSohuReceiveHost() {
		return get(host_sohu_receive);
	}

	public static String getSohuSendHost() {
		return get(host_sohu_send);
	}

	private static final String host_21cn_vip_receive = "host.21cn.vip.receive";
	private static final String host_21cn_vip_send = "host.21cn.vip.send";

	public static String get21cnVipReceiveHost() {
		return get(host_21cn_vip_receive);
	}

	public static String get21cnVipSendHost() {
		return get(host_21cn_vip_send);
	}

	private static final String host_21cn_receive = "host.21cn.receive";
	private static final String host_21cn_send = "host.21cn.send";

	public static String get21cnReceiveHost() {
		return get(host_21cn_receive);
	}

	public static String get21cnSendHost() {
		return get(host_21cn_send);
	}

	private static final String host_tom_receive = "host.tom.receive";
	private static final String host_tom_send = "host.tom.send";

	public static String getTomReceiveHost() {
		return get(host_tom_receive);
	}

	public static String getTomSendHost() {
		return get(host_tom_send);
	}

	private static final String host_263_receive = "host.263.receive";
	private static final String host_263_send = "host.263.send";

	public static String get263ReceiveHost() {
		return get(host_263_receive);
	}

	public static String get263SendHost() {
		return get(host_263_send);
	}

	private static final String host_china_receive = "host.china.receive";
	private static final String host_china_send = "host.china.send";

	public static String getChinaReceiveHost() {
		return get(host_china_receive);
	}

	public static String getChinaSendHost() {
		return get(host_china_send);
	}

	private static final String host_yahoo_receive = "host.yahoo.receive";
	private static final String host_yahoo_send = "host.yahoo.send";

	public static String getYahooReceiveHost() {
		return get(host_yahoo_receive);
	}

	public static String getYahooSendHost() {
		return get(host_yahoo_send);
	}

	private static final String host_gmail_receive = "host.gmail.receive";
	private static final String host_gmail_send = "host.gmail.send";

	public static String getGmailReceiveHost() {
		return get(host_gmail_receive);
	}

	public static String getGmailSendHost() {
		return get(host_gmail_send);
	}

	private static final String host_qq_receive = "host.qq.receive";
	private static final String host_qq_send = "host.qq.send";

	public static String getQQReceiveHost() {
		return get(host_qq_receive);
	}

	public static String getQQSendHost() {
		return get(host_qq_send);
	}

	private MailHost() {
	}

	private static final class ConfigHelper {
		private static final Logger log = Logger.getLogger(ConfigHelper.class
				.getName());

		public static final URL locateConfig(final String path) {
			try {
				return new URL(path);
			} catch (MalformedURLException e) {
				return findAsResource(path);
			}
		}

		public static final URL findAsResource(final String path) {
			URL url = null;
			ClassLoader contextClassLoader = Thread.currentThread()
					.getContextClassLoader();
			if (contextClassLoader != null) {
				url = contextClassLoader.getResource(path);
			}
			if (url != null)
				return url;

			url = ConfigHelper.class.getClassLoader().getResource(path);
			if (url != null)
				return url;

			url = ClassLoader.getSystemClassLoader().getResource(path);

			return url;
		}

		public static final InputStream getConfigStream(final String path)
				throws IOException {
			final URL url = ConfigHelper.locateConfig(path);

			if (url == null) {
				String msg = "Unable to locate config file: " + path;
				log.log(Level.WARNING, msg);
			}

			try {
				return url.openStream();
			} catch (IOException e) {
				throw e;
			}
		}

		public static final Reader getConfigStreamReader(final String path)
				throws IOException {
			return new InputStreamReader(getConfigStream(path));
		}

		public static final Properties getConfigProperties(String path)
				throws IOException {
			try {
				Properties properties = new Properties();
				properties.load(getConfigStream(path));
				return properties;
			} catch (IOException e) {
				throw e;
			}
		}

		private ConfigHelper() {
		}

		public static InputStream getResourceAsStream(String resource) {
			String stripped = resource.startsWith("/") ? resource.substring(1)
					: resource;

			InputStream stream = null;
			ClassLoader classLoader = Thread.currentThread()
					.getContextClassLoader();
			if (classLoader != null) {
				stream = classLoader.getResourceAsStream(stripped);
			}
			if (stream == null) {
				stream = MailHost.class.getResourceAsStream(resource);
			}
			if (stream == null) {
				stream = MailHost.class.getClassLoader().getResourceAsStream(
						stripped);
			}
			if (stream == null) {
				log.log(Level.WARNING, resource + " not found");
			}
			return stream;
		}

		public static InputStream getUserResourceAsStream(String resource) {
			boolean hasLeadingSlash = resource.startsWith("/");
			String stripped = hasLeadingSlash ? resource.substring(1)
					: resource;

			InputStream stream = null;

			ClassLoader classLoader = Thread.currentThread()
					.getContextClassLoader();
			if (classLoader != null) {
				stream = classLoader.getResourceAsStream(resource);
				if (stream == null && hasLeadingSlash) {
					stream = classLoader.getResourceAsStream(stripped);
				}
			}

			if (stream == null) {
				stream = MailHost.class.getClassLoader().getResourceAsStream(
						resource);
			}
			if (stream == null && hasLeadingSlash) {
				stream = MailHost.class.getClassLoader().getResourceAsStream(
						stripped);
			}

			if (stream == null) {
				log.log(Level.WARNING, resource + " not found");
			}

			return stream;
		}
	}
}

你可能感兴趣的:(java,.net,qq,Yahoo,Gmail)