package com.sun.mail;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class ApacheSendMail {
public static void main(String[] args) {
boolean isSSL = true;
String host = "smtp.163.com";
int port = 465;
String from = "发件人地址";
String to = "收件人地址";
String username = "发件人地址";
String password = "发件人地址密码";
try {
Email email = new SimpleEmail();
email.setSSLOnConnect(isSSL);
email.setHostName(host);
email.setSmtpPort(port);
email.setAuthentication(username, password);
email.setFrom(from);
email.addTo(to);
email.setSubject("testEMail");
email.setMsg("hello,austin this is a test mail!");
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
System.out.println("send over!");
}
}
需要用到 Apache 的commons-email.jar 包