有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

缺少java MIMessage附件

我正在尝试使用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, check the 'emailListener' properties.", 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, Consumer<Object> handlerMethod, 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获得电子邮件附件的例子吗

提前谢谢


共 (0) 个答案