public void sendEmail(String policyno,String to,String isfile,String subject,String emailpath ) throws Exception{
Properties pro = new Properties();
FileInputStream fis=null;
try{
fis = new FileInputStream(Consts.EPOLICY_PROPATH);
pro.load(fis);
pro.put("to", to);
Email email = new Email(pro);
email.setFrom(pro.getProperty("from"));
email.setPassword(pro.getProperty("password"));
email.setSubject(subject);
email.setTo(pro.getProperty("to"));
email.setUsername(pro.getProperty("usernme"));
email.setProps(pro);
Multipart mp = new MimeMultipart();
boolean isfileFlag=false;
if(isfile!=null&&isfile.equals(true)){
isfileFlag=true;
}
String content=getSendContent(emailpath);
String newcontent=content.replaceAll("policyno", policyno+"");
email.createPart(mp, subject, newcontent, false);
/*byte[] arr = null;
email.createPart(mp, "b",arr, true);*/
Session session=email.createSession();
Message msg = email.createMessage(session,mp);
Transport transport=session.getTransport();
transport.connect(session.getProperty("mail.smtp.host"),email.getUsername(),email.getPassword());
transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
//Transport.send(msg);
}catch(FileNotFoundException e){
throw new Exception ("获取配置文件失败"+e.getMessage());
}catch(IOException e){
throw new Exception ("获取配置文件失败"+e.getMessage());
}catch(MessagingException e){
e.printStackTrace();
throw new Exception ("发送邮件失败"+e.getMessage());
}
}
private static boolean print=true;
public static void print(String info){
PrintWriter pw=new PrintWriter(System.out);
if(print){
pw.println(info);
}
}
public void sendSMS(String policyno,String mobileno,String smspath,String expired) throws Exception{
try {
ConfigUtil.init();
} catch (Exception e) {
throw new Exception("短信初始化失败"+e.getMessage());
}
SmsSocket sms = new SmsSocket();
//设置发送时间
// 获取yyyy-MM-dd HH:mm:ss
String time=DateUtil.getSysTime();
//转换为long + 60*60*1000*12
long start=DateUtil.TimeFormat(time);
String startnot=DateUtil.getSysDate()+" 08:00:00";
print(startnot);
long lstartnot=DateUtil.TimeFormat(startnot);
String endnot=DateUtil.getSysDate()+" 20:00:00";
long lendnot=DateUtil.TimeFormat(endnot);
SimpleDateFormat dateFormatter = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
if(start>lstartnot&&start<lendnot){
System.out.println("早八点至晚八点之间立即发送");
// 转换为string
DateUtil.formatTime(start);
long end=start+60*60*1000*Integer.parseInt(expired);
//java.util.Date endDate=DateUtil.getTimforString(DateUtil.formatDate(end));
//String endStr=dateFormatter.format(endDate);
java.util.Date startDate=new Date(start);
String startStr=dateFormatter.format(startDate);
print("startStr:"+startStr);
sms.setSmssendtime(startStr);
java.util.Date endDate=new Date(end);
String endStr=dateFormatter.format(endDate);
print("endStr:"+endStr);
sms.setSmsendtime(endStr);
}else{
System.out.println("早八点之前至晚八点以后推迟12小时发送");
// 转换为string
DateUtil.formatTime(start);
start=start+60*60*1000*12;
long end=start+60*60*1000*Integer.parseInt(expired);
//java.util.Date endDate=DateUtil.getTimforString(DateUtil.formatDate(end));
java.util.Date startDate=new Date(start);
String startStr=dateFormatter.format(startDate);
print("startStr:"+startStr);
sms.setSmssendtime(startStr);
java.util.Date endDate=new Date(end);
String endStr=dateFormatter.format(endDate);
print("endStr:"+endStr);
sms.setSmsendtime(endStr);
}
ArrayList listphones = new ArrayList();
listphones.add(mobileno);
String ls_dateTime = DateUtil.getSysTime();
ls_dateTime = ls_dateTime.replaceAll("-", "");
ls_dateTime = ls_dateTime.replaceAll(":", "");
ls_dateTime = ls_dateTime.replace(' ', '_');
String content=getSendContent(smspath);
String newcontent=content.replaceAll("policyno", policyno+"");
sms.sendSMS(newcontent, listphones, expired+ls_dateTime);
}
public String getSendContent(String path) throws Exception{
FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br = null;
StringBuffer sb=new StringBuffer();
try{
fis=new FileInputStream(path);
isr=new InputStreamReader(fis, "GBK");
br = new BufferedReader(isr);
String value =br.readLine();
while(value!=null&&value.trim().length()>1){
sb.append(value);
value=br.readLine();
}
return sb.toString();
}catch(Exception e){
throw new Exception("获取发送内容失败:"+e.getMessage());
}finally{
if(fis!=null){
fis.close();
}
if(isr!=null){
isr.close();
}
if(br!=null){
br.close();
}
}
}