Seam Mail内联图像问题

时间:2012-04-16 01:05:04

标签: java email exception jboss seam

我正在尝试按照discussed here的方向发送带有内嵌图片附件的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.lifecycle.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.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(原生方法)   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中的一个错误吗?或者我在代码中做错了什么?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

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