Session/Message/Address Class
* Session Class
¼¼¼ÇŬ·¡½º´Â ÀÚ¹Ù¸ÞÀÏ ±âº»¿¡ Á¤ÀÇµÈ Å¬·¡½ºÀÌ´Ù. ÀÌ ¼¼¼ÇÀ» ÅëÇؼ ¸¹Àº ÀÏÀ» ÇÒ¼ö Àִµ¥,
ÇØ´ç °´Ã¼´Â ¸ÞÀϼ¹ö, »ç¿ëÀÚ Á¤º¸, Æнº¿öµå µîÀÇ Á¤º¸¸¦ java.util.PropertiesŬ·¡½º¿¡ ÀÇÇØ
¾ò¾î¿Ã ¼ö°¡ ÀÖ´Ù.
PropertiesŬ·¡½ºÀÇ ³»¿ë°ú »ç¿ë¹ýÀº Àڹٱ⺻ÀÇ Property»ç¿ë¹ý¿¡ ´ëÇÏ¿© ÂüÁ¶Ç϶ó.
Ŭ·¡½ºÀÇ »ý¼ºÀÚ´Â privateÇüÅ·ΠµÇ¾îÀÖÀ¸¸ç(Áï singlton patternÀ̶õ À̾߱â°ÚÁÒ?) ÇϳªÀÇ ±âº»¼¼¼ÇÀ»
getDefaultInstance()¸Þ¼Òµå¿¡ ÀÇÇÏ¿© °øÀ¯ÇÒ¼ö ÀְԵȴÙ.
¼¼¼ÇÀ» ¾ò¾î³»´Â ¿¹¸¦ µé¾îº¸¸é
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
ó·³ »ç¿ëÇÒ¼öµµ ÀÖÀ¸¸ç, ÇϳªÀÇ À¯ÀÏÇÑ ¼¼¼ÇÀ» »ý¼ºÇÑ´Ù¸é getInstance()¸Þ¼Òµå¸¦
»ç¿ëÇÒ¼öµµ ÀÖÀ»°ÍÀÌ´Ù.
Properties props = new Properties();
// fill props with any information
Session session = Session.getInstance(props, null);
getInstance(prop, null)¿¡¼ µÎ¹ø° parameterÀÎ nullÀº javax.mail.Authenticator Ŭ·¡½º¸¦ ÁöĪÇϴµ¥
null°ªÀ» »ç¿ëÇÑ´Ù¸é ÀÎÁõÀ» »ç¿ëÇÏÁö ¾Ê°Ú´Ù´Â À̾߱â·Î º¸¸é µÈ´Ù.
Authenticator´Â ³ªÁß¿¡ ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ.
*Message Class
¼¼¼ÇÀÌ »ý¼ºµÇ¾ú´Ù¸é ÀÌÁ¦ ¸ÞÀÏÀ» º¸³»±â À§ÇØ ¸Þ½ÃÁö¸¦ »ý¼ºÇÒ ½ÃÁ¡ÀÌ µÈ°ÍÀÌ´Ù.
MessageŬ·¡½ºÀÇ ÇüŸ¦ »ìÆ캸¸é, ÀüÆí¿¡¼µµ À̾߱â ÇßµíÀÌ abstact class·Î µÇ¾î ÀÖÀ¸¸ç,
´ëºÎºÐÀÇ °æ¿ì javax.mail.internet.MimeMessage¸¦ subclassingÇÏ¿© »ç¿ëÇÑ´Ù.
MimeMessage´Â ¼·Î ´Ù¸¥ RFCÇÁ·ÎÅäÄÝ »ó¿¡¼ Á¤ÀǵǴ MIMEŸÀÔ°ú header¸¦ ÀνÄÇÒ¼ö ÀÖ´Â
email message¸¦ °¡¸£Å²´Ù.
¸Þ½ÃÁöÀÇ Çì´õÁ¤º¸´Â ¹Ýµå½Ã US-ASCIIÄÚµå·Î Á¦ÇѵǾî ÀÖÀ¸¹Ç·Î non-ASCII(ÇѱÛÇ¥ÇöÀÇ °æ¿ì)ÀÇ
°æ¿ì¿¡´Â Çì´õÇʵ忡 Æ÷ÇԵǾî ÀÖ´Â ³»¿ëÀ» encodeÇÏ¿©¾ß ¸Þ½ÃÁö¸¦ Á¦´ë·Î º¼¼ö ÀÖ´Ù.
Message¸¦ »ý¼ºÇÒ¶§´Â MimeMessageÀÇ »ý¼ºÀÚ¿¡ session°´Ã¼°¡ argument·Î µé¾î°¡°Ô µÈ´Ù.
MimeMessage message = new MimeMessage(session);
À§ÀÇ °Í ÀÌ¿ÜÀÇ ´Ù¸¥ »ý¼ºÀÚ¿¡´Â RFC-822 ÇüÅ·ΠµÈ input streamÀ¸·ÎºÎÅÍ
MimeMessage¸¦ »ý¼ºÇÒ ¼öµµ ÀÖ´Ù.
ÀÌÁ¦ ¼ø¼¿¡ µû¶ó ¸Þ½ÃÁö¸¦ »ý¼ºÇÏ°Ô µÇ¸é, ÇØ´ç °´Ã¼¸¦ ÀÌ¿ëÇÏ¿© ¸Þ½ÃÁö °¢°¢ÀÇ ºÎºÐÀ» ¼¼ÆÃÇÒ¼ö
ÀÖ´Â Áغñ°¡ ´Ù µÈ°ÍÀÌ´Ù. Çß´ø ¾ê±â °è¼Ó ¶Ç ³ª¿À´Â °ÍÀÌÁö¸¸ Message´Â Part¸¦ ±¸ÇöÇÑ Å¬·¡½ºÀÌ°í,
MimeMessage´Â MimePart¸¦ ±¸ÇöÇÑ Å¬·¡½º¶ó°í ¼³¸íÇß´Ù.
±âº»ÀûÀÎ ¹æ¹ýÀº content¸¦ À§ÇÑ argument¿Í mime typeÀ» setContent()¸Þ¼Òµå¸¦ ¾Æ·¡Ã³·³ »ç¿ëÇÏ¿©
¼¼ÆýÃÅ°´Â ÀÏÀÌ´Ù.
message.setContent("Hello", "text/plain");
À§ÀÇ ÇüÅÂó·³ content¸¦ Á¶ÇÕÇÒ¼öµµ ÀÖ°ÚÁö¸¸ setText()¸Þ¼Òµå¸¦ »ç¿ëÇصµ ¹«¹æÇÏ´Ù.
¿Ö³ÄÇϸé Message¿¡¼ ±âº» MIMIE typeÀº text/plainÀ» µû¸£°í Àֱ⠶§¹®ÀÌ´Ù.
»ç¿ë¹ýÀ» º¸ÀÚ.
message.setText("Hello"); // equals ->message.setContent("Hello", "text/plain");
content¸¦ ¼¼ÆýÃŲÈÄ¿¡´Â Á¦¸ñÀ» ³Ö¾î¾ß ÇÒ°ÍÀε¥ ±×°ÍÀº ¾Æ·¡ÀÇ ¸Þ¼Òµå¸¦ »ç¿ëÇÏ¿© ¸¸µé¼ö ÀÖ´Ù.
message.setSubject("First");
!!!!!!!!!!!!!!!!!!ÀÚ ÀÌÁ¦ Á¦¸ñ±îÁö ³Ö¾úÀ¸¸é ´ÙÀ½¿¡ ¹«¾ùÀ» ÇØ¾ß ÇÒ±î??
´©±¸¿¡°Ô ¸ÞÀÏÀ» º¸³¾°ÍÀΰ¡~~!
¸ÞÀÏ ¹Þ´Â »ç¶÷À» ¼¼ÆýÃÄÑ¾ß ÇÒ°ÍÀÌ´Ù.
*Address Class
À§ÀÇ ¼ø¼¿¡ µû¶ó ¼¼ÆÃÀÌ µÇ¾î ÀÖÀ»°ÍÀÌ´Ù.
SessionÀÇ »ý¼º --> MessageÀÇ »ý¼º
MessageŬ·¡½ºÃ³·³ AddressŬ·¡½ºµµ abstractŬ·¡½ºÀÌ´Ù. ¿©·¯ºÐÀÌ ¸ÞÀÏÀ» º¸³»±â À§Çؼ´Â
javax.mail.internet.InternetAddress class¸¦ »ç¿ëÇØ¾ß ÇÑ´Ù.
ÁÖ¼Ò¸¦ »ý¼ºÇϱâ À§ÇØ ÇØ´ç InternetAddressŬ·¡½º¿¡ email address¸¸ argument·Î ¼¼ÆÃÇصµ µÈ´Ù.
Address address = new InternetAddress("president@chungwadae.go.kr");
¸¸¾à emailÁÖ¼Ò¿Í ¹Þ´Â »ç¶÷ÀÇ À̸§±îÁö ¸í½ÃÇÏ°í ½Í´Ù¸é ´ÙÀ½°ú °°ÀÌ »ý¼ºÇÏ¸é µÉ °ÍÀÌ´Ù.
Address address = new InternetAddress("president@chungwadae.go.kr", "³ë¹«Çö");
À§ °æ¿ìó·³ ¹ÞÀ» »ç¶÷ÀÇ ÁÖ¼Ò°ªÀÌ »ý¼ºÀÌ µÇ¾ú´Ù¸é ±× ³»¿ëÀ» µÎ °¡Áö ¹æ¹ýÀ» ÅëÇÏ¿©
Àü´Þ½Ãų¼ö°¡ Àִµ¥, ±× ¸Þ¼Òµå´Â setFrom() °ú setReplyTo() methodÀÌ´Ù.
message.setFrom(address)
¸¸¾à¿¡ ¿©·¯»ç¶÷¿¡ ¸Þ½ÃÁö¸¦ º¸³»°í ½Í´Ù¸é ÁÖ¼ÒÀÇ ¹è¿°´Ã¼¸¦ ¸¸µé¾î addFrom() ¸Þ¼Òµå¸¦ ÀÌ¿ëÇÏ¿©
message°´Ã¼¿¡ °ªÀ» ¼¼ÆýÃų ¼öµµ ÀÖ´Ù.
Address address[] = {"ienvyou@orgio.net", "febkang@orgio.net","specular@netsgo.com"};
message.addFrom(address);
ÀÌ·¸°Ô ¸Þ½ÃÁö¿¡ ´ëÇÑ ¼öÃëÀÎÀÌ Á¤ÇØÁ³´Ù¸é addRecipient() method¸¦ »ç¿ëÇÒ¼ö Àִµ¥ ÀÌ ¸Þ¼Òµå´Â
ÁÖ¼Ò°ª¿·¿¡¼ Message.RecipientType À» ¸í½ÃÇÏ¿©¾ß ÇÑ´Ù.
message.addRecipient(type, address)
ÇØ´ç ¼öÃëÀÎÀÇ Type¿¡´Â ¼¼°¡Áö°¡ ÀÖÀ¸¸ç ¾Æ·¡¿Í °°´Ù.
Message.RecipientType.TO
Message.RecipientType.CC
Message.RecipientType.BCC
Address toAddress = new InternetAddress("president@chungwadae.go.kr");
Address ccAddress = new InternetAddress("first.lady@chungwadae.go.kr");
message.addRecipient(Message.RecipientType.TO, toAddress);
message.addRecipient(Message.RecipientType.CC, ccAddress);
ÀÚ¹Ù¸ÞÀÏÀº email ÁÖ¼Ò°¡ À¯È¿ÇÑÁö¸¦ üũÇÏ´Â ¸ÞÄ¿´ÏÁòÀÌ ¾ø´Ù. ¸¸¾à üũ¸¦ ÇÏ°í ½Í´Ù¸é
ÇÁ·Î±×·¥À» Â¥´Â ¿©·¯ºÐÀÌ ¸Þ¼Òµå¸¦ ¸¸µé¾î¼ ¹®¹ý¿¡ ¸Â´Â email ÁÖ¼ÒÀÎÁö¸¦ üũÇس»¾ß ÇÒ°ÍÀÌ´Ù.
|