对JDK自带的邮件操作进一步封装,使用更快捷方便
<!--邮件-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
import java.io.File;
import java.util.Date;
import java.util.List;
public class CommonsEmailUtil {
private String host = "";
private Integer port = null;
private String userName = "";
private String password = "";
private String to = "";
//文件路径
private String filePath = "";
//设置附件名称
private String fileName = "";
private List<String> fileNameList = null;
//图片路径
private String photoPath = "";
//邮件标题
private String subject = "";
//邮件内容,正文
private String message = "";
public CommonsEmailUtil() {
}
/**
* 发送内嵌图片和附件邮件
*
* @throws Exception
*/
public void sendImageAndAttachmentMail() throws Exception {
HtmlEmail mail = new HtmlEmail();
// 设置邮箱服务器信息
mail.setSmtpPort(port);
mail.setHostName(host);
// 设置密码验证器
mail.setAuthentication(userName, password);
// 设置邮件发送者
mail.setFrom(userName);
// 设置邮件接收者
mail.addTo(to);
// 设置邮件编码
mail.setCharset("UTF-8");
// 设置邮件主题
mail.setSubject(subject);
String htmlText = "";
if (!photoPath.equals("")) {
mail.embed(new File(photoPath), "image");
// 设置邮件内容
htmlText = "this is a HTML email.";
} else {
htmlText = message;
}
mail.setHtmlMsg(htmlText);
//有附件的时候才创建附件
if (fileNameList != null && fileNameList.size() != 0) {
// 创建多个附件
for (String fileName : fileNameList) {
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(filePath + fileName);
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName(fileName);
mail.attach(attachment);
}
}
// 设置邮件发送时间
mail.setSentDate(new Date());
// 发送邮件
mail.send();
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getPhotoPath() {
return photoPath;
}
public void setPhotoPath(String photoPath) {
this.photoPath = photoPath;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<String> getFileNameList() {
return fileNameList;
}
public void setFileNameList(List<String> fileNameList) {
this.fileNameList = fileNameList;
}
public CommonsEmailUtil(String host, Integer port, String userName, String password, String to, String filePath, String fileName, List<String> fileNameList, String photoPath, String subject, String message) {
this.host = host;
this.port = port;
this.userName = userName;
this.password = password;
this.to = to;
this.filePath = filePath;
this.fileName = fileName;
this.fileNameList = fileNameList;
this.photoPath = photoPath;
this.subject = subject;
this.message = message;
}
public CommonsEmailUtil(String host, Integer port, String userName, String password, String to, String filePath, String fileName, String photoPath, String subject, String message) {
this.host = host;
this.port = port;
this.userName = userName;
this.password = password;
this.to = to;
this.filePath = filePath;
this.fileName = fileName;
this.photoPath = photoPath;
this.subject = subject;
this.message = message;
}
/**
* 发送文本邮件
*
* @throws Exception
*/
public void sendTextMail() throws Exception {
SimpleEmail mail = new SimpleEmail();
// 设置邮箱服务器信息
mail.setSmtpPort(port);
mail.setHostName(host);
// 设置密码验证器
mail.setAuthentication(userName, password);
// 设置邮件发送者
mail.setFrom(userName);
// 设置邮件接收者
mail.addTo(to);
// 设置邮件编码
mail.setCharset("UTF-8");
// 设置邮件主题
mail.setSubject("Test Email");
// 设置邮件内容
mail.setMsg("this is a test Text mail");
// 设置邮件发送时间
mail.setSentDate(new Date());
// 发送邮件
mail.send();
}
/**
* 发送Html邮件
*
* @throws Exception
*/
public void sendHtmlMail() throws Exception {
HtmlEmail mail = new HtmlEmail();
// 设置邮箱服务器信息
mail.setSmtpPort(port);
mail.setHostName(host);
// 设置密码验证器
mail.setAuthentication(userName, password);
// 设置邮件发送者
mail.setFrom(userName);
// 设置邮件接收者
mail.addTo(to);
// 设置邮件编码
mail.setCharset("UTF-8");
// 设置邮件主题
mail.setSubject("Test Email");
// 设置邮件内容
mail.setHtmlMsg(
"this is a HTML email.");
// 设置邮件发送时间
mail.setSentDate(new Date());
// 发送邮件
mail.send();
}
/**
* 发送内嵌图片邮件
*
* @throws Exception
*/
public void sendImageMail() throws Exception {
HtmlEmail mail = new HtmlEmail();
// 设置邮箱服务器信息
mail.setSmtpPort(port);
mail.setHostName(host);
// 设置密码验证器
mail.setAuthentication(userName, password);
// 设置邮件发送者
mail.setFrom(userName);
// 设置邮件接收者
mail.addTo(to);
// 设置邮件编码
mail.setCharset("UTF-8");
// 设置邮件主题
mail.setSubject("Test Email");
mail.embed(new File("1_jianggujin.jpg"), "image");
// 设置邮件内容
String htmlText = "this is a HTML email.";
mail.setHtmlMsg(htmlText);
// 设置邮件发送时间
mail.setSentDate(new Date());
// 发送邮件
mail.send();
}
/**
* 发送附件邮件
*
* @throws Exception
*/
public void sendAttachmentMail() throws Exception {
MultiPartEmail mail = new MultiPartEmail();
// 设置邮箱服务器信息
mail.setSmtpPort(port);
mail.setHostName(host);
// 设置密码验证器
mail.setAuthentication(userName, password);
// 设置邮件发送者
mail.setFrom(userName);
// 设置邮件接收者
mail.addTo(to);
// 设置邮件编码
mail.setCharset("UTF-8");
// 设置邮件主题
mail.setSubject("Test Email");
mail.setMsg("this is a Attachment email.this email has a attachment!");
// 创建附件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("1_jianggujin.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName("1_jianggujin.jpg");
mail.attach(attachment);
// 设置邮件发送时间
mail.setSentDate(new Date());
// 发送邮件
mail.send();
}
/**
* 发送内嵌图片和附件邮件
*
* @throws Exception
*/
public void sendImageAndAttachmentMailTest() throws Exception {
HtmlEmail mail = new HtmlEmail();
// 设置邮箱服务器信息
mail.setSmtpPort(port);
mail.setHostName(host);
// 设置密码验证器
mail.setAuthentication(userName, password);
// 设置邮件发送者
mail.setFrom(userName);
// 设置邮件接收者
mail.addTo(to);
// 设置邮件编码
mail.setCharset("UTF-8");
// 设置邮件主题
mail.setSubject("Test Email");
mail.embed(new File("1_jianggujin.jpg"), "image");
// 设置邮件内容
String htmlText = "this is a HTML email.";
mail.setHtmlMsg(htmlText);
// 创建附件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("1_jianggujin.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName("1_jianggujin.jpg");
mail.attach(attachment);
// 设置邮件发送时间
mail.setSentDate(new Date());
// 发送邮件
mail.send();
}
}