java email smtp
来源:互联网 发布:数据库管理系统的是 编辑:程序博客网 时间:2024/06/07 09:50
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();
}
}
}
- java email smtp
- SMTP发送Email简单演示
- Redmine EMAIL /SMTP 服务器设置
- Python发送邮件(Email SMTP)
- EMAIL之SMTP应答序列
- Python QQ 发送Email SMTP
- SMTP 协议,验证 Email 地址
- java发送email错误(454 Authentication failed, please open smtp flag first!)
- Wordpress 的SMTP的邮件发送 插件WP Email SMTP
- EMAIL发送系统(C#+基于SMTP认证)
- EMAIL发送系统(C#+基于SMTP认证)
- EMAIL发送系统(C#+基于SMTP认证)
- EMAIL发送系统(C#+基于SMTP认证)
- EMAIL发送系统(C#+基于SMTP认证)
- Email基础知识: SMTP/POP3 命令简介
- EMAIL发送系统(C#+基于SMTP认证)
- IIS 的smtp 不能发送email解决方法
- 转:Email基础知识: SMTP/POP3 命令简介
- 就地逆转输出链表
- 缺陷跟踪系统简介
- JBOSS最大连接数配置和jvm内存配置
- java 发送email
- MS SQL SERVER 2005 MDF文件单独附加
- java email smtp
- Linux/Unix基础
- vi编辑器入门
- 【转、修改】LINUX JAVA环境变量配置 和LINUX Tomcat安装
- S5PV210支持DM9000驱动(Android2.2)
- 函数可变参数
- 二八定律
- IMG ONLOAD 事件加载程序
- Add Google Admob in Android Application