javamail将图像添加到html电子邮件中.怎么样?
作者:互联网
我使用javamail发送电子邮件没有问题(我使用EJB 3.0),问题是当我尝试通过使用多部分方法将一些图像添加到html时.由于某种原因,我得到了FileNotFoundException.我不知道如何获取位于WEB-INF / resources / images的.png图像的路径.
这是我所做的:
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 = new FileDataSource(“logomailtemplate.png”);
这就是控制台所说的:
java.io.FileNotFoundException: logomailtemplate.png (The system cannot find the file specified)
我如何访问该图像?它位于WEB-INF /资源/图像
我因这种不适而陷入困境,真的很感激手:)
———————————–更新————– —————–
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);
– – – – – – – – – – – – – – – – – – – – – 堆栈跟踪 – – – —————————–
WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.el.EvaluationException: javax.ejb.EJBException
…………….SEVERE: javax.ejb.EJBException
javax.faces.el.EvaluationException: javax.ejb.EJBException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:662)
Caused by: javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5119)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5017)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4805)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2004)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1955)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:198)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:84)
at $Proxy162.sendAccountActivationLinkToBuyer(Unknown Source)
at managedbeans.RegistrationController.doRegisterBuyer(RegistrationController.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:72)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
… 32 moreCaused by: java.lang.NullPointerException
at javax.mail.util.ByteArrayDataSource.(ByteArrayDataSource.java:83)
at ejbs.EmailServiceEJB.sendAccountActivationLinkToBuyer(EmailServiceEJB.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1056)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1128)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5292)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:615)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:567)
at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:858)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:567)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:157)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:139)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:858)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:367)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5264)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5252)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:190)
… 45 more
———————————–更新2 ————- —————————-
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));
//DataSource ds = new FileDataSource(new File(classLoader
// .getResource(fileName).toURI())); Also dont work. Also i tried both / and without /
解决方法:
实现此目的的一种方法可以是:
DataSource ds = new ByteArrayDataSource(getServletContext().getResourceAsStream("WEB-INF/resources/images/logomailtemplate.png") ,mimeType);
从HttpServlet接收到getServletContext().getResourceAsStream()并返回InputStream.
更好地做到这一点
String fileName = "/WEB-INF/resources/images/logomailtemplate.png";
InputStream stream = getServletContext().getResourceAsStream(fileName); //or null if you can't obtain a ServletContext
if (stream == null) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
}
stream = classLoader.getResourceAsStream(fileName);
}
DataSource ds = new ByteArrayDataSource(stream, "image/*");
更新资料
或者
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));
确保您的文件名以“ /”开头.
标签:ejb-3-0,java-ee,javamail,java-ee-6,java 来源: https://codeday.me/bug/20191023/1913052.html