java 下载邮箱附件到指定目录
来源:互联网 发布:算法第四版 豆瓣 编辑:程序博客网 时间:2024/04/28 22:32
下载邮箱附件的工具类:
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());
}
}
}
}
0 0
- java 下载邮箱附件到指定目录
- 邮箱附件 保存到指定的文件夹
- JAVA自动生成sql结果到excel,并以附件方式发送到指定邮箱
- Android发送邮件到指定邮箱(可带附件)
- 下载邮箱附件
- 下载数据库文件到指定目录
- JAVA远程下载zip包并解压到指定目录
- python:自动压缩指定文件夹作为附件发送邮件到指定邮箱
- 保存附件到Domino Server端指定目录
- linux上mysql数据备份并自动通过附件发送到指定邮箱
- 将从数据库导出的数据以excel的附件形势发送到指定邮箱
- 使用两种方式,Android 发送文件到指定邮箱(带多个附件)
- Maven下载jar 包到指定目录
- android 将文件下载到指定目录
- 从ftp下载指定文件到指定的目录
- 网易邮箱附件下载困难解决之道
- Android将日志信息自动发送到指定的邮箱中 邮件的内容以附件形式发送
- Android将日志信息自动发送到指定的邮箱中 邮件的内容以附件形式发送
- JAVA中获取项目文件路径[转载的一篇关于 相对路径 的文章]
- 对象的初始化过程
- Notepad++ 快捷键 大全
- Objective-C 程序设计 第十一章
- Range Sum Query 2D - Immutable
- java 下载邮箱附件到指定目录
- php 链接 oracle
- JNDI
- 单例模式优化探索
- bootSt导航
- Android的登陆界面生成验证码
- mysql基础教程(3)---游标
- 从网易与淘宝的font-size思考前端设计稿与工作流
- OBIEE分析开发-获取当月第一天