I’m trying to send an HTML e-mail with inline image attachments per the directions discussed here. In my e-mail XHTML file, I have the following lines to attach the image and then display it within the message:
<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>
However, when I render the message with this code:
@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);
I get the following exception…
Exception during request processing: Caused by javax.el.ELException
with message: “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)Caused by java.lang.NullPointerException with message: “”
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)
I have also tried removing the “disposition=inline” line, at which point the code works as expected. It just doesn’t render the image inline, which defeats the purpose. Any idea why setting the disposition attribute value causes this exception? Is this a bug in Seam Mail? Or am I doing something wrong in my code?
Thanks in advance for any help!
I have solved this myself. It turns out that any inline attachments need to be defined outside of the
<m:body />tag. Standard attachments work just fine within the message body, but inline attachments must be declared outside of it. Once I made this change, everything began to work as expected. This was not clear in the documentation, but was shown in the mail example within the Seam distribution.