import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* javaMail发送邮件工具类
*
* @author
* @version 1.0
*/
public class MailUtil {
/**
* 附件地址
*/
private String filepath;
/**
* 取附件
*
* @return filepath
*/
public String getFilepath() {
return filepath;
}
/**
* 设置附件
*
* @param filepath filepath
*/
public void setFilepath(String filepath) {
this.filepath = filepath;
}
/**
* 发送邮件的服务器地址
*/
private String mailServerHost = "smtp.exmail.qq.com";
/**
* 发送邮件的服务器端口
*/
private String mailServerPort = "25";
/**
* 设置发送邮件前是否需要验证
*/
private boolean validate = true;
/**
* 设置发送邮件的账号
*/
private String userName = "[email protected]";
/**
* 邮件密码
*/
private String userPass = "Cd1555";
/**
* 设置发送邮件的地址
*/
private String fromAddress = "[email protected]";
/**
* 邮件发送对象
*/
private String toAddress = "[email protected]";
/**
* 设置发送邮件的主题
*/
private String subject = "测试java发送邮件";
/**
* 邮件内容
*/
private String content = "您好!
这里用java发送邮件的测试内容
-
";
/**
* 设置发送邮件的格式是否采用html编码
*/
private boolean isHTML = true;
/**
* 设置发送邮件是否采用安全连接
*/
private boolean isSSL = false;
/**
* 设置发送邮件的主机
*
* @param mailServerHost
* 发送邮件的服务器地址
*/
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
/**
* 设置发送邮件的服务器端口
* 说明:默认值"25"
*
* @param mailServerPort
* 发送邮件的服务器端口
*/
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
/**
* 设置发送邮件前是否需要验证
* 说明:默认值true要验证
*
* @param validate
* true要验证 or false不验证
*/
public void setValidate(boolean validate) {
this.validate = validate;
}
/**
* 设置发送邮件的地址
* 说明:默认值"[email protected]"
*
* @param fromAddress
* 发送邮件的地址
*/
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
/**
* 设置接收邮件的地址
* 说明:默认值"[email protected]"
*
* @param toAddress
* 接收邮件的地址
*/
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
/**
* 设置发送邮件的主题
* 说明:默认值"测试java发送邮件"
*
* @param subject
* 发送邮件的主题
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* 设置发送邮件的内容
* 说明:默认值"您好!
* 这里用java发送邮件的测试内容
*
* -顶呱呱
* "
*
* @param content
* 发送邮件的内容
*/
public void setContent(String content) {
this.content = content;
}
/**
* 设置发送邮件的格式是否采用html编码
* 说明:默认true是
*
* @param isHTML
* true是 false否
*/
public void setHTML(boolean isHTML) {
this.isHTML = isHTML;
}
/**
* 设置发送邮件是否采用安全连接
* 说明:默认false否
*
* @param isSSL
* true是 false否
*/
public void setSSL(boolean isSSL) {
this.isSSL = isSSL;
}
/**
* 设置发送邮件的账号
*
* @param userName
* 发送邮件的账号
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* 设置发送邮件的账号密码
*
* @param userPass
* 发送邮件的账号密码
*/
public void setUserPass(String userPass) {
this.userPass = userPass;
}
/**
* 发送邮件
*
* @param toAddress
* 收件人邮箱地址
* @param subject
* 邮件主题
* @param content
* 邮件内容
* @param filepath
* 附件地址
* @return true:发送成功;false:发送失败
*/
public boolean sendMail(String toAddress, String subject, String content,
String filepath) {
this.toAddress = toAddress;
this.subject = subject;
this.content = content;
this.filepath = filepath;
return sendMail();
}
/**
* 发送邮件
*
* @return true:发送成功;false:发送失败
*/
public boolean sendMail() {
Properties p = new Properties();
p.put("mail.smtp.host", mailServerHost);
p.put("mail.smtp.port", mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
if (isSSL) {
p.put("mail.smtp.starttls.enable", "true");
p.put("mail.smtp.socketFactory.fallback", "false");
p.put("mail.smtp.socketFactory.port", mailServerPort);
}
Authenticator auth = null;
if (validate) {
auth = new MyAuthenticator(userName, userPass);
}
try {
Session session = Session.getDefaultInstance(p, auth);
Message message = new MimeMessage(session);
Address from = new InternetAddress(fromAddress);
Address to = new InternetAddress(toAddress);
message.setFrom(from);
message.setRecipient(Message.RecipientType.TO, to);
message.setSubject(subject);
message.setSentDate(new Date());
if (isHTML) {
Multipart m = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
bp.setContent(content, "text/html; charset=utf-8");
// 加入附件
FileDataSource ds = new FileDataSource(filepath);
bp.setDataHandler(new DataHandler(ds));
bp.setFileName(MimeUtility.encodeText(ds.getName()));
m.addBodyPart(bp);
message.setContent(m);
} else
message.setText(content);
Transport.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 测试发送
*
* @param args
* 参数
*/
public static void main(String[] args) {
boolean x = new MailUtil().sendMail();
LoggerUtils.info(x);
}
}