spring mvc javax.mail如何附加图片正文邮件

时间:2017-05-03 06:13:30

标签: spring image spring-mvc javamail attachment

我使用spring mvc并使用javax.mail发送电子邮件。我想在邮件正文中附上一张图片。

项目结构是

enter image description here

我想附加路径/ 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)

如何附加资源文件夹中的图像?

1 个答案:

答案 0 :(得分:0)

String rootDirectory = request.getSession().getServletContext().getRealPath("/");
System.out.println(rootDirectory);
messageBodyPart.attachFile(new File(rootDirectory+"\\resources\\productimages\\"+fondoencabezado_marron + ".png"));

用this替换你的attachfile方法。你可以阅读更多here