javamail出错:com.sun.mail.util.FolderClosedIOException

yutaoshen 2012-11-29 04:05:30
使用javamail读取邮件附件 全是TXT文件 每个邮件大概80-90个附件,一共800多封邮件,读取一定量时 会报如下异常:
com.sun.mail.util.FolderClosedIOException
at com.sun.mail.imap.IMAPInputStream.forceCheckExpunged(IMAPInputStream.java:100)
at com.sun.mail.imap.IMAPInputStream.fill(IMAPInputStream.java:150)
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:182)
at java.io.FilterInputStream.read(FilterInputStream.java:66)
at java.io.PushbackInputStream.read(PushbackInputStream.java:122)
at com.sun.mail.util.QPDecoderStream.read(QPDecoderStream.java:86)
at com.sun.mail.util.QPDecoderStream.read(QPDecoderStream.java:165)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at com.genertech.sahm.smart.parse.utils.JavaMailUtil.saveFile(JavaMailUtil.java:137)
at com.genertech.sahm.smart.parse.utils.JavaMailUtil.getContentWithAttach(JavaMailUtil.java:42)
at com.genertech.sahm.smart.parse.service.impl.ReceiveMailServiceImpl.receiveMail(ReceiveMailServiceImpl.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:311)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:113)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
java.lang.Exception: 文件保存失败!
at com.genertech.sahm.smart.parse.utils.JavaMailUtil.saveFile(JavaMailUtil.java:143)
at com.genertech.sahm.smart.parse.utils.JavaMailUtil.getContentWithAttach(JavaMailUtil.java:42)
at com.genertech.sahm.smart.parse.service.impl.ReceiveMailServiceImpl.receiveMail(ReceiveMailServiceImpl.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:311)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:113)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
------------------第5封邮件----------------------------
2012-11-29 15:54:17,008 [receiveMailQuartzScheduler_Worker-1] ERROR org.quartz.core.JobRunShell -Job DEFAULT.receiveMailCronJobDetail threw an unhandled Exception:
org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'receiveMail' on target class [class com.genertech.sahm.smart.parse.service.impl.ReceiveMailServiceImpl] failed; nested exception is javax.mail.FolderClosedException
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:320)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:113)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
Caused by: javax.mail.FolderClosedException
at com.sun.mail.imap.IMAPMessage.getProtocol(IMAPMessage.java:145)
at com.sun.mail.imap.IMAPMessage.loadFlags(IMAPMessage.java:1377)
at com.sun.mail.imap.IMAPMessage.getFlags(IMAPMessage.java:877)
at com.genertech.sahm.smart.parse.service.impl.ReceiveMailServiceImpl.receiveMail(ReceiveMailServiceImpl.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:311)
... 3 more
2012-11-29 15:54:17,009 [receiveMailQuartzScheduler_Worker-1] ERROR org.quartz.core.ErrorLogger -Job (DEFAULT.receiveMailCronJobDetail threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'receiveMail' on target class [class com.genertech.sahm.smart.parse.service.impl.ReceiveMailServiceImpl] failed; nested exception is javax.mail.FolderClosedException]
at org.quartz.core.JobRunShell.run(JobRunShell.java:224)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
Caused by: org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'receiveMail' on target class [class com.genertech.sahm.smart.parse.service.impl.ReceiveMailServiceImpl] failed; nested exception is javax.mail.FolderClosedException
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:320)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:113)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
... 1 more
Caused by: javax.mail.FolderClosedException
at com.sun.mail.imap.IMAPMessage.getProtocol(IMAPMessage.java:145)
at com.sun.mail.imap.IMAPMessage.loadFlags(IMAPMessage.java:1377)
at com.sun.mail.imap.IMAPMessage.getFlags(IMAPMessage.java:877)
at com.genertech.sahm.smart.parse.service.impl.ReceiveMailServiceImpl.receiveMail(ReceiveMailServiceImpl.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:311)
... 3 more
...全文
1183 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yutaoshen 2012-11-29
  • 打赏
  • 举报
回复
 public static void saveFile(String fileName, InputStream in)
			throws Exception {
    	
		String separator = File.separator;
		
		File storefile = new File(storedir + separator + fileName);
		System.out.println("storefile's path: " + storefile.toString());
		BufferedOutputStream bos = null;
		BufferedInputStream bis = null;
		try {
			bos = new BufferedOutputStream(new FileOutputStream(storefile));
			bis = new BufferedInputStream(in);
			int c;
			while ((c = bis.read()) != -1) {
				bos.write(c);
			}
			bos.flush();
		} catch (Exception exception) {
			exception.printStackTrace();
			throw new Exception("文件保存失败!");
		} finally {
			if(bos!=null){
				bos.close();
			}
			if(bis!=null){
				bis.close();
			}
		}
	}
前面发错 ,这个读取邮件附件的代码
jxplus 2012-11-29
  • 打赏
  • 举报
回复
是否线程方面的问题,加锁试试调一下。
yutaoshen 2012-11-29
  • 打赏
  • 举报
回复

public List readReport(File file) {
		List list=new ArrayList();
		BufferedReader reader=null;
		try {
			if(file.exists()&&file.isFile()){
				reader=new BufferedReader(new FileReader(file));
				String data=null;
				while((data=reader.readLine())!=null){
					Pattern pattern = Pattern.compile(Consts.TITLE_REPORT_DATETIME);
					Matcher matcher = pattern.matcher(data);
					if(!matcher.matches()){
						list.add(data);
					}
				}
				
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(reader!=null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
		return list;
	}


以上是读取邮件附件

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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