springboot email 中常量值 配置 mailUtils
列如:邮件配置:
application-test.properties
#################Email config start###############################
MAIL_SMTPSERVER=192.168.3.253
##if it is more than one,please separating it by "," For example "aa@icil.net,bb@icil.net"
MAIL_SEND_TO=qqq.@163.com
MAIL_FROM=www@qq.net
MAIL_SEND_TO_FAIL=afsa2_test@qq.net #################Email config end###############################
对应的类:
@Component
public class EmailConfig { private static String MAIL_SMTPSERVER; private static String MAIL_SEND_TO; private static String MAIL_FROM; private static String MAIL_SEND_TO_FAIL; public static String getMAIL_SMTPSERVER() {
return MAIL_SMTPSERVER;
} @Value("${MAIL_SMTPSERVER}")
public void setMAIL_SMTPSERVER(String mAIL_SMTPSERVER) {
MAIL_SMTPSERVER = mAIL_SMTPSERVER;
} public static String getMAIL_SEND_TO() {
return MAIL_SEND_TO;
} @Value("${MAIL_SEND_TO}")
public void setMAIL_SEND_TO(String mAIL_SEND_TO) {
MAIL_SEND_TO = mAIL_SEND_TO;
} public static String getMAIL_FROM() {
return MAIL_FROM;
} @Value("${MAIL_FROM}")
public void setMAIL_FROM(String mAIL_FROM) {
MAIL_FROM = mAIL_FROM;
} public static String getMAIL_SEND_TO_FAIL() {
return MAIL_SEND_TO_FAIL;
} @Value("${MAIL_SEND_TO_FAIL}")
public void setMAIL_SEND_TO_FAIL(String mAIL_SEND_TO_FAIL) {
MAIL_SEND_TO_FAIL = mAIL_SEND_TO_FAIL;
} }
MailUtils:
package com.icil.esolution.utils; import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.icil.esolution.config.EmailConfig; /**
* ********************************************
*
* @ClassName: MailUtils
*
* @Description:
*
* @author Sea
*
* @date 24 Aug 2018 8:34:09 PM
*
***************************************************
*/
@SuppressWarnings("all")
public class MailUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(MailUtils.class);
private static final String MAIL_SEND_TO_FAIL="MAIL_SEND_TO_FAIL";
private Vector file = new Vector();// 附件文件集合 public static boolean sendMail(String subject, String content) {
boolean mails = new MailUtils().sendMails(null, null, subject, content, null);
return mails;
}
/**
* this use default config
* @param subject
* @param content
* @return
*/
public static boolean sendFailedMail(String subject, String content) {
//String tofail = properties.getProperty(MAIL_SEND_TO_FAIL);
//String tocommon = properties.getProperty(Constant.MAIL_SEND_TO);
String tofail = EmailConfig.getMAIL_SEND_TO_FAIL();
String tocommon = EmailConfig.getMAIL_SEND_TO();
boolean mails = new MailUtils().sendMails(tofail+","+tocommon, null, subject, content, null);
return mails;
} public static boolean sendMail(String to, String subject, String content) {
boolean mails = new MailUtils().sendMails(to, null, subject, content, null);
return mails;
} public static boolean sendMail(String to, String from, String subject, String content) {
boolean mails = new MailUtils().sendMails(to, from, subject, content, null);
return mails;
} /**
* @return
* @Description: 发送邮件,发送成功返回true 失败false
* @author sea
* @Date 2018年8月3日
*/
private boolean sendMails(String to1, String from1, String subject, String content, String filename) { String to = EmailConfig.getMAIL_SEND_TO();
String from = EmailConfig.getMAIL_FROM();
String host = EmailConfig.getMAIL_SMTPSERVER();
LOGGER.info("to is {}",to);
LOGGER.info("from is {}",from);
LOGGER.info("host is {}",host);
String username = "";
String password =""; if (to1 != null) {
to = to1;
}
if (from1 != null) {
from = from1;
}
if(content==null){
content = "";
}
// 构造mail session
Properties props = new Properties();
props.put("mail.smtp.host", host);
// props.put("mail.smtp.port", "25");//
props.put("mail.smtp.auth","false"); // Session session = Session.getDefaultInstance(props, new Authenticator() {
// public PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(username, password);
// }
// });
Session session=Session.getDefaultInstance(props);
try {
// 构造MimeMessage 并设定基本的值
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from)); // msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to));
// msg.addRecipients(Message.RecipientType.CC, address);
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
subject = transferChinese(subject);
msg.setSubject(subject); // 构造Multipart
Multipart mp = new MimeMultipart(); // 向Multipart添加正文
MimeBodyPart mbpContent = new MimeBodyPart();
mbpContent.setContent(content, "text/html;charset=utf-8"); // 向MimeMessage添加(Multipart代表正文)
mp.addBodyPart(mbpContent); // 向Multipart添加附件
Enumeration efile = file.elements();
while (efile.hasMoreElements()) { MimeBodyPart mbpFile = new MimeBodyPart();
filename = efile.nextElement().toString();
FileDataSource fds = new FileDataSource(filename);
mbpFile.setDataHandler(new DataHandler(fds));
filename = new String(fds.getName().getBytes(), "ISO-8859-1"); mbpFile.setFileName(filename);
// 向MimeMessage添加(Multipart代表附件)
mp.addBodyPart(mbpFile); } file.removeAllElements();
// 向Multipart添加MimeMessage
msg.setContent(mp);
msg.setSentDate(new Date());
msg.saveChanges(); // 发送邮件
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (Exception mex) {
mex.printStackTrace();
return false;
}
return true;
} /**
* @param strText
* @return
* @Description: 把主题转为中文 utf-8
* @Date 2017年6月22日 上午10:37:07
*/
public String transferChinese(String strText) {
try {
strText = MimeUtility.encodeText(new String(strText.getBytes(), "utf-8"), "utf-8", "B");
} catch (Exception e) {
e.printStackTrace();
}
return strText;
} public void attachfile(String fname) {
file.addElement(fname);
} /**
*
* @Description:test ok 2018-08-24
*
* @author Sea
*
* @date 24 Aug 2018 8:34:09 PM
*/
public static void main(String[] args) {
boolean mails = new MailUtils().sendMails("", "", "hello", "I love you icil", null);
System.out.println(mails);
} }
springboot email 中常量值 配置 mailUtils的更多相关文章
- springboot配置文件中使用当前配置的变量
在开发中,有时我们的application.properties某些值需要重复使用,比如配置redis和数据库或者mongodb连接地址,日志,文件上传地址等,且这些地址如果都是相同或者父路径是相同的 ...
- SpringBoot项目中遇到的BUG
1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...
- springBoot中实现自定义属性配置、实现异步调用、多环境配置
springBoot中其他相关: 1:springBoot中自定义参数: 1-1.自定义属性配置: 在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性 ...
- IntelliJ IDEA 中SpringBoot对Run/Debug Configurations配置 SpringBoot热部署
运行一个SpringBoot多模块应用 使用SpringBoot配置启动: Use classpath of module选中要运行的模块 VM options:内部配置参数 -Dserver.por ...
- VC++ 在两个程序中 传送字符串等常量值的方法:使用了 WM_COPYDATA 消息(转载)
转载:http://www.cnblogs.com/renyuan/p/5037536.html VC++ 在两个程序中 传递字符串等常量值的方法:使用了 WM_COPYDATA 消息的 消息作用: ...
- 详解Springboot中自定义SpringMVC配置
详解Springboot中自定义SpringMVC配置 WebMvcConfigurer接口 这个接口可以自定义拦截器,例如跨域设置.类型转化器等等.可以说此接口为开发者提前想到了很多拦截层面的需 ...
- Spring-Boot项目中配置redis注解缓存
Spring-Boot项目中配置redis注解缓存 在pom中添加redis缓存支持依赖 <dependency> <groupId>org.springframework.b ...
- SSIS 实例——将SQL获取的信息传递到Email中
最近在为公司财务开发一个邮件通知时遇到了一个技术问题.原来我设计SSIS的是每天将ERP系统支付数据导出到财务支付平台后 Email 通知财务,然后财务到支付平台上进行支付操作.由于那个时候开发时间很 ...
- Spring 中的 Bean 配置
内容提要 •IOC & DI 概述 •配置 bean –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...
随机推荐
- Windows10 解决“装了 .NET Framework 4.5.2/4.6.1/4.7.1等等任何版本 或版本更高的更新”问题
========================================================= 系统自带的.net framework版本为4.7,自己安装.NET Framewo ...
- CTF-练习平台-Misc之 Linux??????
八.Linux?????? 下载文件,解压后只得到一个没有后缀名的文件,添加后缀名为txt,打开搜索,关键词为“flag”,没有找到:改关键词为“key”得到答案
- hdu2069-2071
hdu2069 选取硬币组成定值,暴力 #include<stdio.h> ]={,,,,,}; int main(){ int n; while(scanf("%d" ...
- MyBatis-动态SQL的if、choose、when、otherwise、trim、where、set、foreach使用(各种标签详解), 以及实体间关系配置
比较全的文档:https://www.cnblogs.com/zhizhao/p/7808880.html 或 https://blog.csdn.net/zhll3377/article/detai ...
- ExtJs 4.0 DeskTop集成 百度地图API
经过3天的奋斗最终搞了出来, 网上的资料非常少,希望小⑦的文章对读者有点帮助,PS:小⑦非常努力的~. 不废话,上代码了. 首先.去百度官网Copy一个模版 http://api.map.baidu. ...
- FastAdmin 绑定的模块禁用路由
为了安全,将后台入口隐藏. 这里出一个问题,因为装了 CMS 插件,使用入口登录后显示的是 CMS 的首页. 这个问题已经修复. https://gitee.com/karson/fastadmin/ ...
- load-display-image之c#版
基本功能 能够从文件load图像 -->显示图像-->在图像上方显示graphics,比如几条线-->鼠标移动,显示鼠标位置的灰度 load-display-image之c#版 lo ...
- yum下载文件的存放位置
yum下载文件的存放位置 默认是: /var/cache/yum 也可以在 /etc/yum.conf 指定 cachedir=/var/cache/yum #存放目录keepcache=1 # ...
- TFS撤销其他人的迁出
1.cd C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE 2.查看工作区tf workspaces /owner:zho ...
- 新玩具,React v16.7.0-alpha Hooks
周五看见React v16.7.0-alpha Hooks,今早起来看见圈里已经刷屏了Hooks,正好周末,正好IG和G2的比赛还没开始,研究下... 刚刚接触react时候非常喜欢用函数式组件,因为 ...