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

spring mvc javax.mail如何附加图像正文邮件,spring,image,spring-mvc,jakarta-mail,attachment,Spring,Image,Spring Mvc,Jakarta Mail,Attachment,我使用SpringMVC并使用javax.mail发送电子邮件。我想在邮件正文中附加一个图像 项目结构如下: 我想从path/WebContent/resources/imagecalcp附加一个图像 在servlet.xml中,我有以下几行代码 <mvc:resources mapping="/resources/**" location="/resources/" /> 代码是: MimeMessage msg = new MimeMessage(session); ms

我使用SpringMVC并使用javax.mail发送电子邮件。我想在邮件正文中附加一个图像

项目结构如下:

我想从path/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);
MimeMessage msg=新MimeMessage(会话);
msg.setFrom(新因特网地址(from));
msg.addRecipient(Message.RecipientType.TO,
新互联网地址(至);;
消息集主题(主题“UTF-8”);
MimMultipart multipart=新的MimMultipart(“相关”);
MimeBodyPart messageBodyPart=新的MimeBodyPart();
htmlFinal=ConversionTools.convertTypeHTML(正文);
setContent(htmlFinal,“text/html”);
multipart.addBodyPart(messageBodyPart);
messageBodyPart=新的MimeBodyPart();
messageBodyPart.attachFile(loader.getResource(“/ecaladmin/resources/imgecalcp/fondoencabezado_marron.png”).getFile();
messageBodyPart.setHeader(“内容ID”,即“”);
multipart.addBodyPart(messageBodyPart);
msg.setContent(多部分);
发送(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)
mailssender en异常类路径资源[ecaladmin/resources/imgecalcp/fondoencabezado_marron.png]无法解析为URL,因为它不存在
java.io.FileNotFoundException:类路径资源[ecalAdmin/resources/imgecalcp/fondoencabezado_marron.png]无法解析为URL,因为它不存在
位于org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:177)
位于org.springframework.core.io.AbstractFileResolvingerSource.getFile(AbstractFileResolvingerSource.java:48)
运行(mailssender.java:229)
在eusurvey.tools.mailssender$$FastClassByCGLIB$$280bfa43.invoke()上
位于org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
位于org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:701)
在org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:150)上
位于org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
位于org.springframework.transaction.interceptor.TransactionSpectSupport.invokeWithinTransaction(TransactionSpectSupport.java:260)
位于org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
位于org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:172)
位于org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:634)
在eusurvey.tools.mailssender$$enhancerbyglib$$f5beb86d.run()上
位于java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
运行(Thread.java:662)
如何从资源文件夹附加图像

String rootDirectory = request.getSession().getServletContext().getRealPath("/");
System.out.println(rootDirectory);
messageBodyPart.attachFile(new File(rootDirectory+"\\resources\\productimages\\"+fondoencabezado_marron + ".png"));
用此替换attachfile方法。您可以阅读更多内容