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(1)
 
¾Æ½Ã´Â ºÐÀº ¾Ë°ÚÁö¸¸ 2001³â¿¡ ¸ð¸ð »çÀÌÆ®¿¡ µ·(¹«·Á 900¿ø)¹Þ°í ¿Ã¶ó°£ ³»¿ëÀε¥ ¿äû¿¡ ÀÇÇÏ¿© ÀÚ¹ÙÆÐÅÏ»çÀÌÆ®¿¡ ´Ù½Ã °ø°³ÇÕ´Ï´Ù. ( 2003/04/24 ) 1279
Written by ienvyou - ÃÖÁö¿õ
4 of 4
 


¿ì¼± Áß¿äÇÑ Å¬·¡½ºµéºÎÅÍ »ìÆ캸µµ·Ï ÇÏÀÚ.

 »ó¼Óµµ

Major Java Mail API Component¸¦ º¸¸é interface¿Í ÇØ´ç Ŭ·¡½º¸¦ extends¹ÞÀº Ŭ·¡½ºµé·Î Á¤ÀǵȴÙ.

ÃÖ»óÀ§ interface Part°¡ Á¸ÀçÇϸç javax.mail.internet.MimePart interface°¡ ÀÌ Å¬·¡½º¸¦ extends¹Þ¾Æ »ç¿ëÇÑ´Ù.

 Part ÀÎÅÍÆäÀ̽º´Â Message¿Í BodyPart¿¡ ÀÇÇØ implementsµÇ¾îÁø´Ù.

 ¶ÇÇÑ MimePart´Â MimeMessage¿Í MimeBodyPart¿¡ ÀÇÇØ implementsµÇ¾îÁø´Ù.

 À§ÀÇ Å¬·¡½ºµéÀ» »ó¼Ó¹ÞÀº ¸¹Àº Ŭ·¡½ºµéÀÌ core API¸¦ ±¸¼ºÇÏ°í ÀÖÀ¸¸ç, service provider¿¡ ÀÇÇÑ

 È®ÀåÀÌ ¿ëÀÌÇØÁö´Â ÀåÁ¡À» °¡Áö°í ÀÖ´Ù. 

 ¸Þ½ÃÁö¸¦ º¸³¾¶§´Â MessageŬ·¡½º¿Í TransportŬ·¡½º¿¡ ÀÇÇÑ ±¸ÇöÀ» ÇÒ¼ö ÀÖÀ¸¸ç,

 ¸Þ½ÃÁö¸¦ ¹ÞÀ»¶§´Â StoreŬ·¡½º¿Í FolderŬ·¡½º¸¦ ÅëÇÏ¿© ÇØ´ç ½Ã½ºÅÛÀÇ ¸ÞÀϹڽº(º¸Åë INBOX)¸¦ ÅëÇØ

 MessageŸÀÔÀÇ µ¥ÀÌÅ͸¦ ¿ì¸® ´«À¸·Î º¼¼ö ÀÖ´Â °á°ú°¡ µÈ´Ù.



À§ÀÇ ÇüÅ·Π¸Þ½ÃÁö¸¦ Àü´ÞÇÒ¼ö °¡ ÀÖ´Ù.

¿©·¯ºÐµéÀÌ Áß¿äÇÏ°Ô ºÁ¾ß ÇÒ Å¬·¡½º´Â Message¿Í Transport, Store, Folder, Session Ŭ·¡½ºµéÀÌ´Ù.

¶ÇÇÑ Data HandlingÀ» À§Çؼ­´Â javax.activationÆÐÅ°ÁöÀÇ Å¬·¡½ºµéÀ» À¯½ÉÈ÷ ºÁ¾ßÇÒ °ÍÀÌ´Ù.

»ç½Ç»ó ¸ÞÀÏÀº À§ Ŭ·¡½ºÀÇ ¸Þ¼Òµå¸¦ À¯È¿ÀûÀýÇÏ°Ô¸¸ È°¿ëÀ» Çصµ Àü´Þ ¶Ç´Â ¸ÞÀϽýºÅÛÀ» ±¸ÃàÇÒ¼ö ÀÖ´Ù.

ÀÚ ÀÌÁ¦ SMTP¸¦ »ç¿ëÇÏ¿© °£´ÜÇÑ ¸ÞÀÏÀ» Àü¼ÛÇÏ¿© º¸µµ·Ï ÇÏÀÚ.

ÀϹÝÀûÀ¸·Î arguments°ªÀ» ¿ÜºÎ·ÎºÎÅÍ ÀÔ·Â¹Þ¾Æ »ç¿ëÇØ¾ß ÇÏÁö¸¸ ¹Ù»Û °ü°è·Î

StringÀÇ ¹è¿­¾È¿¡ ¸ðµÎ ÀÔ·ÂÇÑÈÄ Ã³¸®ÇÏ¿´´Ù.

»ç½Ç ´«¿©°Ü º¼ºÎºÐÀº ¸îÁÙ ¾ÈµÇ¸ç catchºí·ÏÀÌÇÏ´Â ExceptionÀ» ó¸®Çϱâ À§ÇÑ ºÎºÐÀÌ´Ù.

¼Ò½ºÄڵ带 º¸µµ·Ï ÇÏÀÚ.



import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class msgsendsample {
     static String msgText = "This is a message body.\nHere's the second line.";
     public static void main(String[] args) {
        // ½ºÆ®¸µÀÇ ¹è¿­°ªÀ¸·Î º¸³¾ property°ªµéÀ» ¼¼ÆÃÇÑ´Ù. 
        String arg [] ={"ienvyou@orgio.net","ienvyou@javapattern.info", "localhost", "true"};

        System.out.println();

        String to = arg[0]; // º¸³¾ »ç¶÷ÀÇ ÁÖ¼ÒÀÌ´Ù.
        String from = arg[1]; // º¸³»´Â »ç¶÷ÀÇ ÁÖ¼ÒÀÌ´Ù.
        String host = arg[2]; // SMTP¼­¹ö°¡ µ¹°í ÀÖ´Â °÷ÀÇ ÁÖ¼ÒÀÌ´Ù. 
        boolean debug = Boolean.valueOf(arg[3]).booleanValue(); //  
        
        Properties props = new Properties();
        props.put("mail.smtp.host", host);

        // ¸ÞÀÏÀ» º¸³¾¶§ µð¹ö±ëÄڵ带 È­¸é¿¡ º¸¿©ÁÙÁö¸¦ ¼¼ÆÃÇÑ´Ù. 
        if (debug) props.put("mail.debug", arg[3]); 
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug); // ÇØ´ç ¼¼¼ÇŬ·¡½ºÀÇ µð¹ö±ë ¿É¼ÇÀ» argument°ªÀ¸·Î ¼³Á¤ÇÑ´Ù.

        try {
            // create a message
            /**
            * MimeMessage´Â Message¸¦ extends¹Þ°í ÀÖÀ¸¸ç ¸ÞÀÏÀ» º¸³¾¶§ ÇÊ¿äÇÑ ÇÙ½É ¸Þ¼ÒµåµéÀ»
            * abstract method ¶Ç´Â
            * general methodÇüÅ·Π°¡Áö°í ÀÖ´Ù.
            // MimeMessage°´Ã¼¸¦ »ý¼ºÇÏ¿© javax.mail.MessageŸÀÔ¿¡ ³Ö´Â´Ù */
            Message msg = new MimeMessage(session); .
            msg.setFrom(new InternetAddress(from));
            // º¸³¾ »ç¶÷ÀÇ ÁÖ¼Ò¸¦ InternetAddressÇüÅÂÀÇ ¹è¿­·Î º¯È¯ÇÑ´Ù.
            //(¼ö½ÅÀÚ°¡ ¿©·¯¸íµÉ¼ö ÀÖÀ¸¹Ç·Î..); 
            InternetAddress[] address = {new InternetAddress(arg[0])};

            // ¼ö½ÅÀÎÀ» Message¿¡ ¼¼ÆÃÇÑ´Ù.
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("JavaMail APIs Test");
            msg.setSentDate(new Date());
            // If the desired charset is known, you can use
            // setText(text, charset)
            msg.setText(msgText);
            // ¾Æ·¡ÀÇ static¸Þ¼Òµå¿¡ ÀÇÇØ ¸ÞÀϹ߼Û
            Transport.send(msg);
        } catch (MessagingException mex) {
            System.out.println("\n--Exception handling in msgsendsample.java");
            mex.printStackTrace();
            System.out.println();
            Exception ex = mex;
            do {
                if (ex instanceof SendFailedException) {
                    SendFailedException sfex = (SendFailedException)ex;
                    Address[] invalid = sfex.getInvalidAddresses();
                    if (invalid != null) {
                    System.out.println(" ** Invalid Addresses");
                    if (invalid != null) {
                        for (int i = 0; i < invalid.length; i++) 
                        System.out.println(" " + invalid[i]);
                    }
                }
                Address[] validUnsent = sfex.getValidUnsentAddresses();
                if (validUnsent != null) {
                    System.out.println(" ** ValidUnsent Addresses");
                    if (validUnsent != null) {
                        for (int i = 0; i < validUnsent.length; i++) 
                        System.out.println(" "+validUnsent[i]);
                    }
                }
                Address[] validSent = sfex.getValidSentAddresses();
                if (validSent != null) {
                    System.out.println(" ** ValidSent Addresses");
                    if (validSent != null) {
                        for (int i = 0; i < validSent.length; i++) 
                        System.out.println(" "+validSent[i]);
                        }
                    }
                }
                System.out.println();
                if (ex instanceof MessagingException)
                ex = ((MessagingException)ex).getNextException();
                else
                ex = null;
            } while (ex != null);
        }
    }
    private static void usage() {
       System.out.println("usage: java msgsendsample <to> <from> <smtp> true|false");
    }
}

À§ÀÇ Äڵ忡¼­ Àß »ìÆ캸¾Æ¾ß ÇҺκÐÀº MimeMessage¸¦ »ý¼ºÇÑÈÄ Á¤º¸¸¦ ¼¼ÆÃÇÏ´Â ÀϵéÀÌ´Ù. setXXX¸Þ¼Òµé¿¡ ÀÇÇØ ¼¼ÆõǾî Áö¸ç, TransportŬ·¡½ºÀÇ ¸Þ¼Òµé¿¡ ÀÇÇØ ½ÇÁúÀûÀÎ network infrastructure¸¦ ÅëÇØ ¸ÞÀÏÀÌ ¹ß¼ÛµÇ¾î Áö°Ô µÈ´Ù. À¯½ÉÈ÷ º» »ç¶÷Àº PropertiesŬ·¡½º°¡ Á¸ÀçÇϴµ¥, ¿Ö ÀÖÀ»±î? ¶ó´Â ±Ã±ÝÁõÀÌ »ý±æ°ÍÀÌ´Ù.. ¿ø·¡ º¸´Â »ç¶÷µéÀÌ ÈÄÆíÀ» ±â´Ù¸®°Ô ¸¸µé¾î¾ß ¸¸µå´Â »ç¶÷µµ Àç¹ÌÀÖÁö ¾ÊÀ»±î? ´ÙÀ½ ½Ã°£¿¡´Â PropertiesÀÇ ¿ªÇÒ°ú SMTP°¡ local¿¡ ¾øÀ»°æ¿ì(orgio³ª chollian°°Àº °÷ÀÇ smtp)¸¦ ÀÎÁõÀ» ÅëÇÏ¿© »ç¿ëÇÏ°í ½ÍÀ»¶§ÀÇ JavaMail¿¡ ´ëÇÏ¿© À̾߱âÇÒ °ÍÀÌ´Ù..
 
1 2 3 4
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD