从 DAM 附加文件以在 cq/AEM 中发送邮件

attach file from DAM to send mail in cq/AEM

我试图从 cq-DAM 发送邮件但失败了。

我可以从我自己的电脑发送文件,我想从 DAM 获取文件

这是代码::

  @SuppressWarnings("unchecked")
    final Enumeration<String> parameterNames = request.getParameterNames();
    final Map<String, String> parameters = new HashMap<String, String>();
    while (parameterNames.hasMoreElements()) {
        final String key = parameterNames.nextElement();
        parameters.put(key, request.getParameter(key));

    }
    Resource templateRsrc = request.getResourceResolver().getResource("/etc/notification/send-email.html");
    String mailId = request.getParameter("mailId");
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath("/media/intelligrape/MyFiles/Xtra/Crafting/Paper Work/images.jpg");
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("Picture of John");
    attachment.setName("John");

    int code = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    try {
        final MailTemplate mailTemplate = MailTemplate.create(templateRsrc.getPath(), templateRsrc.getResourceResolver().adaptTo(Session.class));
        final HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(parameters), HtmlEmail.class);
        logger.error("PROPERTIES****************************     "+parameters);
        email.addTo(mailId);
        email.attach(attachment);

        URL url = new URL("<any URL except localhost>");
        String cid = email.embed(url, "Apache logo");

        email.setHtmlMsg("<html><body>The ig logo - <img src=\"cid:"+cid+"\"></body></html>");

        email.setTextMsg("Your email client does not support HTML messages");
        messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
        messageGateway.send(email);

提前致谢..:)

您可以获取 DAM 图像的 InputStream 并将其转换为 ByteArrayDataSource and then use attach(datasource,name,description) 方法以将 DAM 图像附加到电子邮件。

Node contentNode = imageNode.getNode("jcr:content");
Binary imageBinary = contentNode.getProperty("jcr:data").getBinary();
InputStream imageStream = imageBinary.getStream();
ByteArrayDataSource imageDS = new ByteArrayDataSource(imageStream,"image/jpeg");
email.attach(imageDS,"Some Image","Some Description");