📄 emailpush.java
字号:
/**
* Created at Dec 4, 2008
*/
package com.jdev.app.db;
import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import com.jdev.db.ConnectPool;
import com.jdev.mail.SmtpMailSender;
import com.jdev.net.connector.ConnectionFactory;
import com.jdev.net.event.Notifier;
import com.jdev.util.Debug;
import com.jdev.util.LogManager;
/**
* <p>Title: EmailPush</p>
* <p>Description: </p>
* @author Lawrence
* @version
*/
public class EmailPush implements Runnable {
private static Notifier notifier = Notifier.getNotifier();
private EmailQueueHelper emailDataHelper;
private String smtp = "smtp.sohu.com";
private String sender = "\"合智创展\"<gwt-tech@sohu.com>";
private String user = "gwt-tech";
private String pass = "gwtgwt";
private java.sql.Connection conn;
private ConnectPool connectionManager;
/**
*
*/
public EmailPush(String smtp, String sender, String user, String pass) {
connectionManager = ConnectPool.getInstance();
emailDataHelper = new EmailQueueHelper(ConnectionFactory.CLIENT);
this.smtp = smtp;
this.sender = sender;
this.user = user;
this.pass = pass;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
conn = connectionManager.getConnection("test");
} catch (Exception e) {
notifier.fireOnError("-->Error occured in ReadMailText openDb: "
+ e.getMessage());
}
while (!Thread.interrupted()) {
try {
Thread.sleep(10000);
if (conn == null || conn.isClosed()) {
Thread.sleep(10000);
try {
conn = connectionManager.getConnection("test");
} catch (Exception e) {
notifier.fireOnError("-->Error occured in DbReader openDb: "
+ e.getMessage());
}
continue;
}
String strOut = emailDataHelper.readSend();
// Debug.logVerbose(strOut);
String[] s = strOut.split(";");
if(s.length != 5) continue;
SmtpMailSender sms=SmtpMailSender.createESmtpMailSender(smtp,sender,user,pass);
sms.addLogManager(new LogPrinter());//添加日志管理器
String recv = "<" + s[4] + ">";
recv = "cindy@yeootut.cn";
boolean b = sms.sendTextMail(recv,recv,recv);
if(b == SmtpMailSender.SUCCESSFUL) {
notifier.fireOnStatus(strOut, Integer.parseInt("0"));
String str = new String(s[2].replaceAll("'", "''"));
// update(s[4],str,s[3],s[3],s[1],0);
}
else {
notifier.fireOnStatus(strOut, Integer.parseInt("1"));
String str = new String(s[2].replaceAll("'", "''"));
// update(s[4],str,s[3],s[3],s[1],1);
}
// 发送短信
// String[] s = strOut.split(",");
// if(s.length == 3){
// String[] callees = new String[]{s[0]};
// String isReturn = "0";
// int msgId = 136744;
//
// strOut = s[1];
// SmtpMailSender sms=SmtpMailSender.createESmtpMailSender("smtp.sohu.com","\"合智创展\"<gwt-tech@sohu.com>","gwt-tech","gwtgwt");
// sms.addLogManager(new LogPrinter());//添加日志管理器
//
//// String[] recv = {"\"杨汉瑜\"<jackyhy@263.net>","\"曾光\"<vma@china.com>","\"李贵生\"<guisheng.li@163.com>","\"李贵生\"<ramon_bird@163.com>","\"尹武\"<google7384@126.com>","\"林锦\"<zerosharp@gmail.com>","\"潘希兴\"<pxx5880547@sina.com>","\"周翔\"<changeasy@tom.com>"};
//// boolean[] b = sms.sendTextMail(recv,"SMTP邮件测试","这是一封测试邮件。",new File[]{new File("java.gif")});
// boolean b = sms.sendTextMail("\"杨汉瑜\"<jackyhy@263.net>","SMTP邮件测试",s[2]);
// if(b == SmtpMailSender.SUCCESSFUL) {
// notifier.fireOnStatus(strOut, Integer.parseInt("0"));
// }
// else
// notifier.fireOnStatus(strOut, Integer.parseInt("1"));
//
// }
// conn.close();
} catch (Exception e) {
notifier.fireOnError("-->Error occured in SmsPush: "
+ e.getMessage());
continue;
}
}
}
private void update(String addr, String body, String audi, String cate, String title,int stat) throws Exception {
java.sql.Statement stmt = null;
try {
// 3. 获取表达式
stmt = conn.createStatement();
StringBuilder strUpdate = new StringBuilder(
"insert into PUSH_MAIL_BILL(MAIL_ADDR,MSG,SEND_STAT,AUDI_ID,AUDI_CATE,MAIL_TITLE) values('");
strUpdate.append(addr);
strUpdate.append("','");
strUpdate.append(body);
strUpdate.append("',");
// strUpdate.append("to_date(sysdate,'yyyymmddhh24miss')");
// strUpdate.append(",");
strUpdate.append(stat);
strUpdate.append(",'");
strUpdate.append(audi);
strUpdate.append("','");
strUpdate.append(cate);
strUpdate.append("','");
strUpdate.append(title);
strUpdate.append("')");
Debug.logVerbose(strUpdate.toString());
stmt.executeUpdate(strUpdate.toString());
// 6. 释放资源
stmt.close();
} catch (Exception e) {
notifier.fireOnError("-->Error occured in DbClient onStatus: "
+ e.getMessage());
try {
stmt.close();
} catch (SQLException e1) {
notifier.fireOnError("-->Error occured in DbClient stmt.close: "
+ e.getMessage());
}
}
}
}
/**
* 一个简单的日志管理器。
*/
class LogPrinter implements LogManager
{
public void output(String info)
{
System.out.println(info);//将日志打印到控制台
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -