MailUtil.java


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.properties


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-commons-email1.4 邮件发送的更多相关文章

  1. 使用Apache Commons Email 发生邮件

    Apache Commons Email的Maven依赖 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-e ...

  2. 邮件发送工具类 SendMail.java

    package com.util; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.Simp ...

  3. Spring 邮件发送

      前言:以前都是直接用Java自带的邮件工具发送邮件,现在Spring帮我们做了封装,提供了更好用更简单的发送邮件工具JavaMailSender 关于邮件服务器的设置就不在这里说了,直接去QQ邮箱 ...

  4. Java系列--第八篇 基于Maven的SSME之定时邮件发送

    关于ssme这个我的小示例项目,想做到麻雀虽小,五脏俱全,看到很多一些web都有定时发送邮件的功能,想我ssme也加入一下这种功能,经查询相关文档,发现spring本身自带了一个调度器quartz,下 ...

  5. 【commons】邮件发送工具——commons-email

    一.概述 直接通过官网的overview进行了解,一句话概括如下: Commons Email aims to provide a API for sending email. It is built ...

  6. 基于Vue、Springboot网站实现第三方登录之QQ登录,以及邮件发送

    基于Vue.Springboot实现第三方登录之QQ登录 前言 一.前提(准备) 二.QQ登录实现 1.前端 2.后端 1.application.yml 和工具类QQHttpClient 2.QQL ...

  7. java apache commons HttpClient发送get和post请求的学习整理(转)

    文章转自:http://blog.csdn.net/ambitiontan/archive/2006/01/06/572171.aspx HttpClient 是我最近想研究的东西,以前想过的一些应用 ...

  8. AspNetCore 目前不支持SMTP协议(基于开源组件开发邮件发送,它们分别是MailKit 和 FluentEmail )

    net所有的功能都要重新来一遍,集成众多类库,core任重道远,且发展且努力!! 我们都知道,很多的邮件发送都是基于这个SMTP协议,但现在的.net core对这方面还不太支持,所以我们选择这两个组 ...

  9. 基于javaMail的邮件发送--excel作为附件

    基于JavaMail的Java邮件发送 Author xiuhong.chen@hand-china.com Desc 简单邮件发送 Date 2017/12/8 项目中需要根据物料资质的状况实时给用 ...

随机推荐

  1. selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

    抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...

  2. 使用IDEA配置SpringBoot热部署无效解决

    首先检查自己pom.xml文件里是否有加入依赖 <dependency> <groupId>org.springframework.boot</groupId> & ...

  3. Nginx事件管理之ngx_event_core_module模块

    1. 概述 ngx_event_core_module 模块是一个事件类型的模块,它在所有事件模块中的顺序是第一位.它主要完成以下两点任务: 创建连接池(包括读/写事件): 决定究竟使用哪些事件驱动机 ...

  4. JDBC——DBHelper代码模版

    JDBC数据库操作代码模版 package com.oolong.util; import java.sql.Connection; import java.sql.DriverManager; im ...

  5. javascript的历史和入门

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 阶段3 3.SpringMVC·_07.SSM整合案例_07.ssm整合之编写MyBatis框架测试保存的方法

    再写一个测试的方法,测试save保存的方法 需要提交事务才能保存到数据库

  7. Python - 数据结构与算法(Data Structure and Algorithms)

    入门 The Algorithms Python https://github.com/TheAlgorithms/Python 从基本原理到代码实现的Python算法入门,简洁地展示问题怎样解决,因 ...

  8. MVC 表单防伪,自定义提示(AntiForgery.Validate)

    再以前的文章解释过@Html.AntiForgeryToken()的部分源码,地址 直奔主题 <form id="form" action=""> ...

  9. git 提交项目到远程仓库,简单实现忽略 node_modules文件

    在项目根目录中创建 .gitignore文件 在文件中添加你要忽略的文件 .DS_Store node_modules /dist # local env files .env.local .env. ...

  10. 调用百度api的原理流程

    1.为了实现酒店地址的定位 2.使用可视化便捷的百度地图API生成器:设置公司的地址和地图等级 3.设置地图的滚轮.缩放功能 4.获取代码,拷贝到html页面中 5.申请秘钥,在html中引用地图AP ...