java发送邮件 java使用 commons-email 发送邮件 发送模板邮件,附件邮件
一、Commons Email 简介
1、Apache Commons Email :Commons Email旨在提供发送电子邮件的API。它构建在Java邮件API之上,其目的是简化。
2、官网: http://commons.apache.org/proper/commons-email/
二、发送邮件相关代码
1、maven 坐标:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>
2、定义邮件服务器地址、端口等常量
/**
* 发件服务器地址
*/
public static final String SMTP_SERVER = "smtp.qq.com";
/**
* 端口
*/
public static final String SMTP_SERVER_PORT = "587";
/**
* 帐号
*/
public static final String ACCOUNT = "[email protected]";
/**
* 密码
*/
public static final String PASSWORD = "xx"; // qq邮箱 需要以授权码为准
3、获取初始化 Email 对象
/**
* description: 获取初始化 Email 对象
* @return HtmlEmail
* @version v1.0
* @author w
* @date 2019年7月30日 下午10:07:56
*/
private static HtmlEmail getEmail() {
HtmlEmail htmlEmail = new HtmlEmail();
// 配置发件信息
htmlEmail.setSmtpPort(Integer.parseInt(SMTP_SERVER_PORT));
htmlEmail.setHostName(SMTP_SERVER);
htmlEmail.setAuthentication(ACCOUNT, PASSWORD);
htmlEmail.setSSLOnConnect(true);
htmlEmail.setCharset("UTF-8");
return htmlEmail;
}
4、发送简单的文本邮件
/**
* description: 发送简单的文本邮件
* @param addTo 收件人
* @param subject 邮件主体
* @param msg 邮件内容
* @throws EmailException
* @return void
* @version v1.0
* @author w
* @date 2019年8月7日 下午3:49:57
*/
public static void sendSimpleEmail(String addTo , String subject , String msg) throws EmailException {
HtmlEmail htmlEmail = getEmail();
htmlEmail.setFrom(ACCOUNT);
htmlEmail.addTo(addTo);
htmlEmail.setSubject(subject);
htmlEmail.setHtmlMsg(msg);
htmlEmail.send();
}
5、发送附件邮件
/**
* description: 发送附件邮件
* @param path 附件路径,也可以是url
* @param attachmentName 附件名称
* @param addTo 收件人
* @param subject 主题
* @param msg 内容
* @throws Exception
* @return void
* @version v1.0
* @author w
* @date 2019年8月7日 下午4:06:28
*/
public static void sendAttachmentEmail(String path ,String attachmentName,String addTo , String subject , String msg) throws Exception {
EmailAttachment attachment = new EmailAttachment();
// 也可以发送本地文件作为附件
// attachment.setPath(path);
attachment.setURL(new URL(path));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("图片描述zzz");
// 附件名称
attachment.setName(attachmentName);
HtmlEmail htmlEmail = getEmail();
// 添加附件
htmlEmail.attach(attachment);
htmlEmail.setFrom(ACCOUNT);
htmlEmail.addTo(addTo);
htmlEmail.setSubject(subject);
htmlEmail.setHtmlMsg(msg);
htmlEmail.send();
}
三、发送模板邮件
1、相关配置、都是用 二 中的。
2、使用 FreeMarker 作为模板语言,maven 坐标如下:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
3、创建FreeMarkerUtil类,将模板换为字符串
/**
* description: 初始化 FreeMarker 相关配置
* @param path 模板路径位置
* @throws Exception
* @return Configuration
* @version v1.0
* @author w
* @date 2019年8月1日 下午1:51:18
*/
private static Configuration init(String path) throws Exception{
Configuration configuration = new Configuration(Configuration.getVersion());
configuration.setEncoding(Locale.CHINESE,"UTF-8");
configuration.setDirectoryForTemplateLoading(new File(path));
return configuration;
}
/**
* description: 获取模板对象
* @param path 模板目录
* @param templateName 模板名称
* @throws Exception
* @return Template
* @version v1.0
* @author w
* @date 2019年8月1日 下午1:51:51
*/
public static Template getTemplate(String path , String templateName) throws Exception {
Configuration configuration = init(path);
Template template = configuration.getTemplate(templateName);
return template ;
}
/**
* description: 生成的模板转换成字符串输出
* @param path 模板目录
* @param templateName 模板名称
* @param dataMap 数据map
* @throws Exception
* @return String
* @version v1.0
* @author w
* @date 2019年8月1日 下午1:52:38
*/
public static String convertToString(String path , String templateName, Map<String,Object> dataMap) throws Exception {
StringWriter stringWriter = new StringWriter();
Template template = getTemplate(path, templateName);
template.process(dataMap, stringWriter);
return stringWriter.toString();
}
4、发送模板邮件
/**
* description: 发送模板邮件
* @param templateName 模板名称
* @param addTo 收件人
* @param subject 邮件主题
* @param map 邮件数据
* @throws Exception
* @return void
* @version v1.0
* @author w
* @date 2019年8月7日 下午3:42:37
*/
public static void sendTemplateEmail(String templateName , String addTo ,String subject,Map<String,Object> map) throws Exception {
HtmlEmail htmlEmail = getEmail();
// 模板根目录
String path = EmailUtil.class.getResource("/").getFile();
// 生成的模板转换成字符串输出
String htmlStr = FreeMarkerUtil.convertToString(path, templateName, map);
// 发件人
htmlEmail.setFrom(ACCOUNT);
// 收件人, 可多个
htmlEmail.addTo(addTo);
// 邮件主题
htmlEmail.setSubject(subject);
// 邮件内容
htmlEmail.setHtmlMsg(htmlStr);
// 发送
htmlEmail.send();
}
四、总结
1、最近公司在做邮件通知相关的功能,借着机会统一整理了下,这只是简单的实现,易于理解,学习。比如:收件人多个、抄送,图片邮件等等功能,按需进行添加即可。
2、若您看不明白,可私信联系提供源代码,谢谢。
参考资料:http://commons.apache.org/proper/commons-email/userguide.html