¢ºGet Attatchment
####################################################
# Java Mail API 1.2¸¦ ÀÌ¿ëÇÑ À¥¸ÞÀÏ ½Ã½ºÅÛÀÇ ±¸Çö. #
####################################################
÷ºÎµÈ ³»¿ë °¡Á®¿À±â
¸Þ½ÃÁö¿Í ÇÔ²² ÷ºÎµÇ¾î ÀÖ´Â ³»¿ëµéÀ» °¡Á®¿À´Â °ÍÀº º¸³¾¶§Ã³·³ ÀϹÝÀûÀÎ MIMEÀÇ
°³³äÀ¸·Î »ý°¢Çؼ´Â ¾ÈµÈ´Ù. ¸Þ½ÃÁöÀÇ ³»¿ëºÎºÐÀÌ Ã·ºÎµÈ ³»¿ëµéÀ» °¡Áö°í ÀÖÀ»¶§
Multipart °´Ã¼¸¦ »ç¿ëÇØ¾ß Çϴµ¥ ÀÌ ¶§ °¢°¢¿¡ ´ëÇÏ¿© ¸Þ½ÃÁöÀÇ ³»¿ë°ú ÷ºÎ(attachement)¿¡
´ëÇÑ ¼·Î ´Ù¸¥ ÇÁ·Î¼¼½º¸¦ Á¤ÀÇÇØÁà¾ß ÇÑ´Ù.
¸¸¾à ¾î¶² ÷ºÎµÈ ³»¿ëÀÌ ÀÖ´Ù¸é °¢°¢ÀÇ PartµéÀº part.getDispostion()¸Þ¼Òµå·ÎºÎÅÍ
Part.ATTACHMENTÀÇ ¼Ó¼ºÀ» °¡Áö°í ÀÖ¾î¾ß ÇÑ´Ù. ¾î·µç ÷ºÎµÇ´Â ³»¿ëÀº ¶ÇÇÑ
non-text MIMEÇüÅÂÀϼöµµ ÀÖ°í, Part.INLINEÇüÅÂÀϼöµµ ÀÖ´Ù.
°á±¹ INLINEÇüųª ATTACHMENT¸¦ Part°¡ °¡Áö°í ÀÖ´Ù¸é getFileName()°ú getInputStream()À»
ÀÌ¿ëÇÏ¿© ÆÄÀÏÀÇ ³»¿ëÀ» »©³»¾î¾ß ÇÒ°ÍÀÌ´Ù
¾Æ·¡ÂÊÀÇ Äڵ带 Çѹø º¸µµ·Ï ÇÏÀÚ.
ContentÀÇ ³»¿ëÀ» Multipart·Î °¡Á®¿Â ÈÄ ¿©·¯°³ÀÇ part°¡ ¸Þ½ÃÁöÀÇ content¿¡ Æ÷ÇԵǾîÁ® ÀÖ´Ù¸é
°¢°¢ÀÇ bodyPartµé partÀÎÅÍÆäÀ̽ºÀÇ ÇüÅ·Π´ãµµ·Ï ÇÑ´Ù.
±×¸®°í °¢°¢ÀÇ part¿¡ ´ëÇÏ¿© getDisposition()¸Þ¼Òµå¸¦ È£ÃâÇÏ¿©, ATTACHMENT¼Ó¼ºÀ̳ª
INLINE¼Ó¼ºÀ» °¡Áö°í ÀÖ´Ù¸é ÆÄÀÏÀÇ ³»¿ëÀ» »©³¾¼ö ÀÖµµ·Ï ÄÚµùÇÏ¸é µÈ´Ù.
Multipart mp = (Multipart)message.getContent();
for (int i=0, n=multipart.getCount(); i < n; i++) {
Part part = multipart.getBodyPart(i));
String disposition = part.getDisposition();
if ((disposition != null) &&
((disposition.equals(Part.ATTACHMENT) ||
(disposition.equals(Part.INLINE))) {
saveFile(part.getFileName(), part.getInputStream());
}
}
saveFile()¸Þ¼Òµå´Â ÆÄÀÏÀ̸§°ú ½ºÆ®¸²À¸·ÎºÎÅÍ byteµéÀ» Àд Äڵ带 ³ÖÀ¸¸é µÈ´Ù.
¸¸¾à ÇØ´ç ÆÄÀÏÀÌ ÀÌ¹Ì Á¸ÀçÇÑ´Ù¸é ÆÄÀÏÀ̸§ÀÇ ³¡¿¡ ¼ýÀÚ¸¦ ºÙ¿©¼ º¯È¯½ÃÅ°°Ô µÈ´Ù.
// saveFile()ÄÚµå
File file = new File(filename);
for (int i=0; file.exists(); i++) {
file = new File(filename+i);
}
À§ÀÇ Äڵ尰Àº °æ¿ì´Â ´Ü¼øÈ÷ message partµéÀÇ Ç÷¡±× °ªÀÌ Å¸´çÇßÀ» °æ¿ì¸¸ »ç¿ëµÇ¾î Áú¼ö Àִµ¥,
¸ðµç °æ¿ì¸¦ µûÁø´Ù¸é ¾Æ·¡ÀÇ ÄÚµåó·³ dispositionÀÌ nullÀÏ °æ¿ì¿Í handle¿¡ µû¶ó¼ partÀÇ
MIMEŸÀÔÀ» ¾ò¾î¾ß ÇÑ´Ù.
if (disposition == null) {
// Check if plain
MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
// Handle plain
} else {
// Special non-attachment cases here of image/gif, text/html, ...
}
...
}
|