最近有个需求是下载文件服务器上备份的邮件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 = Session.getInstance(props);
InputStream inputStream = new ByteArrayInputStream(bytes);
MimeMessage message = new MimeMessage(session, inputStream);

但是获取邮件的附件信息的时候出现了报错

MimeMessageParser mimeMessageParser = new MimeMessageParser((MimeMessage) message).parse();

这里报了个错,错误如下:

com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 2 before EOF, the 10 most recent characters were: "JSVFT0YK\r\n"
	at com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:260)
	at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:144)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
	at org.apache.commons.mail.util.MimeMessageParser.getContent(MimeMessageParser.java:429)
	at org.apache.commons.mail.util.MimeMessageParser.createDataSource(MimeMessageParser.java:273)
	at org.apache.commons.mail.util.MimeMessageParser.parse(MimeMessageParser.java:207)
	at org.apache.commons.mail.util.MimeMessageParser.parse(MimeMessageParser.java:201)
	at org.apache.commons.mail.util.MimeMessageParser.parse(MimeMessageParser.java:95)
	at com.my.service.impl.EmailServiceImpl.saveAttachments(EmailServiceImpl.java:261)
	at com.my.service.impl.EmailServiceImpl.processFailEmails(EmailServiceImpl.java:214)
	at com.my.controller.TestController.test(TestController.java:53)
	at com.my.controller.TestController$$FastClassBySpringCGLIB$$cabfcef5.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
	at com.my.global.log.util.LogAspectUtil.doAround(LogAspectUtil.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
	at com.my.global.log.util.LogMDC.doAround(LogMDC.java:48)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
	at com.my.controller.TestController$$EnhancerBySpringCGLIB$$7ec38af2.test(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
 

https://community.oracle.com/tech/developers/discussion/1590013/java-io-ioexception-error-in-encoded-stream-needed-4-valid-base64-charact

该错误为解析的时候base64异常,且该异常是可以被忽略的,如果没有特殊需求可以直接修改系统配置mail.mime.base64.ignoreerrors设置为 true忽略改异常
注意是系统配置,不是连接配置
注意是系统配置,不是连接配置
注意是系统配置,不是连接配置

直接在执行方法前添加该行配置

System.setProperty("mail.mime.base64.ignoreerrors", "true");

之后可以正常解析邮件内容

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入欢迎使用Markdown编辑器你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Mar import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import jav... BASE64Encoder encoder = new BASE64Encoder(); String imagestr = encoder.encode(captcha); BASE64Decoder decoder = new BASE64Decoder(); byte[] bytes = decoder.decodeBuffer(imagestr); import ... public static void test8() throws IOException { FileInputStream fis = new FileInputStream("C:/Users/Lenovo/Desktop/design.jpg"); StringBuilder sb = new String
如需转载,请注明出处。有时候下班过目里面会出现这个情况。 BASE64Decoder这个类明明有,ctrl+左键也能点出来,但是老是下面报红线,提示找不到这个东西。 解决方案:**步骤一: 右键项目选择properties;** **步骤二: 选择左边列表栏的:java Build Path** **步骤三:
一直以来Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类。这人个类是sun公司的内部方法,并没有在java api中公开过,不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。但是在eclipse和MyEclipse中直接使用,却找不到该类。如下图!
使用Base64加解密时,eclipse的设置 public static void main(String[] args){ String s=“H4sIAAAAAAAAAJVTPW/VMBT9K/wD2/kgbXVlyc/xo1Ff7JA4iNfFS6dKTJ3K72FDQgiQqkclGJgK”+ “A1FB3ehQ9krdUCfs5MWJURey+N5zj2/uxzEsi5Uwel0JSqKEAJp8WLYyN0VOX5xg7CJbF2pRqVoP”+ “JBeY+7AQTwppcqYFjXBEcI
public String encode(String src) byte[] encodeBytes = Base64.getEncoder().encode(src.getBytes()); return new String(encodeBytes); public String decode(String src) byte[] decodeBytes = Base64.getDecoder().decode(src.getBytes());...
当我们需要处理邮件时,可能会遇到eml文件eml文件邮件的一种标准存储格式,通常我们需要读取、解析eml文件,然后将其转换成网页展示。 以下是处理eml文件的详细步骤: 1. 读取eml文件 首先,我们需要读取eml文件的内容,可以使用PHP的file_get_contents函数来读取eml文件的内容,例如: $content = file_get_contents('test.eml'); 2. 解析eml文件 读取eml文件的内容后,我们需要将其解析邮件的各个部分,可以使用PHP的mailparse扩展来解析eml文件,例如: $resource = mailparse_msg_create(); mailparse_msg_parse($resource, $content); 解析eml文件后,我们可以获取邮件的各个部分,包括邮件头、邮件正文、附件等。 3. 生成网页 将eml文件解析邮件的各个部分后,我们需要将其转换成网页展示,可以使用PHP的相关函数来生成网页,例如: - 生成邮件头 $headers = mailparse_msg_get_headers($resource); echo "<h2>邮件头</h2>"; echo "<pre>" . htmlspecialchars($headers) . "</pre>"; - 生成邮件正文 $body = mailparse_msg_get_part_data($resource, "1.1"); echo "<h2>邮件正文</h2>"; echo "<pre>" . htmlspecialchars($body) . "</pre>"; - 生成附件 $attachments = mailparse_msg_get_structure($resource); foreach ($attachments as $attachment) { $part = mailparse_msg_get_part($resource, $attachment); $filename = mailparse_msg_get_part_filename($part); $attachment_data = mailparse_msg_get_part_data($part); echo "<h2>附件:" . $filename . "</h2>"; echo "<pre>" . htmlspecialchars($attachment_data) . "</pre>"; 以上就是处理eml文件的详细步骤,通过以上步骤,我们可以将eml文件转换成网页展示,并且方便我们进行后续的处理