JimmyÀÇ È¨ÆäÀÌÁö > Java > Smtp 3.smtp protocol

       

import java.io.*;
import java.util.*;
import java.net.*;
import java.util.StringTokenizer;


public class SmtpMail {

 final int     SERV_TCP_PORT  =  25;
 Socket smtpSocket = null;
 BufferedReader  smtpIn  = null;
 PrintWriter smtpOut = null;
 String boundary = null;

 public SmtpMail()
 {
  makeBoundary();
 }

 public void makeBoundary()
 {
   StringBuffer temp_boundary = new StringBuffer();
   temp_boundary.append("thismustbechangedassoonas");
   boundary = temp_boundary.toString();
 }

 public boolean getMessage (String code)
 {
  String  msg = null;
  int    n ;
  boolean ret = false;

  try {
   msg = smtpIn.readLine() ;
   System.out.println(msg);
   ret = msg.startsWith(code);
  } catch(IOException e) {
   System.err.println("Couldn't get I/O for ");
   System.exit(1);
  } finally{
   return ret;
  }
 }

 public void transInit(String host){
   try {
     smtpSocket = new Socket(host, 25);
     smtpIn     = new BufferedReader(new InputStreamReader(
                      smtpSocket.getInputStream()));
     smtpOut    = new PrintWriter(smtpSocket.getOutputStream(),true);
   } catch (UnknownHostException e) {
     System.err.println("Don't know about host: taranis.");
     System.exit(1);
   } catch (IOException e) {
      System.err.println("Couldn't get I/O for "
            + "the connection to: taranis.");
                      System.exit(1);
   }
   if(!getMessage("220")){
    System.out.println("Not responding from mail server !!") ;
    System.exit(1);
   }
 }

 public void transHelo(String domain)
 {
  String    helo ;
  helo = "helo " + domain;
  System.out.println(helo);
  smtpOut.println( helo) ;
  smtpOut.flush();
  if(!getMessage("250")){
   System.out.println("Not helo message !!") ;
   System.exit(1);
  }
 }


 public void transMailfrom(String email)
 {
  String    mailfrom = "mail from: " + email;
  System.out.println( mailfrom);
  smtpOut.println( mailfrom);
  smtpOut.flush();
  if(!getMessage("250")){
   System.out.println("Inappropriate sender="+email+"!!");
   //System.exit(1);
  }
 }

public void transRcptto(String to)
 {
  String rcptto = "rcpt to: " + to;
  System.out.println(rcptto);
  smtpOut.println(rcptto);
  smtpOut.flush();
  if(!getMessage("250")){
    System.out.println("<b>Above email is inappropriate mail accout!!</b><br>")
;
  }
 }


 public void transDataStart()
 {
  smtpOut.println("data");
  if(!getMessage("354")){
   System.out.println("Could not sending data !!<p>") ;
   return ;
  }
 }
 
  public void sendHead(String name, String email, String subject)
 {
  smtpOut.println("from:" +  name + "<" +  email + ">");
  smtpOut.println("to:" +  email);
  // ÂüÁ¶
  // smtpOut.println("cc:" +  email);
  // ¼ûÀº ÂüÁ¶
  // smtpOut.println("bcc:" +  email);
  smtpOut.println("subject:" +  subject);

  smtpOut.println("content-type:multipart/mixed; boundary=" + boundary);
 }

 public void sendText(String data){
  sendText(data, "text/plain");
 }

 public void sendText(String data, String mime){
  smtpOut.println("--" + boundary);
  smtpOut.println("content-type: " + mime +  ";");
  smtpOut.println("charset=iso-8859-1");
  smtpOut.println("content-transfer-encoding: 8 bit");
  smtpOut.println("");
  smtpOut.println(data);
  smtpOut.println("");
 }

 void sendFile(String path)
 {
                  //"text/plain";
  sendFile(path, "application/octet-stream");
 }

void sendFile(String path, String mime)
 {
   if(!path.startsWith("/")){
    System.err.println("ÆÄÀϸíÀº Àý´ëÆÐ½º·Î ½ÃÀ۵Ǿî¾ßÇÕ´Ï´Ù. (ex: /file )");
    System.exit(0);
   }

   StringTokenizer st =
     new StringTokenizer(path, "/");

   String fname = null;
   while (st.hasMoreTokens()) {
     fname = st.nextToken();
   }

  String name = "name = " + fname;
  String file = "filename = " + fname;
  Base64 base64  = new Base64();
  smtpOut.println("--" + boundary);
  smtpOut.println("content-type: "+ mime + "; " + name) ;
  smtpOut.println("content-transfer-encoding: base64") ;
  smtpOut.println("content-disposition: inline; " + file );
  smtpOut.println("");
  smtpOut.print( base64.encodeFromFile(fname) );
 }

 public void transDataEnd(){
  smtpOut.println("");
  smtpOut.println("--" + boundary + "--");
  smtpOut.println(".");
  if(!getMessage("250")){
   System.out.println("Could not sending data !!<p>") ;
   return ;
  }
 }

 public void transQuit(){
  smtpOut.println("quit");
  if(!getMessage("221")){
   System.out.println("Could not quit !!<p>") ;
   return ;
  }
 }

}

------------------------------------------
¼³¸í:
smtp¸ÞÀÏ Àü¼Û ¸ðµâÀÔ´Ï´Ù.
¿Ïº®ÇÑ ÇÁ·Î±×·¥ÀÌ µÇ±â À§Çؼ­´Â ¸ÞÀϺ¸³»´Â Áß 
holding µÇ¾úÀ»¶§ ó¸®, ÆÄÀÏÀü¼Û½Ã »çÀÌÁî ³ª´©¾î¼­ º¸³»±â
µî ¿©·¯°¡Áö ¼¼½ÉÇÑ ºÎºÐÀ» °í·ÁÇØºÁ¾ß ÇÕ´Ï´Ù.
 
 


À§ÀÇ ¼Ò½º´Â 
±Ã±ÝÇÑÁ¡ÀÖÀ¸¸é Q&A¿¡ ±ÛÀ»³²°ÜÁÖ¼¼¿ä       

       
       

Jimmy´Â ´©±¸Àΰ¡bit10ÁÖ¼Ò·Ï - ¹æ¸í·Ï - Q&A - E-mail

½Åºñ·Î - °æÈñ´ëÀüÀÚ°ú88Çйø¸ðÀÓ¹æ - ¶óÀÌÄÚ½º - Æ®¶óÀÌÆ÷µå


Âü°í·Î Á¦È¨ÆäÀÌÁöÀÇ Çü½ÄÀº ¶óÀÌÄÚ½º ȨÆäÀÌÁöÀÇ ¼Ò½º¸¦ ÂüÁ¶ÇÏ¿´½À´Ï´Ù.