Session/Store Class
¿ø·¡ ¼ø¼»óÀ¸·Î File AttachmentºÎºÐºÎÅÍ ÇØ¾ß ÇÏÁö¸¸ »ç½Ç ±×ÂÊÀº javax.activationÆÐÅ°Áö¿¡
°ü·ÃµÈ ³»¿ëÀ̶ó ±×ºÎºÐµé¿¡ ´ëÇؼ´Â ÃßÈÄ¿¡ ´Ù·ç°Ú´Ù.
* Store class & Folder class
¸Þ½ÃÁö¸¦ ¹Þ¾Æ¿À´Â °ÍÀº ¼¼¼ÇÀ» »ç¿ëÇÏ¿© ¸Þ½ÃÁö¸¦ º¸³»´Â °Í°ú °ÅÀÇ À¯»çÇÏ´Ù°í º¸¸é µÈ´Ù.
¾îÂîµÆµç ¼¼¼ÇÀ» ¾ò¾î¿ÂÈÄ¿¡, Store°´Ã¼·Î ¼¹ö¿¡ Á¢¼ÓÇÑÈÄ
AuthenticatorŬ·¡½º¸¦ ÅëÇØ Á¢¼ÓÇÏ¸é µÇ±â ¶§¹®ÀÌ´Ù.
Transport Ŭ·¡½ºÃ³·³ StoreŬ·¡½º¸¦ ÅëÇؼ ¼¹ö¿¡ Á¢¼ÓÇÒ¼ö ÀÖ´Â ÄÚµùÀ» ¾Æ·¡¿Í °°ÀÌ ÇÒ¼ö ÀÖ´Ù.
IMAP or POP3¸¦ ÀÌ¿ëÇÏ´Â °æ¿ì¸¦ ¼¼¼ÇÀ» ÅëÇؼ ¸ðµÎ º¸¿©ÁØ´Ù.
// Store store = session.getStore("imap");
Store store = session.getStore("pop3");
store.connect(host, username, password);
StoreŬ·¡½º¸¦ ÅëÇØ ¼¹ö·Î Á¢¼ÓÈÄ FolderŬ·¡½º¸¦
ÅëÇØ ¸Þ½ÃÁö¸¦ ¾ò¾î¿Ã¼ö Àִµ¥, ¿©±â¼ ÁÖÀÇÇÒÁ¡Àº ¸Þ½ÃÁö¸¦ ¾ò¾î³»±â Àü¿¡
¹Ýµå½Ã Æú´õ Ŭ·¡½º°¡ ¿·ÁÀÖ¾î¾ß ÇÑ´Ù.
¾Æ·¡´Â ÇØ´ç ¼¹öÀÇ ¸Þ½ÃÁö¸¦ ¾ò¾î³»´Â ¹æ¹ýÀ» ÄÚµùÀ¸·Î¼ º¸¿©ÁØ´Ù.
Folder folder = store.getFolder("INBOX"); // POP3ÀÇ °æ¿ì INBOXÆú´õ ¸¸À» °¡Áö°í ÀÖ´Ù.
folder.open(Folder.READ_ONLY); // ÀбâÀü¿ëÀ¸·Î Æú´õ¸¦ ¿¬´Ù.
Message message[] = folder.getMessages(); // Æú´õ¾ÈÀÇ ¸Þ½ÃÁöµéÀ» ¾ò¾î¿Â´Ù.
POP3¸¦ À§ÇØ »ç¿ëÇÏ´Â °ÍÀº ´ÜÁö INBOXÆúÅÍ »ÓÀÌ´Ù.
¸¸¾à IMAPÀ» »ç¿ëÇÑ´Ù¸é ¿©·¯°³ÀÇ Æú´õ¸¦ ¾ò¾î¿Ã¼ö°¡ ÀÖ´Ù.
¸Þ½ÃÁö¸¦ Àбâ À§Çؼ´Â getContent()¸Þ¼Òµå¸¦ »ç¿ëÇϰųª writeTo()¸Þ¼Òµå¸¦ ÀÌ¿ëÇÏ¿©
½ºÆ®¸²¿¡ content¸¦ ¾µ¼öµµ ÀÖ´Ù.
getContent()¸Þ¼Òµå´Â ´Ü¼øÈ÷ ¸Þ½ÃÁöÀÇ ³»¿ë¸¸À» ²¨³»¿À´Â ¿ªÇÒÀ» ÇÏÁö¸¸,
writeTo()¸Þ¼Òµå´Â header¸¦ Æ÷ÇÔ ³»¿ëÀ» ¾ò¾î³»´Â ¿ªÇÒÀ» ÇÑ´Ù.
System.out.println(((MimeMessage)message).getContent());
ÇÁ·Î±×·¡¹Ö½Ã ¸ÞÀÏÀ» ¸ðµÎ Àоú´Ù¸é, folder¿Í store¸¦ ¸ðµÎ ´Ý¾ÆÁÖ´Â ³»¿ëÀ» ¾Æ·¡¿Í °°ÀÌ ÀÛ¼ºÇÏ¿© ÇÑ´Ù.
folder.close(aBoolean);
store.close();
±âº»ÀûÀ¸·Î Áö±Ý±îÁö ¼³¸íÇÑ 7°³ÀÇ Å¬·¡½º¸¦ ÀÌÇØÇÏ´Â °ÍÀÌ JavaMailAPIÀÇ °ÅÀÇ
ÀüºÎ¶ó Çصµ °ú¾ðÀÌ ¾Æ´Ï´Ù. 7°³ ÀÌ¿ÜÀÇ Å¬·¡½ºµéÀº ´ÜÁö ÆÄÀÏ÷ºÎ, °Ë»ö µîµî °ü·ÃµÈ °ÍÀε¥
³ªÁß¿¡ ±×°Íµé¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ.
¾Æ·¡ÀÇ ÄÚµå´Â ¸Þ½ÃÁö¸¦ ¾ò¾î¿À´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇϱâ À§ÇÑ ±âº»°ñ°ÝÄÚµåÀÌ´Ù.
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class GetMessageExample {
public static void main (String args[]) throws Exception {
String host = args[0];
String username = args[1];
String password = args[2];
// Create empty properties
// Get session
// Get the store
// Connect to store
// Get folder
// Open read-only
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));
// Get directory
for (int i=0, n=message.length; i < n; i++) {
// Display from field and subject
System.out.println("Do you want to read message? [YES to read/QUIT to end]");
String line = reader.readLine();
if ("YES".equals(line)) {
// Display message
} else if ("QUIT".equals(line)) {
break;
}
}
// Close connection
}
}
|
À§ÀÇ ¼ø¼¸¦ º¸°í¼ ä¿ö³ÖÀ» ¼ö ÀÖ´Ù¸é API¸¦ Àß È°¿ëÇÑ ÄÚµùÀÌ µÉ°ÍÀÌ´Ù.
¹®Á¦°¡ °¡±î¿î ÄÚµùÀÌ ¾Æ´Ò±î?
ÀÚ.. ÀÌÁ¦ À§ °ñ°ÝÄÚµåÀÇ ¼Ò½ºÄڵ带 º¸µµ·Ï ÇÏÀÚ.
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class GetMessageExample {
public static void main (String args[]) throws Exception {
String host = args[0];
String username = args[1];
String password = args[2];
// Create empty properties
Properties props = new Properties();
// Get session
Session session = Session.getDefaultInstance(props, null);
// Get the store
Store store = session.getStore("pop3");
// Connect to store
store.connect(host, username, password); //´Ü¼øÈ÷ ÀÌ ³»¿ë¸¸À¸·Îµµ ¼¹ö·Î Á¢¼ÓÇÑ´Ù.
// Get folder
Folder folder = store.getFolder("INBOX");// INBOXÆú´õ·Î Á¢±ÙÇÑ´Ù.
// Open read-only
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++) {
// Display from field and subject
System.out.println(i + ": " + message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
System.out.println("Do you want to read message? [YES to read/QUIT to end]");
String line = reader.readLine();
//YES¸¦ ´©¸£°Ô µÇ¸é ÇØ´çµÇ´Â ¸ÞÀÏÀÌ ³»¿ëÀ» °¡Á®¿Â´Ù.
// ¾Æ·¡ÀÇ °æ¿ì POP3 INBOXÆú´õÀÇ ³»¿ëÀ» ´Ù¿îÇÏÁö ¾Ê°í °è¼Ó °¡Áö°í ÀÖ°Ô µÈ´Ù.
if ("YES".equals(line)) {
// Display message content
System.out.println(message[i].getContent());
} else if ("QUIT".equals(line)) {
break;
}
}
// Close connection
folder.close(false);
store.close();
}
}
|
¾Æ°í¾Æ°í.. ½±Áö ¾ÊÀº°¡?? ÀÌÁ¦ 7°³ Ŭ·¡½º¿¡ ´ëÇÏ¿© ¸ðµÎ ¾Ë¾ÆºÃ´Ù.
ÀÌÁ¨ ¹«¾ùÀ» ÇÒ°ÍÀΰ¡?
¾ÆÁ÷µµ ¸¹ÀÌ ¸¹ÀÌ ³²¾Ò´Ù.
|