使用commons-email 发送邮件

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

须要的jar包java

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

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

commons-email.jar 是简化java原生email API开发的工具包,因此须要mail.jar:ide

导入上面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("username@gmail.com");
		email.setSubject("TestMail");
		email.setMsg("This is a test mail ... :-)");
		email.addTo("test@host.com");
		email.send();
	}
}
相关文章
相关标签/搜索