java发送邮件工具类

package com.message.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 邮件工具类
 * @author xiatianlong
 *
 * @date 2016年4月18日 下午5:12:47
 */
public class  EmailUtil {

	private static String id = "xiatianlong_xtl"; //发送账户
	private static String pwd = "********"; //账户密码
	private static String form = "xiatianlong_xtl@163.com"; //账户的地址
	private static String smtp = "smtp.163.com"; //smtp地址
	private static Properties props = null;
	private static Session session = null;
	private static Boolean outDebug = true;//输出调试信息
	static
	{
		props=new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		session=Session.getInstance(props);
		session.setDebug(outDebug);
	}
	public static void main(String[] args) {
		//675500969@qq.com
		boolean isok = EmailUtil.send("testtitle", "testcontent", "test", "675500969@qq.com");
		System.out.println(isok);
	}
	/**
	 *
	 * @param title 主题
	 * @param content 内容
	 * @param fromName 发件人姓名
	 * @param toAddress 收件人地址
	 * @return 是否成功
	 */
	public static Boolean send(String title,String content,String fromName,String[]toAddress) {
		if(toAddress==null || toAddress.length==0){return false;}
		Message msg=new MimeMessage(session);
		try {
			//发送的邮箱地址
			msg.setFrom(new InternetAddress(form,fromName));
			msg.setSubject(title);
			msg.setContent(content,"text/html;charset=gbk;");
			Transport transport=session.getTransport();
			//设置服务器以及账号和密码
			transport.connect(smtp,25,id,pwd);
			Address[] add = new Address[toAddress.length];
			//发送到的邮箱地址
			for (int i = 0; i < toAddress.length; i++) {
				add[i] = new InternetAddress(toAddress[i]);
			}
			transport.sendMessage(msg,add);
			transport.close();
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public static Boolean send(String title,String content,String fromName,String toAddress){
		String[] add = new String[]{toAddress};
		return send( title,content,fromName,add);
	}
	/**
	 * 发送一封密码找回的邮件
	 * @param name
	 * 			会员名称
	 * @param href
	 * 			链接
	 * @param toAddress
	 * 			接收者邮箱
	 * @param siteName
	 * 			发件人姓名
	 * @return
	 */
	public static boolean sendPwdHref(String name ,String href,String toAddress,String siteName){
		String title = " 【Message】帐号密码重置提示 此链接在24小时后自动失效,请勿回复此邮件";
		String content = name+"您好:<br>";
		content += "<br/>您收到这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了找回密码. 假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员!";
		content += "<br/> 需要重置密码请点击这里 <a href = "+href+">"+href+"</a>";
		content += "<br/> 如果页面不能跳转,复制该地址到浏览器地址栏访问:"+href;
		content += "<br/> 此邮件30分钟内有效!";
		content += "<br/><br/>"+siteName ;
		content += "<br/> "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) ;
		return send(title,content,siteName,toAddress);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值