java使用SimpleEmail发送邮件

来源:互联网 发布:淘宝客软文制作 编辑:程序博客网 时间:2024/05/22 01:15

java 发送html邮件,使用SimpleEmail,简单方便,分享一下。  

     所需的jar包下载地址:http://download.csdn.net/download/long66666666/10107444

代码如下:

      /**
* 发送html邮件
* @param sendTo  收接人邮件地址
* @param receiver 收接人名称
* @param subject 发送标题
* @param msg  发送内容
* @return
*/
public static boolean sendSimpleHtmlMail(String sendTo,String receiver,String subject,String msg){
String hostName = "smtp.163.com";//发送邮件的服务地址
String sendFrom = "authenUser@163.com";//发送者邮件地址
String sender = "long"; //发送者的名称
String authenUser = "authenUser@163.com"; //发送者的帐户
String authenPwd = "authenPwd";  //发送都 的密码
String smtpPort ="25";  //发送邮件的服务端口
String isSSL = "false";//是否为SSL链接的邮箱
String isTSL = "true";//是否为TSL加密方式的邮箱,true=是,false=否
try{
SimpleEmail email = new SimpleEmail();
email.setCharset("UTF-8");
email.setHostName(hostName);
email.setAuthentication(authenUser,authenPwd);
email.addTo(sendTo, receiver);
email.setFrom(sendFrom,sender);
if("true".equals(isSSL)){
email.setSslSmtpPort(smtpPort);
email.setSSLOnConnect(true);
}else{
email.setSmtpPort(25);
if("true".equals(isTSL)) {
email.setStartTLSEnabled(true);
}
}
email.setSubject(subject);
email.setMsg(msg);
email.setSentDate(new Date());
email.send();
}catch(Exception ex){
ex.printStackTrace();
return false;
}
return true;
}