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 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...
随机推荐
- linux环境下编译php扩展
1.使用ext_skel工具生成扩展框架 ./ext_skel --extname=myext 2.编辑config.m4文件 cd myext/vim config.m4 去掉以下内容的注释: PH ...
- 字符串哈希算法(以ELFHash详解)
更多字符串哈希算法请参考:http://blog.csdn.net/AlburtHoffman/article/details/19641123 先来了解一下何为哈希: 哈希表是根据设定的哈希函数H( ...
- windows dos命令
dos命令配置环境变量: path=%path%;D:\Installed software\Professional software\Python27 (https://www.cnblogs ...
- Dataframe 取列名
1.[column for column in df] 2.df.columns.values 返回 array 3.list(df) 4.df.columns 返回Index,可以通过 tolist ...
- .gitignore忽略git版本库中的文件(夹)
# 忽略*.o和*.a文件 *.[oa] # 忽略*.b和*.B文件,my.b除外 *.[bB] !my.b # 忽略dbg文件和dbg目录 dbg # 只忽略dbg目录,不忽略dbg文件 dbg/ ...
- RAC2——11g Grid Infrastructure的新机制
版权声明:本文为博主原创文章,未经博主允许不得转载. 可以看到,最初CRS(Cluster Ready Services)名词的起源就是因为10.1中作为集群软件的原因.后来经历了Clusterwar ...
- POJ3613 k边最短路
题目:http://poj.org/problem?id=3613 Floyd求最短路的实质是矩阵的自乘.( i , k )是第 i 行第k列,( k , j )是第k行第 j 列:用它们的max更新 ...
- openwrt设置默认登陆密码
1.修改dropbear配置文件 找到package/network/services/dropbear/files/dropbear.config 修改如下: config dropbear opt ...
- TroubleShoot: Enable Developer Mode in Windows 10 Insider Preview Build 10074
There is a known issue in Windows 10 Insider Preview build 10074 (see here). Developers cannot enabl ...
- .gitignore 存放位置
放在仓库根目录下即可.比如你的仓库在“D:\MYREPO”,位置就是“D:\MYREPO\.gitignore”. 模板可从GITHUB上COPY一份.