【问题标题】:spring mvc javax.mail how to attach a image body mailspring mvc javax.mail如何附加图像正文邮件
【发布时间】: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


    【解决方案1】:
    String rootDirectory = request.getSession().getServletContext().getRealPath("/");
    System.out.println(rootDirectory);
    messageBodyPart.attachFile(new File(rootDirectory+"\\resources\\productimages\\"+fondoencabezado_marron + ".png"));
    

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

    【讨论】:

      猜你喜欢
      • 2017-10-25
      • 2018-04-15
      • 2011-01-19
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多