1

我正在尝试按照此处讨论的说明发送带有内嵌图像附件的 HTML 电子邮件。在我的电子邮件 XHTML 文件中,我有以下几行来附加图像,然后在消息中显示它:

<m:message xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:a="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:s="http://jboss.com/products/seam/taglib"
    xmlns:m="http://jboss.com/products/seam/mail">

    <m:from name="#{emailDetails.senderName}"
        address="#{emailDetails.senderAddress}" />
    <m:to name="#{emailDetails.recipientName}"
        address="#{emailDetails.recipientAddress}" />
    <m:subject>
        <h:outputText value="#{emailDetails.subject}" />
    </m:subject>

    <m:body>
        <p>THIS IS A TEST</p>
    <m:attachment contentType="image/png"
        fileName="dwx_logo.png" value="#{imageData}"
        status="logoImage" disposition="inline" /> 
    <img src="cid:#{logoImage.contentId}" alt="logo header"
        border="no" style="margin: 0px; padding: 0px; display: block;" />
    </m:body>
</m:message>

但是,当我使用此代码呈现消息时:

@In
private Renderer renderer = null;

...

    String senderName = "test";
    String senderAddress = ...
    String recipientName = "test";
    String recipientAddress = ...
    String subject = test";
    String messageTemplate = "/WEB-INF/emailTemplates/test.xhtml";

    InputStream inStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("/logo_light.png");
    byte[] imageData = IOUtils.toByteArray(inStream);

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("imageData", imageData);

    EmailDetails details = new EmailDetails(senderName, senderAddress,
            recipientName, recipientAddress, subject);
    details.setMessageTemplate(messageTemplate);
    details.setEventParameters(params);

    Contexts.getEventContext().set("emailDetails", details);
    if (params != null) {
        Iterator<Map.Entry<String, Object>> it = params.entrySet()
                .iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            Contexts.getEventContext()
                    .set(entry.getKey(), entry.getValue());
        }
    }

    renderer.render(messageTemplate);

我得到以下异常......

请求处理期间的异常:由 javax.el.E​​LException 引起,消息为:“java.lang.NullPointerException” org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:339) org.jboss.el.util.ReflectionUtil。 invokeMethod(ReflectionUtil.java:280) org.jboss.el.parser.AstMethodSuffix.getValue(AstMethodSuffix.java:59) org.jboss.el.parser.AstMethodSuffix.invoke(AstMethodSuffix.java:65) org.jboss.el。 parser.AstValue.invoke(AstValue.java:96) org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) org.jboss.seam.core.Expressions$2.invoke(Expressions.java:221) org.jboss .seam.navigation.Pages.callAction(Pages.java:708) org.jboss.seam.navigation.Pages.preRender(Pages.java:331) org.jboss.seam.jsf.SeamPhaseListener.preRenderPage(SeamPhaseListener.java:560 ) org.jboss.seam.jsf.SeamPhaseListener。beforeRenderResponse(SeamPhaseListener.java:471) org.jboss.seam.jsf.SeamPhaseListener.beforeServletPhase(SeamPhaseListener.java:147) org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:117) com.sun.faces。生命周期.Phase.handleBeforePhase(Phase.java:214) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:96) com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) javax. faces.webapp.FacesServlet.service(FacesServlet.java:266)java:139) javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)java:139) javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)

由 java.lang.NullPointerException 引起,消息为:“” org.jboss.seam.mail.ui.UIBody.encodeChildren(UIBody.java:79) org.jboss.seam.ui.util.JSF.renderChild(JSF.java: 175) org.jboss.seam.ui.util.JSF.renderChildren(JSF.java:163) org.jboss.seam.mail.ui.UIMessage.encodeChildren(UIMessage.java:192) org.jboss.seam.ui。 util.JSF.renderChild(JSF.java:175) org.jboss.seam.ui.util.JSF.renderChildren(JSF.java:163) org.jboss.seam.ui.facelet.RendererRequest.renderFacelet(RendererRequest.java: 140) org.jboss.seam.ui.facelet.RendererRequest.run(RendererRequest.java:107) org.jboss.seam.ui.facelet.FaceletsRenderer.render(FaceletsRenderer.java:43) com.dezinewerks.market.web。 actions.test.TestAction.runTest(TestAction.java:986) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl。invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) org.jboss.seam.util.Reflections.invoke( Reflections.java:22)

我还尝试删除“disposition=inline”行,此时代码按预期工作。它只是不内联渲染图像,这违背了目的。知道为什么设置处置属性值会导致此异常吗?这是 Seam Mail 中的错误吗?或者我在我的代码中做错了什么?

提前感谢您的帮助!

4

1 回答 1

3

我自己解决了这个问题。事实证明,任何内联附件都需要在<m:body />标签之外定义。标准附件在邮件正文中工作得很好,但必须在正文之外声明内联附件。一旦我做出这个改变,一切都开始按预期工作。这在文档中并不清楚,但在 Seam 发行版中的邮件示例中显示。

于 2012-04-23T19:51:48.313 回答