列如:邮件配置:

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的更多相关文章

  1. springboot配置文件中使用当前配置的变量

    在开发中,有时我们的application.properties某些值需要重复使用,比如配置redis和数据库或者mongodb连接地址,日志,文件上传地址等,且这些地址如果都是相同或者父路径是相同的 ...

  2. SpringBoot项目中遇到的BUG

    1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...

  3. springBoot中实现自定义属性配置、实现异步调用、多环境配置

    springBoot中其他相关: 1:springBoot中自定义参数: 1-1.自定义属性配置: 在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性 ...

  4. IntelliJ IDEA 中SpringBoot对Run/Debug Configurations配置 SpringBoot热部署

    运行一个SpringBoot多模块应用 使用SpringBoot配置启动: Use classpath of module选中要运行的模块 VM options:内部配置参数 -Dserver.por ...

  5. VC++ 在两个程序中 传送字符串等常量值的方法:使用了 WM_COPYDATA 消息(转载)

    转载:http://www.cnblogs.com/renyuan/p/5037536.html VC++ 在两个程序中 传递字符串等常量值的方法:使用了 WM_COPYDATA 消息的 消息作用:  ...

  6. 详解Springboot中自定义SpringMVC配置

    详解Springboot中自定义SpringMVC配置 WebMvcConfigurer接口 ​ 这个接口可以自定义拦截器,例如跨域设置.类型转化器等等.可以说此接口为开发者提前想到了很多拦截层面的需 ...

  7. Spring-Boot项目中配置redis注解缓存

    Spring-Boot项目中配置redis注解缓存 在pom中添加redis缓存支持依赖 <dependency> <groupId>org.springframework.b ...

  8. SSIS 实例——将SQL获取的信息传递到Email中

    最近在为公司财务开发一个邮件通知时遇到了一个技术问题.原来我设计SSIS的是每天将ERP系统支付数据导出到财务支付平台后 Email 通知财务,然后财务到支付平台上进行支付操作.由于那个时候开发时间很 ...

  9. Spring 中的 Bean 配置

    内容提要 •IOC & DI 概述 •配置 bean –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...

随机推荐

  1. Linux Shell查看物理CPU个数、核数、逻辑CPU个数

    Linux Shell常用命令: ====================================== # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理C ...

  2. hdu2083 简易版之最短距离 排序水题

    给出数轴n个坐标,求一个点到所有点距离总和最小.排序后最中间一个点或两个点之间就是最优 #include<stdio.h> #include<algorithm> using ...

  3. SQLite数据库学习小结——Frameworks层实现

    3. SQLite的Frameworks层实现 3.1 Frameworks层架构 Android系统方便应用使用,在Frameworks层中封装了一套Content框架,之所以叫Content框架而 ...

  4. 升级CentOS 7.4内核版本的三种方案

    https://blog.csdn.net/breeze915/article/details/79243673 在实验环境下,已安装了最新的CentOS 7.4操作系统,现在需要升级内核版本. 实验 ...

  5. Excel 从字符串中提取日期值

    因为工作需要,Excel 表中有一串字符,需要将字符里的日期提取出,并转成日期值. 需要转成如下格式: 可使用以下公式. =DATEVALUE(TEXT(MID(I2,1,4)+1&" ...

  6. ORA-10997:another startup/shutdown operation of this instance in progress解决方法

    SQL> startup ORA-10997: another startup/shutdown operation of this instance inprogress ORA-09967: ...

  7. Maven的dependency type属性

    官方地址: http://maven.apache.org/ref/3.5.2/maven-model/maven.html (搜索:Some examples are jar, war, ejb-c ...

  8. css 通用兄弟选择器( ~ )

    stylus设置兄弟元素样式: 鼠标浮动在 .video-li 元素上时,.video-li 兄弟中 .video-info 下的 .word 显示. .video-li &:hover ~ ...

  9. 查看 linux cpu 、内存、服务器型号和序列号、磁盘、raid 的信息

    yum -y install dmidecode 查看cpu的型号: 查看cpu的颗数:dmidecode -t processor |grep "Version"dmidecod ...

  10. 关于有时候导入maven项目时候报错(有红色叹号,类中导入的包提示"the import java.util cannot be resolve,")

    ------解决方案--------------------解决方案:右键项目-------buildpath--------最下面那个configura...的选择libraries找到JRE(这个 ...