您的位置:首页 > 编程语言 > Java开发

java 下载邮箱附件到指定目录

2015-11-12 12:00 399 查看
下载邮箱附件的工具类:

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.Properties;

import javax.mail.Flags;

import javax.mail.Folder;

import javax.mail.Message;

import javax.mail.Multipart;

import javax.mail.Part;

import javax.mail.Session;

import javax.mail.Store;

import javax.mail.URLName;

/**

* 下载存储名片全能王的邮箱附件 工具类

* @author chenfanglin

* @date 2015年5月25日 下午7:00:14

*/

public class DownloadCCUtil {

public static void main(String[] args) {

File file = new File("/data0");

file.mkdir();

new DownloadCCUtil();

}

public DownloadCCUtil(){

try {

DownloadCCUtil.getEmailWithSubjectContaining("/data0", false);

} catch (Exception e) {

e.printStackTrace();

}

}

public static Folder getMailFolder() throws Exception {

Folder folder = null;

String email = "";// 邮箱地址 (指定一个邮箱)

String password = "";// 邮箱登陆密码(密码)

URLName urlname = new URLName("pop3","pop.exmail.qq.com",110,null,email,password);

// 1. 设置连接信息, 生成一个 Session

Properties props = new Properties();

props.setProperty("mail.smtp.host", "pop.exmail.qq.com");

props.setProperty("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(props);

Store store = session.getStore(urlname);

store.connect();

folder = store.getDefaultFolder();// 默认父目录

Folder popFolder = folder.getFolder("INBOX");

return popFolder;

}

public static void getEmailWithSubjectContaining(String filepath,

boolean delete) throws Exception {

Folder folder = getMailFolder();

if (folder == null) {

return;

}

folder.open(Folder.READ_WRITE);

Message[] msgs = folder.getMessages();

System.out.println("Totally there are " + msgs.length);

for (int i = msgs.length-1; i > msgs.length-2; i--) {

String pathToSave = filepath;

if(!folder.isOpen()){

folder.open(Folder.READ_WRITE);

}

String subject = msgs[i].getSubject();

System.out.println("Email subject: " + subject);

Part p = (Part) msgs[i];

dumpAttachment(p, pathToSave);

msgs[i].setFlag(Flags.Flag.DELETED, delete); //得到邮件后删除

}

// folder.expunge(); //清除邮箱里DELETE的邮件

folder.close(false);

}

public static void dumpAttachment(Part part, String filepath) throws Exception{

if (part.isMimeType("text/plain")) {

System.out.println("This is plain text");

} else if (part.isMimeType("multipart/*")) {

System.out.println("This is a Multipart, trying to dig into the attachment now");

Multipart mp = (Multipart) part.getContent();

int count = mp.getCount();

for (int i = 0; i < count; i++) {

dumpAttachment(mp.getBodyPart(i), filepath);

}

} else if (part.isMimeType("message/rfc822")) {

System.out.println("This is a Nested Message");

dumpAttachment((Part) part.getContent(), filepath);

} else {

Object o = part.getContent();

String attachmentFilename = "no_name";

if (part.getFileName() != null && !part.getFileName().equals("")) {

attachmentFilename = part.getFileName();

}

if (o instanceof String) {

System.out.println("This is a string");

} else if (o instanceof InputStream) {

System.out.println("This is just an input stream");

InputStream is = (InputStream) o;

FileOutputStream fos = new FileOutputStream(filepath + "/" + attachmentFilename);

int c;

int count = 0;

System.out.print("Saving attachment: " + attachmentFilename);

while ((c = is.read()) != -1) {

fos.write(c);

if (count % 1000000 == 0) {

} else if (count % 5000 == 0) {

}

count ++;

}

} else {

System.out.println("This is an unknown type");

System.out.println(o.toString());

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: