使用commons-email 发送邮件

具体请看commons-email项目主页:http://commons.apache.org/email/userguide.html

需要的jar包

commons-email.jar:http://labs.renren.com/apache-mirror//commons/email/binaries/commons-email-1.2-bin.zip

mail.jar:http://download.oracle.com/otn-pub/java/javamail/1.4.5/javamail1_4_5.zip

commons-email.jar 是简化java原生email API开发的工具包,所以需要mail.jar:

导入上面2个jar后,简单的测试:

package test;

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

public class Main {
	public static void main(String[] args) throws EmailException {
		Email email = new SimpleEmail();
		email.setHostName("smtp.gmail.com");
		email.setSmtpPort(587);
		email.setAuthenticator(new DefaultAuthenticator("username", "password"));
		email.setTLS(true);
		email.setFrom("[email protected]");
		email.setSubject("TestMail");
		email.setMsg("This is a test mail ... :-)");
		email.addTo("[email protected]");
		email.send();
	}
}

 

你可能感兴趣的:(使用commons-email 发送邮件)