【发布时间】:2017-05-03 06:13:30
【问题描述】:
我使用 spring mvc 并使用 javax.mail 发送电子邮件。我想在邮件正文中附加一张图片。
项目结构是
我想从路径 /WebContent/resources/imagecalcp 附加一张图片
在 servlet.xml 我有以下行
<mvc:resources mapping="/resources/**" location="/resources/" />
代码是:
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
msg.setSubject(subject,"UTF-8");
MimeMultipart multipart = new MimeMultipart("related");
MimeBodyPart messageBodyPart = new MimeBodyPart();
htmlFinal = ConversionTools.convertTypeHTML(body);
messageBodyPart.setContent(htmlFinal, "text/html");
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
messageBodyPart.attachFile(loader.getResource("/EcalpAdmin/resources/imgecalcp/fondoencabezado_marron.png").getFile());
messageBodyPart.setHeader("Content-ID", "<image>");
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport.send(msg);
我得到了异常
MailSender en exception class path resource [EcalpAdmin/resources/imgecalcp/fondoencabezado_marron.png] cannot be resolved to URL because it does not exist
java.io.FileNotFoundException: class path resource [EcalpAdmin/resources/imgecalcp/fondoencabezado_marron.png] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:177)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
at eusurvey.tools.MailSender.run(MailSender.java:229)
at eusurvey.tools.MailSender$$FastClassByCGLIB$$280bfa43.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:701)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:634)
at eusurvey.tools.MailSender$$EnhancerByCGLIB$$f5beb86d.run(<generated>)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
如何附加资源文件夹中的图片?
【问题讨论】:
标签: spring image spring-mvc jakarta-mail attachment