[자바]smtp프로토콜 이용, 이메일 실제 존재 여부 체크
열심히 만들었지만....;;
완벽히 체크가 안된다..
각 메일 서버 마다 응답하는 형식이 조금 틀릴줄이야..
그래도 가장 많이 쓰는 메일 서버는 체크가 된다.
리눅스에 올려서 사용해야된다. [host -t mx 도메인]명령이 리눅스 명령이라 로컬에서 테스트가 안된다.반드시 리눅스에 올려서 테스트 요망.ㅋ
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
public class test implements Serializable{
private static Socket smtp;
private static BufferedReader input;
private static PrintStream output;
//네이버
private static List smtpServer;
private static String serverReply;
private static int port =25;
private static String from;
private static String to;
private static String subject;
private static String message;
public static boolean mailExistCheck(String email) throws Exception{
String result="fail";
try{
from ="test@test.com";
to=email;
smtpServer=getSTMPServer(to);
System.out.println(smtpServer);
// smtpServer="mx2.hotmail.com";
for(int i=0;i<smtpServer.size();i++){
try{
smtp= new Socket((String)smtpServer.get(i),port);
break;
}catch(Exception e){
}
}
//10초이상 기다리면 잘 빠져 나가기
smtp.setSoTimeout(10000);
input = new BufferedReader(new InputStreamReader(smtp.getInputStream()));
output=new PrintStream(smtp.getOutputStream(),true);
if(email.substring(email.indexOf("@")+1).equals("gmail.com") || email.substring(email.indexOf("@")+1).equals("yahoo.com") || email.substring(email.indexOf("@")+1).equals("yahoo.co.kr") ){
from="<"+from+">";
to="<"+to+">";
}
serverReply=input.readLine();
if(serverReply.charAt(0)=='2' || serverReply.charAt(0)=='3'){
output.println("HELO "+email.substring(email.indexOf("@")+1));
serverReply=input.readLine();
// System.out.println(serverReply);
output.println("MAIL FROM:"+from);
serverReply=input.readLine();
// System.out.println(serverReply);
output.println("RCPT TO:"+to);
serverReply=input.readLine();
// System.out.println(serverReply);
if(serverReply.startsWith("250")){
result="success";
}else{
result="fail";
}
// output.println("DATA");
// serverReply=input.readLine();
// System.out.println(serverReply);
// output.println("test");
// output.println(".");
// serverReply=input.readLine();
// System.out.println(serverReply);
}else{
System.out.println("Error connection to SMTP server "+smtpServer+" on port"+port);
}
}catch(Exception e){
result="fail";
}finally{
output.close();
input.close();
smtp.close();
}
if(result.equals("success")){
return true;
}else{
return false;
}
}
public static void main(String[] args) throws Exception{
long atime=System.currentTimeMillis();
System.out.println("start");
System.out.println("result==="+mailExistCheck(args[0]));
long btime=System.currentTimeMillis();
System.out.println(btime-atime);
}
public static List getSTMPServer(String email) throws Exception{
List<String> result=new ArrayList<String>();
//도메인 가져오기
String domain=email.substring(email.indexOf("@")+1);
String line="";
String status="";
Runtime r= Runtime.getRuntime();
//smtp 서버 가져 오는 리눅스 명령어
String command="host -t mx "+domain;
Process proc= r.exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((line = reader.readLine()) != null) {
StringTokenizer lineResult = new StringTokenizer(line, " ");
lineResult.nextToken();lineResult.nextToken();lineResult.nextToken();lineResult.nextToken();
lineResult.nextToken();
status=lineResult.nextToken();
line=lineResult.nextToken(); //smtp 서버
result.add(line.substring(0,line.length()-1));
}
return result;
}
}