java 发送邮件

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {
	public static String SMTP_HOST = "smtp.126.com";
	public static String SMTP_PORT = "25";
	public static String SMTP_PROTOCOL = "SMTP";
	public static String SMTP_AUTH = "true";
	public static String AUTH_USER = "t***gon@126.com";
	public static String AUTH_PASSWORD = "asdfsdf";
	public static String FROM_ADDRESS = "*****@126.com";

	/**
	 * 发送email,目的地址为一个
	 * 
	 * @param to
	 *            目的email地址
	 * @param title
	 *            email的标题
	 * @param content
	 *            email的内容
	 * @return 返回是否发送成功
	 */
	public static boolean send(String to, String title, String content) {
		boolean isSuccess = true;
		if (to == null || title == null || content == null)
			return false;
		Properties property = new Properties();
		// 设置一些基本属性
		property.put("mail.smtp.host", SMTP_HOST);
		property.put("mail.smtp.port", SMTP_PORT);
		property.put("mail.smtp.protocol", SMTP_PROTOCOL);
		property.put("mail.smtp.auth", SMTP_AUTH);
		MyAuthenticator myauth = new MyAuthenticator(AUTH_USER, AUTH_PASSWORD);
		// 获得发送邮件的会话
		Session mailSession = Session.getDefaultInstance(property, myauth);
		// 生成发送的消息
		Message message = new MimeMessage(mailSession);
		try {
			// 形成发送的mail地址
			InternetAddress fromAddress = new InternetAddress(FROM_ADDRESS);
			message.setFrom(fromAddress);
			InternetAddress toAddress = new InternetAddress(to);
			// 加入发送消息的目的地址addRecipients()两个重载函数
			message.addRecipient(Message.RecipientType.TO, toAddress);
			// 设置消息题
			message.setSubject(title);
			// 设置消息主题
			message.setText(content);
			// 保存
			message.saveChanges();
		} catch (Exception e) {
			isSuccess = false;
			System.out.println(e.getMessage());
		}
		// 发送mail
		try {
			Transport.send(message,
					message.getRecipients(Message.RecipientType.TO));
		} catch (Exception e) {
			isSuccess = false;
			System.out.println(e.getMessage());
		}
		return isSuccess;
	}

	/**
	 * 发送email,目的地址为一组
	 * 
	 * @param toList
	 *            一组email地址
	 * @param title
	 *            email的标题
	 * @param content
	 *            email的内容
	 * @return boolean 返回是否成功
	 */
	public static boolean send(List<String> toList, String title, String content) {
		boolean isSuccess = true;
		if (toList == null || title == null || content == null
				|| toList.size() == 0)
			return false;
		Properties property = new Properties();
		// 设置一些基本属性
		property.put("mail.smtp.host", SMTP_HOST);
		property.put("mail.smtp.port", SMTP_PORT);
		property.put("mail.smtp.protocol", SMTP_PROTOCOL);
		property.put("mail.smtp.auth", SMTP_AUTH);
		MyAuthenticator myauth = new MyAuthenticator(AUTH_USER, AUTH_PASSWORD);
		// 获得发送邮件的会话
		Session mailSession = Session.getDefaultInstance(property, myauth);
		// 生成发送的消息
		Message message = new MimeMessage(mailSession);
		try {
			// 形成发送的mail地址
			InternetAddress fromAddress = new InternetAddress(FROM_ADDRESS);
			message.setFrom(fromAddress);
			for (String to : toList) {
				InternetAddress toAddress = new InternetAddress(to);
				// 加入发送消息的目的地址addRecipients()两个重载函数
				message.addRecipient(Message.RecipientType.TO, toAddress);
			}
			// 设置消息题
			message.setSubject(title);
			// 设置消息主题
			message.setText(content);
			// 保存
			message.saveChanges();
		} catch (Exception e) {
			isSuccess = false;
			System.out.println(e.getMessage());
		}
		// 发送mail
		try {
			Transport.send(message,
					message.getRecipients(Message.RecipientType.TO));
		} catch (Exception e) {
			isSuccess = false;
			System.out.println(e.getMessage());
		}
		return isSuccess;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO
		String to = "xuzhanyi@yahoo.com.cn";
		String title = "title";
		String content = "content ";
		List<String> toList = new ArrayList<String>();
		toList.add(to);
		int i;
		for(;;){
//			System.out.println(new Date() + ":" + SendMail.send(toList, title + (int)(1000*Math.random()), content));
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class MyAuthenticator extends Authenticator {
	private String user;
	private String password;

	public MyAuthenticator(String user, String password) {
		this.user = user;
		this.password = password;
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(user, password);
	}
}

需要mail.jar




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值