基于apache-commons-email1.4 邮件发送
Apache 基于 发送 邮件 commons
2023-09-11 14:16:54 时间
package com.lucky.base.common.util;
import com.zuche.framework.utils.PropertiesReader;
import org.apache.commons.mail.HtmlEmail;
import javax.mail.internet.InternetAddress;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* 发送邮件
* Created by dongdong.shi@ucarinc.com on 2017/7/29.
*/
public final class MailUtil {
/**
* 邮件配置
*/
private static final Properties CONFIG = PropertiesReader.getProperties("mail");
/**
* 默认编码
*/
private static final String UTF8 = "utf-8";
/**
* 域名
*/
private static String DOMAIN = CONFIG.getProperty("mail.domain");
/**
* 发送服务器地址
*/
private static String HOST = CONFIG.getProperty("mail.host");
/**
* 发送端口
*/
private static String PORT = CONFIG.getProperty("mail.port");
/**
* 用户名
*/
private static String USER = CONFIG.getProperty("mail.user");
/**
* 密码
*/
private static String PASSWORD = CONFIG.getProperty("mail.password");
/**
* 发送者
*/
private static String SENDER = CONFIG.getProperty("mail.sender");
/**
* 获取邮件domain
* @return 返回domain
*/
public static String getMailDomain() {
return DOMAIN;
}
/**
* 获取admin的邮箱
* @return admin邮箱地址
*/
public static String getAdminMail() {
return USER+"@"+DOMAIN;
}
/**
* 发送邮件
* @param receives 收件人,多个收件人以;隔开
* @param subject 邮件主题
* @param msg 邮件内容
* @throws Exception 发送邮件异常
*/
public static void send(String receives, String subject, String msg) throws Exception {
HtmlEmail htmlEmail = new HtmlEmail();
htmlEmail.setHostName(HOST);
htmlEmail.setSmtpPort(Integer.parseInt(PORT));
htmlEmail.setAuthentication(USER, PASSWORD);
htmlEmail.setFrom(SENDER);
htmlEmail.setTo(getSendAddressList(receives));
htmlEmail.setCharset(UTF8);
htmlEmail.setSubject(subject);
htmlEmail.setMsg(msg);
htmlEmail.send();
}
/**
* 格式化邮件内容
* @param template 邮件模板
* @param args 参数
* @return 返回实际的邮件内容
*/
public static String format(String template, String[] args) {
MessageFormat format = new MessageFormat(template);
return format.format(args);
}
/**
* 组装收件人
* @param receives 收件人
* @return 真实的收件人地址
* @throws Exception 拼装邮件地址异常
*/
private static List<InternetAddress> getSendAddressList(String receives) throws Exception {
List<InternetAddress> addressList = new ArrayList<InternetAddress>();
String[] tokens = receives.split(";");
for(String address : tokens) {
addressList.add(new InternetAddress(address));
}
return addressList;
}
}
#配置文件#
mail.domain=luckycoffee.com
mail.host=mail.luckycoffee.com
mail.port=25
mail.user=sysadmin
mail.password=lucky_admin_2017
mail.sender=sysadmin@luckycoffee.com
mail.domain=luckycoffee.com
mail.host=mail.luckycoffee.com
mail.port=25
mail.user=sysadmin
mail.password=lucky_admin_2017
mail.sender=sysadmin@luckycoffee.com
相关文章
- 查看apache httpd server中加载了哪些模块
- web安全day31:人人都要懂的LAMP--apache服务安防配置
- POI操作报错-- java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils 解决
- 《Apache Velocity用户使用指南》翻译邀请
- 大数据系统构建:可扩展实时数据系统构建原理与最佳实践》一3.2 Apache Thrift
- 如何在 FreeBSD 10.2 上安装 Nginx 作为 Apache 的反向代理
- Hbase报错:org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet
- 基于Apache Maven构建多模块项目
- Java读取Excel数据:基于Apache POI(一)
- Apache Storm内部原理分析
- CentOS7:搭建SVN + Apache 服务器
- Apache HttpComponents 多线程处理HTTP请求
- Apache用户目录枚举工具apache-users
- 漏洞复现----13、Apache Flink 文件上传漏洞 (CVE-2020-17518)
- Apache Curator与Zookeeper版本兼容性异常:org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode =Unimplemented
- Apache Flink中的广播状态实用指南
- 快手基于 Apache Flink 的实时数仓建设实践
- 基于 Apache Flink 的实时计算数据流业务引擎在京东零售的实践和落地
- 《Spark与Hadoop大数据分析》一一第2章 Apache Hadoop和Apache Spark入门
- 《Spark与Hadoop大数据分析》一一2.1 Apache Hadoop概述
- apache jserv漏洞Apache漏洞
- linux apache虚拟主机配置(基于ip,端口,域名)
- Ubuntu Apache vhost不执行php小记
- Apache + Tomcat集群配置详解 (1)
- Descriptio Resource Path LocationType Archive for required library: 'D:/apache-maven/apache-maven-3.6.0/mavenrepository/org/springframework/spring-aspects/4.3.7.RELEASE/spring-aspects-4.3.7.RELEASE.