`
g21121
  • 浏览: 685842 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java发送email

    博客分类:
  • java
 
阅读更多
package com.net263.ccs.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Pattern;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.util.StringUtils;

/**
 * 
 * 类描述:email工具
 * 
 * @author liming
 * @time 2011-7-19 下午02:51:56
 */
public class EmailUtils {

	private static Properties p;
	private static String formAddress;
	private static String host;
	private static String userName;
	private static String userPasswd;
	private static String serverType;

	static {
		InputStream is = EmailUtils.class.getClassLoader().getResourceAsStream("email.properties");
		p = new Properties();
		try {
			if (is != null) {
				p.load(is);
				formAddress = p.getProperty("mail.from.address");
				host = p.getProperty("mail.smtp.host");
				userName = p.getProperty("mail.user.name");
				userPasswd = p.getProperty("mail.user.pass");
				serverType = p.getProperty("mail.transport.protocol");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 方法描述:发送邮件
	 * 
	 * @author liming
	 * @time 2011-7-19 下午03:45:08
	 * 
	 * @param title
	 * @param context
	 * @param emails
	 */
	public static void sendEmail(String title, String context, String[] emails) {

		try {
			if (emails != null && emails.length > 0) {
				if (p != null) {
					// 建立会话
					Session session = Session.getInstance(p);
					Message msg = new MimeMessage(session); // 建立信息
					msg.setFrom(new InternetAddress(formAddress)); // 发件人
					msg.setSentDate(new Date()); // 发送日期
					msg.setContent(context, "text/html; charset=utf-8"); 
					msg.setSubject(title); // 主题
					for (String add : emails) {
						if (isEmailAddress(add)) {
							msg.setRecipient(Message.RecipientType.TO, new InternetAddress(add)); // 收件人
							// 邮件服务器进行验证
							Transport tran = session.getTransport(serverType);
							tran.connect(host, userName, userPasswd);
							tran.sendMessage(msg, msg.getAllRecipients()); // 发送邮件
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 方法描述:判断是否为邮件地址
	 * 
	 * @author liming
	 * @time 2011-8-23 下午01:58:31
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmailAddress(String email) {
		Pattern emailer = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		if (!StringUtils.hasText(email))
			return false;
		email = email.toLowerCase();
		if (email.endsWith(".con"))
			return false;
		if (email.endsWith(".cm"))
			return false;
		if (email.endsWith("@gmial.com"))
			return false;
		if (email.endsWith("@gamil.com"))
			return false;
		if (email.endsWith("@gmai.com"))
			return false;
		return emailer.matcher(email).matches();
	}

}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics