javamail将图像添加到html电子邮件中。怎么用?
我用javamail发送电子邮件没有问题(我使用EJB3.0),问题是当我试图通过使用多部分方法向html添加一些图像时。由于某种原因,我得到了一个FileNotFoundException。我不知道如何获取位于WEB-INF/resources/images的.png图像的路径 以下是我所做的:javamail将图像添加到html电子邮件中。怎么用?,java,jakarta-ee,ejb-3.0,jakarta-mail,java-ee-6,Java,Jakarta Ee,Ejb 3.0,Jakarta Mail,Java Ee 6,我用javamail发送电子邮件没有问题(我使用EJB3.0),问题是当我试图通过使用多部分方法向html添加一些图像时。由于某种原因,我得到了一个FileNotFoundException。我不知道如何获取位于WEB-INF/resources/images的.png图像的路径 以下是我所做的: Message message = new MimeMessage(mailSession); // From: is our service message.setF
Message message = new MimeMessage(mailSession);
// From: is our service
message.setFrom(new InternetAddress(from));
// To: destination given
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Uspijesna registracija");
// How to found at http://www.rgagnon.com/javadetails/java-0321.html
message.setContent(generateActivationLinkTemplate(), "text/html");
Date timeStamp = new Date();
message.setSentDate(timeStamp);
// Prepare a multipart HTML
Multipart multipart = new MimeMultipart("related");
// Prepare the HTML
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(generateActivationLinkTemplate(), "text/html");
multipart.addBodyPart(htmlPart);
// PREPARE THE IMAGE
BodyPart imgPart = new MimeBodyPart();
DataSource ds = new FileDataSource("logomailtemplate.png");
imgPart.setDataHandler(new DataHandler(ds));
imgPart.setHeader("Content-ID", "the-img-1");
multipart.addBodyPart(imgPart);
// Set the message content!
message.setContent(multipart);
Transport.send(message);
文本来自另一种正确工作的方法。我认为问题在于这一行代码:
DataSource ds=新文件数据源(“logomailtemplate.png”)
这是控制台所说的:
java.io.FileNotFoundException:logomailtemplate.png(系统找不到指定的文件)
我如何访问该图像?它位于WEB-INF/resources/images中
我被这一天卡住了,我真的很感激你的帮助:)
-----------------------------------更新-------------------------------
Message message = new MimeMessage(mailSession);
// From: is our service
message.setFrom(new InternetAddress(from));
// To: destination given
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Uspijesna registracija");
// How to found at http://www.rgagnon.com/javadetails/java-0321.html
message.setContent(generateActivationLinkTemplate(), "text/html");
Date timeStamp = new Date();
message.setSentDate(timeStamp);
// Prepare a multipart HTML
Multipart multipart = new MimeMultipart();
// Prepare the HTML
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(generateActivationLinkTemplate(), "text/html");
multipart.addBodyPart(htmlPart);
// PREPARE THE IMAGE
BodyPart imgPart = new MimeBodyPart();
String fileName = "WEB-INF/resources/images/logoemailtemplate.png";
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
stream = classLoader.getResourceAsStream(fileName);
DataSource ds = new ByteArrayDataSource(stream, "image/*");
imgPart.setDataHandler(new DataHandler(ds));
imgPart.setHeader("Content-ID", "the-img-1");
multipart.addBodyPart(imgPart);
// Set the message content!
message.setContent(multipart);
Transport.send(message);
String fileName = "/WEB-INF/resources/images/logoemailtemplate.png";
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
//This line dont work!
stream = classLoader.getResourceAsStream(fileName);
DataSource ds = new URLDataSource(classLoader.getResource(fileName));
------------------------------------------堆栈跟踪-----------------------------------
Message message = new MimeMessage(mailSession);
// From: is our service
message.setFrom(new InternetAddress(from));
// To: destination given
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Uspijesna registracija");
// How to found at http://www.rgagnon.com/javadetails/java-0321.html
message.setContent(generateActivationLinkTemplate(), "text/html");
Date timeStamp = new Date();
message.setSentDate(timeStamp);
// Prepare a multipart HTML
Multipart multipart = new MimeMultipart();
// Prepare the HTML
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(generateActivationLinkTemplate(), "text/html");
multipart.addBodyPart(htmlPart);
// PREPARE THE IMAGE
BodyPart imgPart = new MimeBodyPart();
String fileName = "WEB-INF/resources/images/logoemailtemplate.png";
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
stream = classLoader.getResourceAsStream(fileName);
DataSource ds = new ByteArrayDataSource(stream, "image/*");
imgPart.setDataHandler(new DataHandler(ds));
imgPart.setHeader("Content-ID", "the-img-1");
multipart.addBodyPart(imgPart);
// Set the message content!
message.setContent(multipart);
Transport.send(message);
String fileName = "/WEB-INF/resources/images/logoemailtemplate.png";
InputStream stream = null;
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
//This line dont work!
stream = classLoader.getResourceAsStream(fileName);
DataSource ds = new URLDataSource(classLoader.getResource(fileName));
警告:StandardWrapperValve[Faces Servlet]:PWC1406:Servlet.service()用于Servlet Faces Servlet引发异常
javax.faces.el.EvaluationException:javax.ejb.EJBException
严重:javax.ejb.EJBException
javax.faces.el.EvaluationException:javax.ejb.EJBException
位于javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
位于com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
在javax.faces.component.UICommand.broadcast(UICommand.java:315)
位于javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
位于javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
位于com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
位于com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
在com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)上
位于javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
位于org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
位于org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
位于org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
位于org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
位于com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
位于com.sun.enterprise.web.peSessionLockingsStandardPipeline.invoke(peSessionLockingsStandardPipeline.java:85)
位于org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
位于org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
位于org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
位于com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
在com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
位于com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
位于com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
位于com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
位于com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)上
在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)上
http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
位于com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
在com.sun.grizzly.SelectionKeyContextTask.call上(SelectionKeyContextTask.java:57)
位于com.sun.grizzly.ContextTask.run(ContextTask.java:69)
位于com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
位于com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
运行(Thread.java:662)
原因:javax.ejb.EJBException
位于com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5119)
在com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5017)
在com.sun.ejb.containers.BaseContainer.postInvokeTx上(BaseContainer.java:4805)
在com.sun.ejb.containers.BaseContainer.postInvoke上(BaseContainer.java:2004)
在com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1955)上
在com.sun.ejb.containers.ejblocationHandler.invoke(ejblocationHandler.java:198)
在com.sun.ejb.containers.ejblocationHandlerDelegate.invoke(ejblocationHandlerDelegate.java:84)
在$Proxy162.sendAccountActivationLinkToBuyer(未知来源)
位于managedbeans.RegistrationController.doRegisterBuyer(RegistrationController.java:64)
在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)
位于com.sun.el.parser.AstValue.invoke(AstValue.java:234)
位于com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
位于org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
位于org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:72)
在com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)上
位于javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 32多
原因:java.lang.NullPointerException
位于javax.mail.util.ByteArrayDataSource。(ByteArrayDataSource.java:83)
String fileName = "WEB-INF/resources/images/logomailtemplate.png";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
DataSource ds = new FileDataSource(new File(classLoader.getResource(fileName).toURI()));
//OR
DataSource ds = new URLDataSource(classLoader.getResource(fileName));