以下提供範例, 僅供參考
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MailReceiver {
private static final Log log = LogFactory.getLog(MailReceiver.class);
private String host = null;
private String username = null;
private String password = null;
private String attachPath = null;
// receive
public void reveiveMail() throws Exception {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("pop3");
store.connect(getHost(), getUsername(), getPassword());
// default 是 inbox
Folder folder = store.getFolder("INBOX");
// can delete, 設定對信箱的操作
folder.open(Folder.READ_WRITE);
// 讀取信件
Message message[] = folder.getMessages();
// 有幾封信
int messageLength = message.length;
log.info("Messages's length: " + messageLength);
for (int i = 0; i < message.length; i++) {
// 設定對信的操作, 接收完要不要刪除
message[i].setFlag(Flags.Flag.DELETED, false);
handleMultipart(message[i]);
}
// close
if (folder != null) {
folder.close(true);
}
if (store != null) {
store.close();
}
}
// handle
private void handleMultipart(Message msg) throws Exception {
handle(msg);
String disposition;
Multipart mp = (Multipart) msg.getContent();
int mpCount = mp.getCount();
for (int m = 0; m < mpCount; m++) {
BodyPart part = mp.getBodyPart(m);
disposition = part.getDisposition();
// 處理附件
if (disposition != null && disposition.equals(Part.ATTACHMENT)) {
saveAttach(part, getAttachPath());
} else {
// 其它的物件
Object obj = part.getContent();
if (obj instanceof MimeMultipart) {
MimeMultipart mm = (MimeMultipart) obj;
showContent(mm);
}
}
}
}
private void showContent(MimeMultipart mm) {
try {
log.info("content-type : " + mm.getContentType());
int count = mm.getCount();
log.info("count : " + count);
for (int i = 0; i < count; i++) {
log.info(i + "----");
log.info(mm.getBodyPart(i).getContent());
}
} catch (MessagingException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
private static void handle(Message msg) throws Exception {
log.info("郵件主題 : " + msg.getSubject());
log.info("郵件作者 : " + Arrays.toString(msg.getFrom()));
log.info("發送日期 : " + msg.getSentDate());
}
private static void saveAttach(BodyPart part, String filePath)
throws Exception {
String temp = part.getFileName();
log.info("temp file name : " + temp);
String fileName = temp;
if (temp.length() > 8 && temp.indexOf("?=") != -1) {
String s = temp.substring(8, temp.indexOf("?="));
fileName = new String(new Base64().decode(s.getBytes("big5")), "big5");
}
log.info(" get attachment : " + fileName);
InputStream in = part.getInputStream();
// save
log.info(filePath + "" + fileName);
FileOutputStream out = new FileOutputStream(new File(filePath + fileName));
IOUtils.copy(in, out);
}
public String getAttachPath() {
return attachPath;
}
public void setAttachPath(String attachPath) {
this.attachPath = attachPath;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static void main(String[] args) {
MailReceiver receiver = new MailReceiver();
receiver.setHost("staff.pchome.com.tw");
receiver.setUsername("1234");
receiver.setPassword("1234");
receiver.setAttachPath("D:\\email\\");
new File(receiver.getAttachPath()).mkdirs();
try {
receiver.reveiveMail();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
沒有留言:
張貼留言