Java를 이용한 Mail 처리에는 JavaMail API와 JavaBeans Activation Freamework(JAF)가 필요하다. 각각의 Jar파일은 다음에서 받을 수 있다.
JavaMail API : http://java.sun.com/products/javamail/downloads/index.html
JAF : http://java.sun.com/products/javabeans/jaf/downloads/index.html
---------------
/*
* @(#) ReadMailFileSample.java
* Created on 2007. 03. 23
*
* Copyright (c) 2007 Woorinfo.net All rights reserved.
*
* NOTICE ! You can copy or redistribute this code freely, but you should not
* remove the information about the copyright notice and the author.
*
* @author Haewoo Kang (haewoo@woorinfo.net)
*
*/
package study.javaMail;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import com.sun.mail.util.BASE64DecoderStream;
@SuppressWarnings( { "unchecked", "unused" })
public class ReadMailFileSample {
/**
* @param args
* @throws MessagingException
* @throws IOException
*/
public static void main(String[] args) throws MessagingException, IOException {
Properties props = System.getProperties();
props.put("mail.host", "used.mail.host.name");
props.put("mail.transports.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
InputStream source = new FileInputStream("d:/Temp/ccc.eml");
MimeMessage message = new MimeMessage(mailSession, source);
System.out.println("Subject : " + message.getSubject());
System.out.print("From : ");
Address[] fromAddr = message.getFrom();
for (Address address : fromAddr) {
System.out.println(MimeUtility.decodeText(address.toString()));
}
System.out.println("---------------------------");
Object objContent = message.getContent();
MimeMultipart mm = null;
if ( objContent instanceof Multipart ) {
mm = (MimeMultipart) objContent;
int bodyCount = mm.getCount();
System.out.println("Multipart Ccount : " + mm.getCount());
System.out.println("Body : ");
for (int i = 0; i < bodyCount; i++) {
BodyPart bp = mm.getBodyPart(i);
// printAllHeader(bp);
printBody(bp);
}
}
else {
System.out.println(objContent);
}
}
private static void printBody(BodyPart bp) throws IOException, MessagingException {
Object obj = bp.getContent();
if (obj instanceof BASE64DecoderStream) {
String[] aaa = bp.getHeader("Content-Disposition");
System.out.println("Content-Disposition : " + aaa[0]);
BASE64DecoderStream newObj = (BASE64DecoderStream) obj;
String filename = MimeUtility.decodeText(bp.getFileName());
File attacheFile = new File("d:/Temp/" + filename);
FileOutputStream fos = new FileOutputStream(attacheFile);
byte[] buffer = new byte[1024];
while (0 < newObj.read(buffer)) {
fos.write(buffer);
}
newObj.close();
fos.close();
System.out.println("Filename : " + filename);
} else {
System.out.println("Body Type : " + bp.getContentType());
System.out.println(obj);
}
}
private static void printAllHeader(BodyPart bp) throws MessagingException {
Enumeration<Header> enumHeaders = bp.getAllHeaders();
while (enumHeaders.hasMoreElements()) {
Header aa = enumHeaders.nextElement();
System.out.println(aa.getName() + " : " + aa.getValue());
}
}
}
[출처] [본문스크랩] Mail File(eml) 읽기 Sample|작성자 행복가득