高效利用java发送邮件

package cn.com.huainanit.mail;

import cn.com.huainanit.mail.domain.MailOO;
import cn.com.huainanit.mail.util.TestMail;

public class Main {
	public static void main(String[] args) {
		for (int i = 0; i < 2; i++) {
			MailOO mail = new MailOO();
			mail.setToMail("[email protected]");
			mail.setSubject("helle");
			mail.setMessage("hello message");
			TestMail.send(mail);
			System.out.println("ss");
		}
		
	}
}



package cn.com.huainanit.mail.util;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import cn.com.huainanit.mail.domain.MailOO;

public class TestMail {
	private static String userName = "[email protected]";
	private static String name = "HNIT";
	private static String password = ""
	private static String hostName = "smtp.gmail.com";

	public static void send(MailOO mailOO) {
		// TODO Auto-generated method stub
		SimpleEmail email = new SimpleEmail();
		email.setHostName(hostName);
		try {
			email.addTo(mailOO.getToMail());
			email.setFrom(userName, name);
			email.setSubject(mailOO.getSubject());
			email.setMsg(mailOO.getMessage());
			email.setSSL(true);
			email.setAuthentication(userName, password);
			System.out.println(email.isSSL());
			email.send();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}



package cn.com.huainanit.mail.domain;

public class MailOO {
	private String toMail;
	private String message;
	private String subject;
	public String getToMail() {
		return toMail;
	}
	public void setToMail(String toMail) {
		this.toMail = toMail;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	

}





成功的哦!

你可能感兴趣的:(java,apache,Gmail)