`
liufei.fir
  • 浏览: 685988 次
  • 性别: 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发送邮件工具类

    三、创建易用的Java邮件工具类 在实际开发中,我们可以封装一个工具类,简化上述步骤,让使用者只需提供必要的参数即可发送邮件。以下是一个简化的示例: ```java import javax.mail.*; import javax.mail.internet...

    java mail工具类

    这个工具类的创建旨在简化邮件处理的复杂性,使开发者能够便捷地集成邮件功能。以下是对JavaMail工具类及其相关知识点的详细说明: 1. **JavaMail API**: JavaMail API 是Java平台上的标准邮件接口,提供了发送、...

    java一键发送邮件,封装好的工具类

    本教程将详细讲解一个已经封装好的Java邮件发送工具类——`EasyEmailUtil`,它使得邮件发送变得简单易行,无需繁琐的配置,只需一键调用即可完成。 首先,`EasyEmailUtil`类通常会包含以下几个核心功能: 1. **...

    java 发送邮件 小工具类+ mail.jar

    这个"java 发送邮件 小工具类+ mail.jar"提供了一个便捷的解决方案。它包含了一个Java类,该类封装了发送电子邮件的过程,以及一个名为`mail.jar`的库,这个库可能包含了JavaMail API,是发送邮件所必需的。 ...

    java邮件发送工具类

    Java邮件发送工具类是Java开发中常用的一种组件,主要用于实现程序自动发送电子邮件的功能。这个工具类可以帮助开发者在程序中方便地添加附件,并确保内容和附件能够同时正确显示。以下是对这个知识点的详细说明: ...

    Java常用工具类大全,工作5年精心整理.zip

    "Java常用工具类大全,工作5年精心整理.zip"这个压缩包文件很可能包含了一位有经验的Java开发者在五年工作中积累的各种实用工具类,这些工具类能够极大地提高开发效率,简化代码编写。以下是对可能包含的知识点进行...

    5年Java开发常用工具类

    在Java开发过程中,工具类是必不可少的组成部分,它们通常包含了各种通用功能的静态方法,能够极大地提高开发效率和代码质量。这位拥有五年经验的开发者分享的工具类集合,覆盖了数据库连接、格式转换、文件操作和...

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

    本压缩包"java写的几种方便web开发工具类源码"提供了多种功能,涵盖了数据转换、数据库连接、邮件发送、验证码生成以及编码过滤和加密解密等关键领域,对于Web开发工作极具价值。 首先,让我们深入了解一下其中的几...

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

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

    java邮件开发实例

    ### Java邮件开发实例知识点 #### 一、Java邮件发送机制概览 在Java开发中,邮件功能是非常实用的一项技术,可以用于用户注册验证、找回密码等场景。本篇将通过一个具体的实例——“Java邮件开发实例”,来详细...

    Java邮件开发包 用于Java开发的邮件开发工具

    Java邮件开发包,通常指的是JavaMail API,是一个用于在Java应用程序中发送和接收电子邮件的开源库。这个包为开发者提供了全面的功能,包括SMTP、POP3、IMAP等邮件协议的支持,使得开发者能够方便地实现邮件服务功能...

    Java邮件开发详解

    ### Java邮件开发详解 在Java开发领域中,JavaMail API是一项非常重要的技术,它使得Java开发者能够方便地处理电子邮件操作,如发送、接收邮件等。本文将深入探讨JavaMail API的相关概念、工作原理以及如何使用...

    张孝祥Java邮件开发详解.docx

    【Java邮件开发详解】 在Java开发中,发送和接收邮件是一项常见的任务,涉及到的主要协议有SMTP(Simple Mail Transfer Protocol)和POP3(Post Office Protocol version 3)。SMTP用于发送邮件,而POP3则用于接收...

    常用Java工具类

    5. **Mail工具类**:JavaMail API是Java处理电子邮件的标准接口,它提供了发送、接收邮件的功能。一个Mail工具类可能封装了JavaMail的复杂配置,提供简单易用的发送邮件方法,包括设置发件人、收件人、主题、正文,...

    java 发送多人邮件+多附件工具类

    在Java编程中,发送电子邮件是一项常见的任务,尤其在企业级应用和自动化流程中。这个"发送多人邮件+多附件工具类"是为了解决这个问题而设计的,它...在实际项目中,这样的工具类可以大大提高开发效率,降低出错概率。

    java常用工具类

    Java作为一个广泛使用的编程语言,提供了丰富的工具类来简化开发工作,极大地提高了开发效率。在Java中,工具类通常是一些静态方法的集合,用于执行特定的任务,而不涉及对象的实例化。以下是对标题和描述中提到的...

    java开发常见工具类

    本文将详细讲解几个常见的Java工具类,包括地址解析工具、日期操作工具、邮件工具、JSON工具以及SQL工具,这些工具类极大地提高了开发效率。 1. 地址解析工具:在处理网络相关的任务时,例如获取或设置IP地址、域名...

    javamail邮件发送工具类

    JavaMail邮件发送工具类是Java开发中用于发送电子邮件的标准库,它提供了丰富的API来处理复杂的邮件功能。在JavaMail中,我们可以通过创建特定的类和对象来实现邮件的发送。以下将详细介绍这些类以及如何使用它们。 ...

Global site tag (gtag.js) - Google Analytics