获取AEM自适应表单中的文件附件路径以在JSP中用于发送带附件的电子邮件?

时间:2015-10-26 13:53:26

标签: java jsp cq5 aem

我是AEM和Adaptive Forms的新手,我正在为表单制作自定义操作。

我的问题是,我不知道如何从文件附件组件中获取信息,以便在我的JSP代码中发送电子邮件。

这是我现在拥有的代码的一部分:

ValueMap props = ResourceUtil.getValueMap(resource);
HtmlEmail email = new HtmlEmail();
String[] mailTo = props.get("mailto", new String[0]);
email.setFrom((String)props.get("from"));
    for (String toAddr : mailTo) {
        email.addTo(toAddr);
  }

//EmailAttachment attachment = new EmailAttachment();

//What do i need to do from here to set the attachment?

email.setHtmlMsg((String)props.get("template"));

email.setSubject((String)props.get("subject"));
MessageGatewayService messageGatewayService = sling.getService(MessageGatewayService.class);
MessageGateway messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
messageGateway.send(email);

1 个答案:

答案 0 :(得分:4)

最后我明白了!我正在分享帮助其他人的答案。

这是我的最终代码:

ValueMap props = ResourceUtil.getValueMap(resource);
    HtmlEmail email = new HtmlEmail();
    String[] mailTo = props.get("mailto", new String[0]);
    email.setFrom((String)props.get("from"));
        for (String toAddr : mailTo) {
            email.addTo(toAddr);
      }
    //========Email Attachments===============
    for (Map.Entry<String, RequestParameter[]> param : slingRequest.getRequestParameterMap().entrySet()) {
        RequestParameter rpm = param.getValue()[0];
        if(!rpm.isFormField()) {
            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath(rpm.getFileName());
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription("Any Description");
            attachment.setName("Any name you can set");
            email.embed(new ByteArrayDataSource(rpm.get(), rpm.getContentType()), rpm.getFileName());
        }
    }
    //========Email Attachment END===========

    String emailTextToSend = "<p>Name: " + slingRequest.getParameter("name") + "</p>";
    emailTextToSend += "<p>Message: " + slingRequest.getParameter("message") + "</p>";
    email.setHtmlMsg(emailTextToSend);
    email.setSubject((String)props.get("subject"));
    MessageGatewayService messageGatewayService = sling.getService(MessageGatewayService.class);
    MessageGateway messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
    messageGateway.send(email);

希望它可以帮助其他人。