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());
}
}
}
}
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());
}
}
}
}
相关文章推荐
- 关于用记事本编写和使用java包
- Struts1和Struts2的区别和对比(完整版)
- Java开发中的23种设计模式详解(转)
- java注解
- 遇到 java.lang.OutOfMemoryError: PermGen space配多少合适我的内存为4G在哪配置Jboss的run.bat还是。。
- Java中普通代码块,构造代码块,静态代码块区别及代码示例
- Java 跳出单个循环、多重嵌套循环
- import java.io.*;
- Javassist 动态生成类
- Java获取工程目录下图片资源
- 从”JAVA“而终 18:java Jdbc编程(ResultSet)集合讲解
- java JDK 动态代理(如何使用,以及工作原理解析)
- Stooge sort: Sorting by Exchanging
- 多态性
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- java基本集成开发环境搭建
- java 验证手机号码、电话号码(包括最新的电信、联通和移动号码)
- 第7条:避免使用终结方法
- java两种方式遍历map
- 方法重载和重写