使用Apache common email发送163邮件
使用commons-email发送邮件:http://commons.apache.org/proper/commons-email/index.html使用到的Jar:
commons-email-1.2.jar
mailapi.jar(主要提供InternetAddress类)
main方法测试:
public static void main(String[] args) {
Email email =new HtmlEmail();
HtmlEmail htmlEmail = new HtmlEmail();
try {
htmlEmail.setHtmlMsg("<html><body>163发送邮件测试</body></html>");
} catch (EmailException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
email = htmlEmail;
// 编码格式
String charset = SysConfig.getInstance().getValue("mail.charset",
"UTF-8");
// 邮件服务器
String mailServer = "smtp.163.com";
//发件邮件
String emailFrom ="xujunxiong0621@163.com";
//发件人
String emailFromName ="xujunxiong";
email.setCharset(charset);
email.setHostName(mailServer);
try {
email.setFrom(emailFrom, emailFromName);
List<InternetAddress> mailToList = new ArrayList <InternetAddress> (0);
InternetAddress [] addr = InternetAddress.parse("ygz2008it@163.com,zhou_jian_0319@163.com");
if(addr.length > 0){
for(InternetAddress add : addr){
mailToList.add(add);
}
}
//收件人
//email.addTo("ygz2008it@163.com");
email.setAuthenticator(new DefaultAuthenticator("xujunxiong0621@163.com", "*********"));
//多人发送需要使用API SetTo
/**
* setTo(Collection<InternetAddress> aCollection)
* Set a list of "TO" addresses.
*/
//email.setTo(mailToList);
email.setTo(mailToList);
//标题
email.setSubject("aaaaaa");
// send the email
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
页:
[1]