`
liufei.fir
  • 浏览: 676688 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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邮件开发工具类--EmailUtil(java源码)

    import java.util.List; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.Properties; import javax.mail.Session; import javax.mail.Transport; import javax....

    Java常用工具类大全,工作5年精心整理(最新版)

    提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...

    5年Java开发常用工具类

    此工具类是工作5年之久的大佬发给我的。工具类涉及数据库连接、格式转换、文件操作、发送邮件等等。提高开发效率,欢迎收藏

    java写的几种方便web开发工具类源码

    工具类里面有关于java bean 转map map转Javabean 可以支持日期类型转换。关于发邮件可见附件工具类,验证码工具类 编码过滤工具类 加密解密工具类,用c3po连接数据库的

    java代码-java邮件工具类代码合集

    java代码-java邮件工具类代码合集 ——学习参考资料:仅用于个人学习使用!

    java Email工具类

    该工具类可直接复制代码,进行调用 在我们的实际开发当中,一封邮件既可能包含图片,又可能包含有附件,在这样的情况下,RFC882文档规定的邮件格式就无法满足要求了。  MIME协议是对RFC822文档的升级和补充,它...

    java基础类库开发包,工作5年精心整理_Java常用工具类源码

    工作5年精心整理_Java常用工具类源码 收集了java项目开中常用的工具操作类,方法非常全,可应用在大部份java 项目中。 提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档...

    java开发常见工具类

    工具类中包括,地址解析工具,日期操作工具,邮件工具,json工具,sql工具等等。

    java发送邮件的工具类

    在日常的开发过程中,进行邮件发送的情况有很多,现在写了一个工具类发送邮件,并且包含邮件内容的样式.

    31个java工具类大汇总

    汇总了Java学习开发过程中常用的一些工具类,如字符串处理的StringUtils,日期处理的DateUtils,生成32位不重复的随机数、文件操作,xml操作,文件上传下载,收发邮件等。

    Java常用工具类

    提供了一些Java常用的工具类,工具类涉及数据库连接、格式转换、文件操作、发送邮件等等。方便大家的开发与使用,提高开发效率

    邮件发送工具类

    java开发,配置邮件客户端参数,可直接调用发送E-mail到指定账号

    JAVA语言中发送邮件超详细工具类

    JAVA语言中发送邮件超详细工具类,填写发送着邮箱信息即可发送。

    收发邮件工具类

    使用javamail开发的工具类收发邮件

    springboot与mail开发了一个发送邮件的工具、实战项目

    使用springboot开发了一个发送邮件的工具demo,亲测可用,实现类似于邮箱里面发送邮件的功能。支持发送html,图片,文档以及文件等等。 非常适合初学者学习借鉴,代码注释完整丝毫没有压力理解。

    JAVA项目开发全程实录(含电子书和所有源代码)

    2.5.2 系统工具类 74 2.6 系统托盘模块设计 79 2.6.1 系统托盘模块概述 79 2.6.2 系统托盘模块技术分析 79 2.6.3 系统托盘模块实现过程 80 2.7 系统工具模块设计 82 2.7.1 系统工具模块概述 82 2.7.2 系统工具模块...

    java工作6年积累常用工具类,可以快速解决日常开发各种问题

    java工作6年积累常用工具类,可以快速解决日常开发各种问题,涵盖线程、网络、DB、文件、IO流、邮件、Linux远程登录、加密解密、定时任务、XML解析、JOSN解析、CVS和Excel解析、中英文转换等等

    精通Java:JDK、数据库系统开发Web开发(实例代码)

    第3章 选择开发工具IDE 第2篇 Java语言基础 第4章 Java基本语法 第5章 Java面向对象程序设计(上) 第6章 Java面向对象程序设计(下) 第7章 Java异常处理机制 第8章 Java反射机制 第9章 数据结构与集合类 第3篇 ...

    java源码包---java 源码 大量 实例

     Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...

Global site tag (gtag.js) - Google Analytics