邮件反垃圾反病毒

反病毒 http://www.clamav.net/
反垃圾 http://spamassassin.apache.org/




private int timeout = 40000;

private static int buffsize = 1024;

// -----------------------------------------------------
private String spamIp;

private String clamIp;

private int spamPost;

private int clamPost;

// -----------------------------------------------------
/**
* 核查垃圾邮件
*
* @param message
* @return
*/
public boolean checkGarbageMail(Message message, long accountId) {
Socket socket = null;
OutputStream out = null;
BufferedReader in = null;

boolean isGarbage = false;
try {
socket = new Socket(spamIp, spamPost);
socket.setSoTimeout(timeout);
out = socket.getOutputStream();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write("CHECK SPAMC/1.2\r\n\r\n".getBytes()); // CHECK
// SPAMC/1.2\r\n\r\n
// 为Spam命令
// pass the message to spamd
message.writeTo(out);
out.flush();
socket.shutdownOutput();
String s = null;
while ((s = in.readLine()) != null) {
logger.debug("Get [spamd] scan result:" + s);
if (s.startsWith("Spam:")) {
s = s.split("\\;")[0].split("\\:")[1].trim();
isGarbage = (s.equals("True"));
}
}
} catch (Exception e1) {
logger.error("connect spam is error! accountId:" + accountId, e1);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
return isGarbage;
}

/**
* 反病毒扫描
*
* @param message
* @return
*/
public String checkVirusMail(Message message, long accountId) {
Socket socket = null;
Socket uploadFileSocket = null;
OutputStream out = null;
OutputStream ufout = null;
BufferedReader in = null;

String virusName = null;
try {
socket = new Socket(clamIp, clamPost);
socket.setSoTimeout(timeout);
out = socket.getOutputStream();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write("STREAM".getBytes()); // STREAM 为clam命令
out.flush();
socket.shutdownOutput();
String s = null;
if ((s = in.readLine()) != null) {
s = s.split("\\ ")[1].trim();
}
logger.debug("Get clamd uploadFile port:" + s);

uploadFileSocket = new Socket(clamIp, Integer.parseInt(s));
uploadFileSocket.setSoTimeout(timeout);
ufout = uploadFileSocket.getOutputStream();
message.writeTo(ufout);
ufout.flush();
uploadFileSocket.shutdownOutput();
String s2 = null;
while ((s2 = in.readLine()) != null) {
logger.debug("Get [clamd] scan result:" + s2);
s2 = s2.split(":")[1].trim().split("\\ ")[0].trim();
if (!s2.equals("OK")) {
virusName = s2;
}
}
} catch (Exception e1) {
logger.error("scan virus is error! accountId:" + accountId, e1);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (ufout != null) {
try {
ufout.close();
} catch (IOException e) {
}
}
if (uploadFileSocket != null) {
try {
uploadFileSocket.close();
} catch (IOException e) {
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
return virusName;
}

你可能感兴趣的:(apache,.net,socket)