从Docker容器中的Springboot Resources读取文件
我正在尝试在电子邮件正文中发送带有内联图像的电子邮件。当我从Eclipse本地运行它时,该代码正常工作。但是,当它被停靠并部署到kubernetes群集时,它无法读取PNG文件。
我会遇到错误“ java.io.filenotfoundexception:类路径资源[external_files/email_template.png]无法解决到绝对文件路径,因为它不属于文件系统:jar:jar:file:/app.jar!/boot!/boot! -inf/class!/external_files/email_template.png“ 项目结构屏幕截图
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.setSubject(AppConstants.EMAIL_SUBJECT);
MimeMultipart multipart = new MimeMultipart(AppConstants.RELATED);
MimeBodyPart messageBodyPart = new MimeBodyPart();
String html = AppConstants.EMAIL_HTML_PART_1 + recepientName +
AppConstants.EMAIL_HTML_PART_2
+ fromName + AppConstants.EMAIL_HTML_PART_3 + appProperties.getEmailOnboardPage()
+ AppConstants.EMAIL_HTML_PART_4 + appProperties.getEmailHelpPage()
+ AppConstants.EMAIL_HTML_PART_5;
messageBodyPart.setContent(html, AppConstants.TEXT_HTML);
multipart.addBodyPart(messageBodyPart);
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
DataSource fds = new FileDataSource(ResourceUtils.getFile(src/main/resources/External_Files/email_template.png));
messageBodyPart2.setDataHandler(new DataHandler(fds));
messageBodyPart2.setHeader(AppConstants.CONTENT_ID, AppConstants.IMAGE_HEADER);
multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
message.setHeader(AppConstants.X_PRIORITY, AppConstants.ONE);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
log.info("executing Transport.send");
Transport.send(message);
I am trying to send email with inline image in the email body. The code is working fine when I am running it locally from eclipse. But when it's getting dockerized and deployed to Kubernetes cluster it's unable to read the png file.
I am getting the error "java.io.FileNotFoundException: class path resource [External_Files/email_template.png] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/BOOT-INF/classes!/External_Files/email_template.png"
Project structure screen shot
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.setSubject(AppConstants.EMAIL_SUBJECT);
MimeMultipart multipart = new MimeMultipart(AppConstants.RELATED);
MimeBodyPart messageBodyPart = new MimeBodyPart();
String html = AppConstants.EMAIL_HTML_PART_1 + recepientName +
AppConstants.EMAIL_HTML_PART_2
+ fromName + AppConstants.EMAIL_HTML_PART_3 + appProperties.getEmailOnboardPage()
+ AppConstants.EMAIL_HTML_PART_4 + appProperties.getEmailHelpPage()
+ AppConstants.EMAIL_HTML_PART_5;
messageBodyPart.setContent(html, AppConstants.TEXT_HTML);
multipart.addBodyPart(messageBodyPart);
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
DataSource fds = new FileDataSource(ResourceUtils.getFile(src/main/resources/External_Files/email_template.png));
messageBodyPart2.setDataHandler(new DataHandler(fds));
messageBodyPart2.setHeader(AppConstants.CONTENT_ID, AppConstants.IMAGE_HEADER);
multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
message.setHeader(AppConstants.X_PRIORITY, AppConstants.ONE);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
log.info("executing Transport.send");
Transport.send(message);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Indranil Halder我知道回答已经晚了,但是对于有这个问题的其他人来说,发现此问题很有用,
您可以使用Resoursceloader在Spring Jar文件中找到classpath
@Indranil Halder I know it's late to answer but for others who have this problem finding this issue would be useful
you can use ResourceLoader to find classpath in spring jar file