有关JAVAmail读取邮件的问题

qq_36567914 2017-03-04 08:27:11
为什么我在设置附件下载地址的时候,能成功放在e盘d盘,但无法放到那下面的子目录中。。。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
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.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
public class TestMail {

private MimeMessage mimeMessage = null ;
private String saveAttachPath = ""; // 附件下载后的存放目录
private StringBuffer bodyText = new StringBuffer(); // 存放邮件内容的StringBuffer对象

/**
* 构造函数,初始化一个MimeMessage对象
*/

public TestMail(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}

public void setMimeMessage(MimeMessage mimeMessage) {
this.mimeMessage = mimeMessage;
}

/**
*  * 获得发件人的地址和姓名  
*/
public String getFrom() throws Exception{
InternetAddress address[] = (InternetAddress[])mimeMessage.getFrom();
String from = address[0].getAddress() ;
if(from == null){
from = " " ;
System.out.println("未知发送来源");
}

String personal = address[0].getPersonal() ;
if(personal == null){
personal = " " ;
System.out.println("未知发送者");
}

String fromAddr = null ;
if (personal != null || from != null) {
fromAddr = personal + "<" + from + ">";
System.out.println("发送者是:" + fromAddr);
} else {
System.out.println("无法获得发送者信息.");
}
return fromAddr;
}

/**
*  * 获得邮件主题  
*/
public String getSubject() throws MessagingException {
String subject = " " ;
try{
subject = MimeUtility.decodeText(mimeMessage.getSubject());
if (subject == null) {
subject = "";
}
}catch(Exception e){
e.printStackTrace();
}
return subject ;
}

/**
* 判断此邮件是否已读,如果未读返回false,反之返回true
*/
public boolean isNew() throws MessagingException {
boolean isNew = false ;
Flags flags = mimeMessage.getFlags() ;
Flags.Flag[] flag = flags.getSystemFlags() ; //返回Flags对象中所有的系统标记
for(int i = 0 ; i<flag.length ; i++){
if(flag[i] == Flags.Flag.SEEN)
isNew = true ;
}
return isNew;
}

/**
* 判断此邮件是否包含附件
*/
public boolean isContainAttach(Part part) throws Exception{
boolean attachFlag = false;
if(part.isMimeType("multipart/*")){
Multipart mp = (Multipart) part.getContent();
for(int i = 0 ; i<mp.getCount() ; i++){
BodyPart mPart = mp.getBodyPart(i);
String disposition = mPart.getDisposition();
if((disposition != null)
&& ((disposition.equals(Part.ATTACHMENT)) || (disposition.equals(Part.INLINE))))
//ATTACHMENT<附件> INLINE<嵌入>
attachFlag = true;
else if (mPart.isMimeType("multipart/*")){
attachFlag = isContainAttach((Part) mPart);
} else{
String conType = mPart.getContentType();

if (conType.toLowerCase().indexOf("application") != -1)
attachFlag = true;
if (conType.toLowerCase().indexOf("name") != -1)
attachFlag = true;
}
}
} else if (part.isMimeType("message/rfc822")) {
attachFlag = isContainAttach((Part)part.getContent());
}
return attachFlag;

}

/**
* 保存附件
* @param part 邮件中多个组合体中的其中一个组合体
* @param destDir 附件保存目录
* @throws UnsupportedEncodingException
* @throws MessagingException
* @throws FileNotFoundException
* @throws IOException
*/
public static void saveAttachment(Part part, String destDir) throws UnsupportedEncodingException, MessagingException,
FileNotFoundException, IOException {
if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent(); //复杂体邮件
//复杂体邮件包含多个邮件体
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
//获得复杂体邮件中其中一个邮件体
BodyPart bodyPart = multipart.getBodyPart(i);
//某一个邮件体也有可能是由多个邮件体组成的复杂体
String disp = bodyPart.getDisposition();
if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
InputStream is = bodyPart.getInputStream();
saveFile(is, destDir, decodeText(bodyPart.getFileName()));
} else if (bodyPart.isMimeType("multipart/*")) {
saveAttachment(bodyPart,destDir);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) {
saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName()));
}
}
}
} else if (part.isMimeType("message/rfc822")) {
saveAttachment((Part) part.getContent(),destDir);
}
}

/**
* 读取输入流中的数据保存至指定目录
* @param in 输入流
* @param fileName 文件名
* @param destDir 文件存储目录
* @throws FileNotFoundException
* @throws IOException
*/
private static void saveFile(InputStream in, String destDir, String fileName)
throws FileNotFoundException, IOException {
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(new File(destDir+fileName)));
int len = -1;
while ((len = bis.read()) != -1) {
bos.write(len);
bos.flush();
}
bos.close();
bis.close();
}

/**
* 文本解码
* @param encodeText 解码MimeUtility.encodeText(String text)方法编码后的文本
* @return 解码后的文本
* @throws UnsupportedEncodingException
*/
public static String decodeText(String encodeText) throws UnsupportedEncodingException {
if (encodeText == null || "".equals(encodeText)) {
return "";
} else {
return MimeUtility.decodeText(encodeText);
}
}


/**
* 测试
*/
public static void main(String args[]) throws Exception{
String host = "pop.163.com";
String username = "*********";
String password = "*************"; //邮箱信息
String protocol = "pop3";

Properties prop = new Properties();//Properties类用于读取Java的配置文件
prop.setProperty("mail.pop3.store","pop3");
prop.setProperty("mail.pop3.host","pop.163.com");
Session session = Session.getDefaultInstance(prop, null);//Session类用于定义应用程序所需的环境信息


Store store = session.getStore(protocol);
store.connect(host, username, password);

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
System.out.println("邮件数量: " + message.length);
TestMail re = null;

for (int i = 0;i < message.length; i++){
re = new TestMail((MimeMessage) message[i]);
System.out.println("邮件 " +(i+1) + " 发送人地址: " + re.getFrom());
System.out.println( " 是否已读: " + re.isNew());
System.out.println( " 是否包含附件: "
+ re.isContainAttach((Part) message[i]));
System.out.println( " 主题: " + re.getSubject());
System.out.println();
re.saveAttachment((Part)message[i],"D:\\cp1");
}
}
}
...全文
175 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36567914 2017-03-04
  • 打赏
  • 举报
回复
为啥不能成功放入这些盘的文件夹中,要怎么改。。。。。。。

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧