페이지

2014년 3월 20일 목요일

sendmail client source

MakeContentsAndSend.java

public class MakeContentsAndSend {
    
    public static void main(String[] args) throws Exception {
        MakeContentsAndSend.sendTemporaryPassword("scoutreturns");
    }
    
    
     
    public static void sendContents(String str1) throws Exception{
        
        StringBuffer content = new StringBuffer();
        content.append("<html>");
        content.append("<head>");
        content.append("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />");
        content.append("<meta charset=\"utf-8\" />");
        content.append("<title> test </title>");
        content.append("</head>");
        
        content.append("<body>");
        content.append("<table width=600 class=MsoTableGrid border=0 cellspacing=0 cellpadding=0 style='border-collapse:collapse;border:none'>");
        content.append("<tr>");
        content.append("<td align=center height=30>");
        content.append("<b><span style='font-size:14pt'>메일 테스트</span><b>");
        content.append("</td>");
        content.append("</tr>");
        content.append("<tr>");
        content.append("<td style='height:2pt; background:silver'>");
        content.append("</td>");
        content.append("</tr>");
        content.append("</table>");
        content.append("<br><br>");
        content.append("</body>");
        content.append("</html>");
        
      
        new MailSend( "111.222.333.444", 
                      "fromName", 
                      "toName" ,
                      "fromEmail", 
                      "toEmail" ,
                      "title" ,
                      content.toString()).send();
    }
}


MailSend.java

import java.net.*;
import java.io.*;

public class MailSend {
    
    private static String smtpServer    = null;
    private static String sendNm        = null;
    private static String rcptNm        = null;
    private static String sendId        = null;
    private static String rcptId        = null;
    private static String title         = null;
    private static String content       = null;

    public MailSend(
        String _smtpServer, 
        String _sendNm, 
        String _rcptNm, 
        String _sendId, 
        String _rcptId, 
        String _title, 
        String _content
    ) {
        this.smtpServer = _smtpServer;
        this.sendNm     = _sendNm;
        this.rcptNm     = _rcptNm;
        this.sendId     = _sendId;
        this.rcptId     = _rcptId;
        this.title      = _title;
        this.content    = _content;
    }

    public synchronized int send() throws Exception {

        Socket socket=new Socket(smtpServer, 25);
        BufferedReader br=new BufferedReader(new InputStreamReader( socket.getInputStream(),"EUC-KR" ) );
        PrintWriter pw=new PrintWriter( new OutputStreamWriter(socket.getOutputStream(),"EUC-KR"), true );
        
        System.out.println("서버에 연결되었습니다.");
        
        String line=br.readLine();
        System.out.println("응답:"+line);
        
        if (!line.startsWith("220")) {
            //throw new Exception("SMTP서버가 아닙니다:"+line);
            System.out.println("SMTP서버가 아닙니다:"+line);
            return 1;
        }


/*
        System.out.println("HELO 명령을 전송합니다.");
        pw.println("HELO");
        line=br.readLine();
        System.out.println("응답:"+line);
        if (!line.startsWith("250")) throw new Exception("HELO 실패했습니다:"+line);
*/
        System.out.println("MAIL FROM 명령을 전송합니다.");
        
        pw.println("mail from : <AAA:"+sendId+"@test.co.kr>");
        
        line=br.readLine();
        
        System.out.println("응답:"+line);
        
        if (!line.startsWith("250")) {
            //throw new Exception("MAIL FROM 에서 실패했습니다:"+line);
            System.out.println("MAIL FROM 에서 실패했습니다:"+line);
            return 2;
        }
        
        System.out.println("RCPT 명령을 전송합니다.");
        pw.println("rcpt to : "+rcptId);
        line=br.readLine();
        System.out.println("응답:"+line);
        if (!line.startsWith("250")) {
            //throw new Exception("RCPT TO 에서 실패했습니다:"+line);
            System.out.println("RCPT TO 에서 실패했습니다:"+line);
            return 3;
        }
        
        System.out.println("DATA 명령을 전송합니다.");
        pw.println("data 제목");
        line=br.readLine();
        System.out.println("응답:"+line);
        if (!line.startsWith("354")) {
            //throw new Exception("DATA 에서 실패했습니다:"+line);
            System.out.println("DATA 에서 실패했습니다:"+line);
            return 4;
        }
        
        System.out.println("본문을 전송합니다.");
        pw.println("From: "+sendNm+" <"+sendId+">");
        pw.println("To: "+rcptNm+" <"+rcptId+">");
        pw.println("Subject: "+title);
        pw.println("Content-Type: text/html;");
        
        //pw.println(content);
        pw.println("\r");
        pw.println(content);
        pw.println(".");
        line=br.readLine();
        System.out.println("응답:"+line);
        if (!line.startsWith("250")) {
            //throw new Exception("내용전송에서 실패했습니다:"+line);
            System.out.println("내용전송에서 실패했습니다:"+line);
            return 5;
        }
        
        System.out.println("접속 종료합니다.");
        pw.println("quit");
        
        br.close();
        pw.close();
        socket.close();
        
        return 0;
    }
    
  
  
}

댓글 없음:

댓글 쓰기

image

image