java发邮件
本实例使用HtmlEmail发送邮件
HtmlEmail email = new HtmlEmail();
email.setSSL(true);
email.setSmtpPort(SMTP_PORT);
email.setHostName(SMTP_SERVER);
email.setAuthentication(USER_NAME, PASSWORD);
email.addTo(“**@**.com”);
email.setFrom(“**@**.com”);
email.setSubject(“subject”);
email.setCharset(“GBK”);
email.setHtmlMsg(“<html><body><b>this is email message!</body></html>”);
email.send();
1、添加maven依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
2、发送
import org.apache.commons.mail.HtmlEmail;
import org.junit.Test;
public class TestMail {
@Test
public void testMail(){
try{
HtmlEmail email = new HtmlEmail();
email.setSSL(true);
email.setSmtpPort(25);
email.setHostName(“smtp.163.com”);
email.setAuthentication(“username”, “password”);
email.addTo(“XXX@qq.com”);
email.setFrom(“XXX@163.com”);
email.setSubject(“测试”);
email.setCharset(“GBK”);
email.setHtmlMsg(“<html><body><b>this is email message!</body></html>”);
email.send();
}catch(Exception e){
e.printStackTrace();
}
}
}