【邮件处理】邮件eml⽂件解析
邮件eml⽂件解析
最近有个需求是下载⽂件服务器上备份的邮件eml⽂件并解析,之后对解析出来的邮件Message进⾏处理
⼤体流程
下载eml⽂件⽣成InputStream流
解析成Message对象
获取Message对象的各种属性进⾏处理
jar包版本
javax.mail-1.5.6.jar
遇到的坑
eml解析后的message⽆法获取邮件内容
//下载eml⽂件
byte[] bytes =downloadEml();
//eml⽂件解析成message
Properties props =new Properties();
Session session = Instance(props);
InputStream inputStream =new ByteArrayInputStream(bytes);
MimeMessage message =new MimeMessage(session, inputStream);
但是获取邮件的附件信息的时候出现了报错
MimeMessageParser mimeMessageParser =new MimeMessageParser((MimeMessage) message).parse();
这⾥报了个错,错误如下:
DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 2 before EOF, the 10 mos t recent characters were:"JSVFT0YK\r\n"
at BASE64DecoderStream.decode(BASE64DecoderStream.java:260)
ad(BASE64DecoderStream.java:144)
at BufferedInputStream.fill(BufferedInputStream.java:246)
ad(BufferedInputStream.java:265)
Content(MimeMessageParser.java:429)
ateDataSource(MimeMessageParser.java:273)
at MimeMessageParser.parse(MimeMessageParser.java:207)
at MimeMessageParser.parse(MimeMessageParser.java:201)
at MimeMessageParser.parse(MimeMessageParser.java:95)
at EmailServiceImpl.saveAttachments(EmailServiceImpl.java:261)
at EmailServiceImpl.processFailEmails(EmailServiceImpl.java:214)
st(TestController.java:53)
at TestController$$FastClassBySpringCGLIB$$cabfcef5.invoke(<generated>)
at MethodProxy.invoke(MethodProxy.java:218)
at CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
at ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
at LogAspectUtil.doAround(LogAspectUtil.java:57)
at NativeMethodAccessorImpl.invoke0(Native Method)
at NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at Method.invoke(Method.java:498)
at AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
at AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
at AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
at CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
at LogMDC.doAround(LogMDC.java:48)
at NativeMethodAccessorImpl.invoke0(Native Method)
at NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at Method.invoke(Method.java:498)
at AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
at AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
at AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
at CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
at ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
at TestController$$EnhancerBySpringCGLIB$$st(<generated>)
at NativeMethodAccessorImpl.invoke0(Native Method)
at NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at Method.invoke(Method.java:498)
java streamat InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) at RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java :878)
at RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) at AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at DispatcherServlet.doService(DispatcherServlet.java:943)
at FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at FrameworkServlet.doGet(FrameworkServlet.java:898)
at HttpServlet.service(HttpServlet.java:626)
at FrameworkServlet.service(FrameworkServlet.java:883)
at HttpServlet.service(HttpServlet.java:733)
at ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at WsFilter.doFilter(WsFilter.java:53)
at ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93)
at OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at StandardContextValve.invoke(StandardContextValve.java:96)
at AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at StandardHostValve.invoke(StandardHostValve.java:139)
at ErrorReportValve.invoke(ErrorReportValve.java:92)
at StandardEngineValve.invoke(StandardEngineValve.java:74)
at CoyoteAdapter.service(CoyoteAdapter.java:343)
at Http11Processor.service(Http11Processor.java:373)
at AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
at SocketProcessorBase.run(SocketProcessorBase.java:49)
at SocketProcessorBase.run(SocketProcessorBase.java:49)
at ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at TaskThread$WrappingRunnable.run(TaskThread.java:61)
at Thread.run(Thread.java:748)
该错误为解析的时候base64异常,且该异常是可以被忽略的,如果没有特殊需求可以直接修改系统配置mail.mime.base64.ignoreerrors 设置为 true忽略改异常
注意是系统配置,不是连接配置
注意是系统配置,不是连接配置
注意是系统配置,不是连接配置
直接在执⾏⽅法前添加该⾏配置
System.setProperty("mail.mime.base64.ignoreerrors","true");
之后可以正常解析邮件内容
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论