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(4)
 
÷ºÎÆÄÀÏÀÇ ¸ÞÀϺ¸³»±â ¹× POP3¼­¹öÀÇ Æú´õ³»¿ëÀ» ÀÌ¿ëÇÏ¿© µ¥ÀÌÅ͸¦ °¡Á®¿Àµµ·Ï ÇÏ´Â »ùÇÃÀ» ¼³¸í°ú ÇÔ²² º¸µµ·Ï ÇÏÀÚ. ( 2003/05/13 ) 684
Written by ienvyou - ÃÖÁö¿õ
2 of 4
 


¢ºFile Attatchment Mail Source Code


Source Code : 



import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.event.*;

public class sendfile  implements ConnectionListener, TransportListener{

    public static void main(String[] args) {
            if (args.length != 5) {
                System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false");
                System.exit(1);
            }

            String to = args[0];
            String from = args[1];
            String host = args[2];
            String filename = args[3];
            String bcc = "ienvyou@javapattern.info";
            boolean debug = Boolean.valueOf(args[4]).booleanValue();
            String msgText1 = "º¸³»Á³½À´Ï±î?\n";
            String subject = "ÆÄÀϺ¸³»±â ¿¹Á¦";
            
            // create some properties and get the default Session
            Properties props = System.getProperties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth" , "true"); //¾ÆÁÖ Áß¿ä ¾øÀ¸¸é ÀÎÁõÀ» ½ÃµµÇÒ¼ö°¡ ¾ø´Ù.
            
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(debug);
            
            sendfile sf = new sendfile();
            sf.go(session, to, bcc, from, subject, msgText1, filename );
    }

    public void go(Session session, 
                        String to, 
                        String bccc, 
                        String from, 
                        String subject, 
                        String msgText1, 
                        String filename){
            Transport trans = null;
            try {
                // create a message
                MimeMessage msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress(from));
                InternetAddress[] address = {new InternetAddress(to)};
                InternetAddress [] cc = {new InternetAddress("ienvyou@javapattern.info")};
                InternetAddress [] bcc = {new InternetAddress("ienvyou@javapattern.info")};
                msg.setRecipients(Message.RecipientType.TO, address);
                msg.setRecipients(Message.RecipientType.CC, cc);
                msg.setRecipients(Message.RecipientType.BCC, bcc);
                msg.setSubject(subject);

                // create and fill the first message part
                MimeBodyPart mbp1 = new MimeBodyPart();
                mbp1.setText(msgText1);

                // create the second message part
                MimeBodyPart mbp2 = new MimeBodyPart();

                    // attach the file to the message
                FileDataSource fds = new FileDataSource(filename);

                mbp2.setDataHandler(new DataHandler(fds));
            // Çì´õ¿¡ ÆÄÀÏÀ̸§À» ¼¼ÆÃÇÒ¶§ base64ÇüÅ·Π¹Ýµå½Ã.
                mbp2.setFileName(MimeUtility.encodeText(fds.getName(), "euc-kr","B"));  

                // create the Multipart and its parts to it
                Multipart mp = new MimeMultipart();
                mp.addBodyPart(mbp1);
                mp.addBodyPart(mbp2);

                // add the Multipart to the message
                msg.setContent(mp);

                // set the Date: header
                msg.setSentDate(new Date());
                
        /************** Carouser Adding *************************************/
                msg.saveChanges(); // msg°´Ã¼¿¡¼­ µ¥ÀÌÅ͸¦ º¸³¾¼ö ÀÖµµ·Ï ¼Ó¼ºº¯°æ

                // ÀÎÁõÀ» ½ÃµµÇϵµ·Ï ÇÑ´Ù.
                trans = session.getTransport(address[0]);

                // register ourselves as listener for ConnectionEvents 
                // and TransportEvents
                trans.addConnectionListener(this);
                trans.addTransportListener(this);


                 // connect the transport
                trans.connect("openitech.co.kr","ienvyou","0000");
               

                // send the message
                //Transport.send(msg);
                trans.sendMessage(msg, address);
                
            } catch (Exception mex) {
                mex.printStackTrace();
                
            } finally {
                try{
                    trans.close();
                }catch(MessagingException e){}
            }
    }

     // implement ConnectionListener interface
    public void opened(ConnectionEvent e) {
        System.out.println(">>> ConnectionListener.opened()");
    }
    public void disconnected(ConnectionEvent e) {}
    public void closed(ConnectionEvent e) {
        System.out.println(">>> ConnectionListener.closed()");
    }

    // implement TransportListener interface
    public void messageDelivered(TransportEvent e) {
        System.out.print(">>> TransportListener.messageDelivered().");
        System.out.println(" Valid Addresses:");
        Address[] valid = e.getValidSentAddresses();
        if (valid != null) {
            for (int i = 0; i < valid.length; i++) 
            System.out.println("    " + valid[i]);
        }
    }
    public void messageNotDelivered(TransportEvent e) {
        System.out.print(">>> TransportListener.messageNotDelivered().");
        System.out.println(" Invalid Addresses:");
        Address[] invalid = e.getInvalidAddresses();
        if (invalid != null) {
            for (int i = 0; i < invalid.length; i++) 
            System.out.println("    " + invalid[i]);
        }
    }
    public void messagePartiallyDelivered(TransportEvent e) {
    // SMTPTransport doesn't partially deliver msgs
    }
}

mbp2.setFileName(MimeUtility.encodeText(fds.getName(), "euc-kr","B")); // Çì´õ¿¡ ÆÄÀÏÀ̸§À» ¼¼ÆÃÇÒ¶§ base64ÇüÅ·Π¹Ýµå½Ã ÆÄÀÏÀ̸§À» º¯È¯½ÃÄÑÁØ´Ù.
 
1 2 3 4
References
 
¾ÆƼŬ¼Ò½º : javaMail2.zip
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD