MimeMessage 附件丢失

问题描述

我正在尝试使用 Java 从电子邮件获取附件。我的代码是:

    public IntegrationFlowContext.IntegrationFlowRegistration createEmailListener(String id,String protocol,String server,String inBox,String username,String password,Consumer<Object> handlerMethod,SearchTermStrategy filter) {
        try {
            return this.flowContext.registration(buildFlow(String.format("%s://%s:%s@%s/%s",protocol,URLEncoder.encode(username,"UTF-8"),password,server,inBox),handlerMethod,filter))
                            .id(id)
                            .useFlowIdAsPrefix()
                            .register();
        } catch (UnsupportedEncodingException e){
            LOGGER.error("There was an error trying to create a connection string. Please,check the 'emailListener' properties.",e);
            throw new RuntimeException("There was an error trying to create a connection string. Please,e);
        } catch (Exception e){
            LOGGER.error("There was an unexpected error trying to open a connection with the email server.",e);
            throw new RuntimeException("There was an unexpected error trying to open a connection with the email server.",e);
        }
    }

    private IntegrationFlow buildFlow(String datasourceUrl,SearchTermStrategy filter) {
        return IntegrationFlows
                .from(Mail.imapIdleAdapter(datasourceUrl)
                        .autoStartup(true)
                        .searchTermStrategy(filter)
                        .javaMailProperties(p -> p.put("mail.debug","false")
                                .put("mail.imap.connectionpoolsize","5"))
                        .shouldReconnectAutomatically(true)
                        .shouldMarkMessagesAsRead(true)
                        .headerMapper(new DefaultMailHeaderMapper()))
                .handle(handlerMethod)
                .get();
    }

    public void proccessEmail(Object m){
        try {
            Session session = Session.getDefaultInstance(new Properties());
            MimeMessage msg = new MimeMessage(session,new ByteArrayInputStream((byte[])m));

            //final MimeMessageParser mimeMessageParser = new MimeMessageParser(msg).parse();

            Multipart multiPart = (Multipart) msg.getContent();

            for (int i = 1; i < multiPart.getCount(); i++) {
                MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
            }
        } catch (Exception e) {
            e.printstacktrace();
        }
    }

问题是,如果我检查“msg”变量,它包含电子邮件包括附件):

--000000000000c177b905b8dac400
Content-Type: text/plain; charset="UTF-8"

kkkkkkkkkkkkkkkkkkkkk

--000000000000c177b905b8dac400
Content-Type: text/html; charset="UTF-8"

<div dir="ltr">kkkkkkkkkkkkkkkkkkkkk</div>

--000000000000c177b905b8dac400--
--000000000000c177bb05b8dac402
Content-Type: application/octet-stream; name="datos.txt"
Content-disposition: attachment; filename="datos.txt"
Content-transfer-encoding: base64
X-Attachment-Id: f_kjwsb0go0
Content-ID: <f_kjwsb0go0>

PD9waHAgDQoNCnJlcxvpcmVfb25jZShyZWFscGF0aChkaXJuYW1lKF9fRklMRV9fKSkgLiAiLy4u
L2NsYXNzZXMvZGJjLnBocCIpOw0KDQpjbGFzcyBwaG9lbml4IHsNCgkvLyBBcXXDrSBlc3TDoW4g
...
dHJpY3VsYXMgYXMgJG0pew0KCQkJYXJyYXlfcHVzaCgkbWF0cmljdWxhczIsICRtWyJNQVRSSUNV
TEEiXSk7DQoJCX0NCgkJcmV0dXJuICRtYXRyaWN1bGFzMjsNCgl9DQp9Pz4=
--000000000000c177bb05b8dac402--

但是,当我收到电子邮件的部分时,附件部分不在那里。 为了重现它,我正在发送和接收来自 gmail 帐户的电子邮件。 有谁知道为什么? 有人有 IntegrationFlows 获取电子邮件附件的示例吗?

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)