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;
}
}
}

/* * JCatalog Project */ package com.hexiang.utils; 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.mail.Message; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.hexiang.exception.CatalogException; /** * Utility class to send email. * * @author <a href="380595305@qq.com">hexiang</a> */ public class EmailUtil { //the logger for this class private static Log logger = LogFactory.getLog("com.hexiang.util.EmailUtil"); /** * Send email to a single recipient. * * @param smtpHost the SMTP email server address * @param senderAddress the sender email address * @param senderName the sender name * @param receiverAddress the recipient email address * @param sub the subject of the email * @param msg the message content of the email */ public static void sendEmail(String smtpHost, String senderAddress, String senderName, String receiverAddress, String sub, String msg) throws CatalogException { List<String> recipients = new ArrayList<String>(); recipients.add(receiverAddress); sendEmail(smtpHost, senderAddress, senderName, recipients, sub, msg); } /** * Send email to a list of recipients. * * @param smtpHost the SMTP email server address * @param senderAddress the sender email address * @param senderName the sender name * @param recipients a list of receipients email addresses * @param sub the subject of the email * @param msg the message content of the email */ public static void sendEmail(String smtpHost, String senderAddress, String senderName, List<String> recipients, String sub, String msg) throws CatalogException { if (smtpHost == null) { String errMsg = "Could not send email: smtp host address is null"; logger.error(errMsg); throw new CatalogException(errMsg); } try { Properties props = System.getProperties(); props.put("mail.smtp.host", smtpHost); Session session = Session.getDefaultInstance(props, null ); MimeMessage message = new MimeMessage( session ); message.addHeader("Content-type", "text/plain"); message.setSubject(sub); message.setFrom(new InternetAddress(senderAddress, senderName)); for (Iterator<String> it = recipients.iterator(); it.hasNext();) { String email = (String)it.next(); message.addRecipients(Message.RecipientType.TO, email); } message.setText(msg); message.setSentDate( new Date() ); Transport.send(message); } catch (Exception e) { String errorMsg = "Could not send email"; logger.error(errorMsg, e); throw new CatalogException("errorMsg", e); } } }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值