使用html外部模板发送带有java的html电子邮件?

时间:2015-12-07 19:35:36

标签: java html email cq5 aem

我尝试发送带有参数和附件的HTML电子邮件。 我现在拥有的是这段代码:

<%@include file="/libs/fd/af/components/guidesglobal.jsp" %>
<%@page import="com.day.cq.wcm.foundation.forms.FormsHelper,
                org.apache.sling.api.resource.ResourceUtil,
                org.apache.sling.api.resource.ValueMap,
                org.apache.sling.api.request.RequestParameter,
                com.day.cq.mailer.MessageGatewayService,
                com.day.cq.mailer.MessageGateway,
                org.apache.commons.mail.Email,
                org.apache.fulcrum.template.TemplateHtmlEmail,
                org.apache.commons.mail.*" %>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %>
<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0"
%>
<cq:defineObjects/>
<sling:defineObjects/>
<%
    String storeContent = "/libs/fd/af/components/guidesubmittype/store";
    FormsHelper.runAction(storeContent, "post", resource, slingRequest, slingResponse);
    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);
      }

    String htmlEmailTemplate = props.get("templatePath");

    //========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>Company Name: " + slingRequest.getParameter("company-name") + "</p>";
    emailTextToSend += "<p>Message: " + slingRequest.getParameter("address") + "</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);
%>

使用此代码,我可以发送电子邮件,但我想修改代码以使用html模板文件的路径(路径位于变量htmlEmailTemplate上。

这是我的第一个问题,如何更改该代码。 我的第二个问题是,如果在该模板中我可以有这样的东西:

<span>${company-name}</span> 

其中company-name是我想在模板上使用的字段之一。 这可能吗?

2 个答案:

答案 0 :(得分:1)

看一下com.day.cq.commons.mail.MailTemplate api 如果您的模板位于JCR存储库中,则可以使用以下内容对其进行实例化:

String template = values.get(TEMPLATE_PROPERTY, String.class);
Resource templateRsrc = request.getResourceResolver().getResource(template);
final MailTemplate mailTemplate = MailTemplate.create(templateRsrc.getPath(),
           templateRsrc.getResourceResolver().adaptTo(Session.class));
final HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(properties), HtmlEmail.class);

其中,属性只是Key的HashMap:模板自身属性的值。

由于MailTemplate返回一个HtmlEmail对象,您仍然可以设置您在自己的代码中设置的所有设置。

答案 1 :(得分:-1)

以下链接可能有所帮助:

Link to send mail with attachment