Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


Java WebMail System Implementation(3)
 
¾Æ½Ã´Â ºÐÀº ¾Ë°ÚÁö¸¸ 2001³â¿¡ ¸ð¸ð »çÀÌÆ®¿¡ µ·(¹«·Á 900¿ø)¹Þ°í ¿Ã¶ó°£ ³»¿ëÀε¥ ¿äû¿¡ ÀÇÇÏ¿© ÀÚ¹ÙÆÐÅÏ»çÀÌÆ®¿¡ ´Ù½Ã °ø°³ÇÕ´Ï´Ù. ( 2003/04/29 ) 482
Written by ienvyou - ÃÖÁö¿õ
2 of 3
 


Delete/Reply Message

* Deleting Messages and Flags


¸Þ½ÃÁö¸¦ »èÁ¦ÇÏ´Â ÀÛ¾÷Àº ¸Þ½ÃÁöµé°ú ¿¬°üµÈ Ç÷¡±×°ªÀ» Á¶Á¤ÇÔÀ¸·Î¼­ ÀÌ·ç¾îÁø´Ù.

¸Þ½ÃÁöµéÀ» À§ÇØ ¼­·Î´Ù¸¥ »óŸ¦ À§ÇÑ °¢°¢ÀÇ Ç÷¡±×µéÀÌ Á¸ÀçÇϴµ¥, ¿©±â¿¡´Â ¸î°¡Áö 

½Ã½ºÅÛÁ¤ÀÇ »óÅÂ¿Í »ç¿ëÀÚÁ¤ÀÇ »óÅ·Π³ª´­¼ö ÀÖ´Ù.  

¹Ì¸® Á¤ÀÇµÈ Ç÷¡±×´Â inner classÀÎ Flags.FlagŬ·¡½º¾È¿¡ Á¤ÀǵǾî ÀÖ´Ù.

Ç÷¡±×µéÀÇ ¸®½ºÆ®´Â ¾Æ·¡¿Í °°´Ù.

Flags.Flag.ANSWERED 
Flags.Flag.DELETED 
Flags.Flag.DRAFT 
Flags.Flag.FLAGGED 
Flags.Flag.RECENT 
Flags.Flag.SEEN 
Flags.Flag.USER 

À§ÀÇ Ç÷¡±×°ªµéÀº ¸ðµç ¸ÞÀϼ­¹ö¶Ç´Â provider°¡ Á¦°øÇØÁÖÁö´Â ¾Ê´Â´Ù. 

¿¹¸¦ µé¸é ¸Þ½ÃÁö¸¦ »èÁ¦ÇÏ´Â Ãø¸é¿¡¼­ POPÇÁ·ÎÅäÄÝÀº ±×·¯ÇÑ Ç÷¡±×°ªµéÀ» 

Á¦°øÇØÁÖÁö ¾Ê±â¶§¹®ÀÌ´Ù. 

»õ·Î¿î ¸ÞÀÏÀ» üũÇÏ´Â °ÍÀº POP task°¡ ¾Æ´Ï¶ó ÇϳªÀÇ ¸ÞÀÏŬ¶óÀ̾ðÆ®À̱⠶§¹®ÀÌ´Ù.

¸¸¾à Ç÷¡±×°ªÀ» Áö¿øÇÏ´Â Áö ¾Ë¾Æº¸·Á¸é folderŬ·¡½ºÀÇgetPermanentFlags()¸Þ¼Òµå¸¦ 

»ç¿ëÇÏ¿© °ªÀ» ¾ò¾îº¸¸é µÈ´Ù.

ÀÌÁ¦ ½ÇÁúÀûÀÎ ¸Þ½ÃÁöµéÀ» »èÁ¦Çϱâ À§Çؼ­´Â DELETEDÇ÷¡±×°¡ ¾Æ·¡Ã³·³ ¼¼ÆõǾîÁ® ÀÖ¾î¾ß ÇÑ´Ù.

message.setFlag(Flags.Flag.DELETED, true);

À§ÀÇ °æ¿ìó·³ Çϱâ À§Çؼ­´Â ´ç¿¬È÷ folder°¡ READ_WRITEÇüÅ·Π¿ÀǵǾî ÀÖ¾î¾ß ÇÔÀº ¹°·ÐÀÌ´Ù.

folder.open(Folder.READ_WRITE);

¸ðµç ¸Þ½ÃÁö¿¡ ´ëÇÑ Ã³¸®°¡ ³¡³µ´Ù¸é ´Ù½Ã Æú´õ¸¦ ´Ý¾ÆÁÖ´Â ÀÏÀ» ó¸®ÇØ¾ß ÇÒ°ÍÀÌ´Ù.

folder.close(true);

FolderÀÇ expunge()¸Þ¼Òµå¸¦ ¸Þ½ÃÁö¸¦ »èÁ¦Çϱâ À§ÇÏ¿© À¯¿ëÇÏ°Ô »ç¿ëµÈ´Ù.

(SunÀÇ POP3 provider´Â Á¦°øÇÏÁö ¾Ê´Â´Ù.)

Ç÷¡±×°ªÀ» ÇØÁ¦½Ãų¶§¿¡´Â setFlag()¸Þ¼Òµå¿¡ false°ª¸¸ ¼¼ÆýÃÅ°¸é µÈ´Ù.

ÇöÀç Ç÷¡±×ÀÇ ¼¼ÆðªÀ» ¾Ë¾Æº¸·Á¸é isSet()¸Þ¼Òµå¸¦ »ç¿ëÇÏ¿© ¾Ë¾Æº¸¸é µÈ´Ù.

* Reply Message

MessageŬ·¡½º´Â reply()¶ó´Â ¸Þ¼Òµå¸¦ Æ÷ÇÔÇÏ°í Àִµ¥, »õ·Î¿î ¸Þ½ÃÁö¸¦ ¼ö½ÅÀÚ, 

Á¦¸ñ¿¡ "Re:"¸¦ Æ÷ÇÔÇÏ°Ô ÇÑ´Ù. ¿©±â¿¡´Â ¸Þ½ÃÁö¸¦ À§ÇØ ¾î¶°ÇÑ contentµµ Ãß°¡µÇÁö ¾ÊÀ¸¸ç, 

´ÜÁö »õ·Î¿î ¼ö½ÅÀÚ¸¦ À§ÇÑ reply-toÇì´õ ¶Ç´Â from(¹ß½ÅÀÎ)ÀÇ Á¤º¸¸¸ º¹»çÇÏ°Ô µÈ´Ù.
 
 ÀÌ ¸Þ¼Òµå´Â true¶Ç´Â false°ªÀ» °¡Áú¼ö ÀÖ´Â ¸ðµç »ç¶÷¿¡°Ô replyÇÒ°æ¿ì¿¡´Â true°ªÀ» ¼¼ÆÃÇϸç, 

´ÜÁö º¸³½ ÇÑ »ç¶÷¿¡°Ô replyÇÒ°æ¿ì¿¡´Â false°ªÀ» ¼¼ÆÃÇÏ¸é µÈ´Ù.

 ¾Æ·¡ÀÇ º¸±âÀÌ´Ù.

MimeMessage reply = (MimeMessage)message.reply(false);
reply.setFrom(new InternetAddress("president@chungwadae.go.kr"));
reply.setText("Thanks");
Transport.send(reply);

¸Þ½ÃÁö¸¦ º¸³¾¶§  reply-toÁÖ¼Ò¸¦ ¼³Á¤Çϴ°ÍÀº setReplyTo()¸Þ¼Òµå¸¦ »ç¿ëÇÏ¸é µÈ´Ù.

¾Æ·¡´Â Çٽɰñ°ÝÄÚµåÀÌ´Ù.. ÁÖ¼®À¸·Î ¸·¾Æ³í ºÎºÐÀÌ Reply½Ãų¶§ »ç¿ëÇÏ´Â ºÎºÐÀ̸ç, 

ȸ½ÅÇϱâ À§Çؼ­´Â Æú´õÀÇ ¸Þ½ÃÁöµéÀ» °¡Á®¿Í¾ß Çϴ°ÍÀº ´ç¿¬ÇÏ´Ù.


import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class ReplyExample {
  public static void main (String args[]) throws Exception {
    String host = args[0];
    String sendHost = args[1];
    String username = args[2];
    String password = args[3];
    String from = args[4];

    // Create empty properties
    Properties props = System.getProperties();
    props.put("mail.smtp.host", sendHost);

    // Get session
    Session session = Session.getDefaultInstance(props, null);

    // Get the store
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    BufferedReader reader = new BufferedReader (
      new InputStreamReader(System.in));

    // Get directory
    Message message[] = folder.getMessages();
    for (int i=0, n=message.length; i < n; i++) {
       System.out.println(i + ": " + message[i].getFrom()[0] 
         + "\t" + message[i].getSubject());

      System.out.println("Do you want to reply to the message? [YES to reply/QUIT to end]");
      String line = reader.readLine();
      if ("YES".equals(line)) {
        // Create a reply message

        // Set the from field

        // Create the reply content, copying over the original if text

        // Set the content

        // Send the message
      } else if ("QUIT".equals(line)) {
        break;
      }
    }

    // Close connection 
    folder.close(false);
    store.close();
  }
}
¾Æ·¡´Â storeÀÎÁõ ¹× sendÀÎÁõÀ» ¸ðµÎ °áÇÕÇÑ ÄÚµåÀ̸ç ÇöÀç °¡Áö°í ÀÖ´Â ³î»õ°èÁ¤À¸·Î Å×½ºÆ®Çغ¼¼ö ÀÖÀ¸¸ç, º¹»ç¸¸ ÇÏ¿©µµ µ¹¾Æ°¡°Ô²û ¸¸µé¾ú´Ù. ¸ðµÎ Å×½ºÆ®Çغ¸±â ¹Ù¶õ´Ù.


import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class ReplyExample {
  public static void main (String args[]) throws Exception {
    String host = "openitech.co.kr";
    String sendHost = "openitech.co.kr";
    String username = "ienvyou";
    String password = "0000";
    String from = "ienvyou@orgio.net";

    // Create empty properties
    Properties props = System.getProperties();
    props.put("mail.smtp.host", sendHost);
	props.put("mail.smtp.auth","true");

	Authenticator auth = new MyAuthentication();
    // Get session
    Session session = Session.getDefaultInstance(props, auth);
	session.setDebug(true);


    // Get the store
    Store store = session.getStore("pop3");
    store.connect(host, username, password);

    // Get folder
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);

    BufferedReader reader = new BufferedReader (
      new InputStreamReader(System.in));

    // Get directory
    Message message[] = folder.getMessages();
    for (int i=0, n=message.length; i < n; i++) {
       System.out.println(i + ": " + message[i].getFrom()[0] 
         + "\t" + message[i].getSubject());

      System.out.println("Do you want to reply to the message? [YES to reply/QUIT to end]");
      String line = reader.readLine();
      if ("YES".equals(line)) {

        // Create a reply message
        MimeMessage reply = (MimeMessage)message[i].reply(false);

        // Set the from field
        reply.setFrom(new InternetAddress(from));

        // Create the reply content, copying over the original if text
        MimeMessage orig = (MimeMessage)message[i];
        StringBuffer buffer = new StringBuffer("Thanks\n\n");
        if (orig.isMimeType("text/plain")) {
          String content = (String)orig.getContent();
          StringReader contentReader = new StringReader(content);
          BufferedReader br = new BufferedReader(contentReader);
          String contentLine;
          while ((contentLine = br.readLine()) != null) {
            buffer.append("> ");
            buffer.append(contentLine);
            buffer.append("\r\n");
          }
        }

        // Set the content
        reply.setText(buffer.toString());

        // Send the message
        Transport.send(reply);

      } else if ("QUIT".equals(line)) {
        break;
      }
    }

    // Close connection 
    folder.close(false);
    store.close();
  }
}


class MyAuthentication extends Authenticator {
	PasswordAuthentication pa;
	public MyAuthentication(){
		pa = new PasswordAuthentication("ienvyou","0000");
	}
	public PasswordAuthentication getPasswordAuthentication() {
		return pa;
	}
}
´ÙÀ½Æí¿¡´Â ¸Þ½ÃÁöÆ÷¿öµù¿¡ ´ëÇØ ¾Ë¾Æº¸°Ú´Ù.
 
1 2 3
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD