使用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();
	}
}
相關文章
相關標籤/搜索