ozimov / spring-boot-email-tools

A set of services and tools for sending emails in a Spring Boot 1.5.x application using a Template Engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Strange behavior regarding attachment in DefaultEmailService using templates

fromi opened this issue · comments

When you send a message using DefaultEmailService.send(email, template, modelObject, inlinePictures), attachments are included twice in the message:

  • The first time through method "DefaultEmailService.toMimeMessage", using Spring MimeMessageHelper.
  • The second time at line 120, this time not using Spring MimeMessageHelper. This code is probably wrong, as the comment line 121 relates to image parts, not attachments.

Oddly, this does not cause the attachments to be added twice in the final email, but because MimeMessageHelper is not called the second time, MimeUtility.encodeText is not applied on attachment filename, which results in attachment filename to be missing in my case.

Workaround: call MimeUtility.encodeText myself:
DefaultEmailAttachment.builder().attachmentName(MimeUtility.encodeText(filename)))

I guess some refactoring related to inlinePictures and attachments would help :)

Hi @fromi
Thank you for reporting.
Please, can you post an example that reproduces the issue?
Which version are you using?

Best
R

Hi, I use latest released version, 0.6.2 (I included it today on my project).
My example is quite complexe, as I said I use a Thymeleaf template and attachments.
Here is the name of the attachment that produces the bug: "Conditions_Générales_Service.pdf". The accents might cause the bug, Instead of having the attachment name in the email I received, there is no name.

Best regards,
Romain

Got it.

I will try as soon as I will find some time. For now you can test your guess by renaming the attachment without attachments.

@fromi I reproduced the case in one of my examples. The branch is https://github.com/ozimov/spring-boot-email-tools/tree/test/issue-59-attachment-with-accented-chars
If you change the TO field and the username and password fields in the application.yml file, you will receive an email like that:
image

So, I don't see issues with the name of the attachments that you mentioned. If you don't want to share the code you are using I'm pretty sure I couldn't help. Guessing is not really one of my top skills, I prefer to debug.

I found the root cause of the bug, I was very subtle.

I debug my projet with spring-boot-maven-plugin. This plugin launches my Spring application indirectly. It does not add "-Dfile.encoding=UTF8" by default. Therefore, the value was "Cp1252" for me. Solved it by adding the argument in plugin configuration:
<jvmArguments>-Dfile.encoding=UTF8</jvmArguments>

My code before including your library was including the attachment using Spring MimeMessageHelper.addAttachment.
With your code, MimeMessageHelper is not used in class DefaultEmailService. The thing is, it is used in class EmailToMimeMessage, but using a template overrides this process, which is misleading.

I close the bug as the main problem was my configuration, but I advise you to refactor DefaultEmailService in order not to build attachments twice in the template method, it is not very efficient ;)
Also, I think you can use MimeMessageHelper for attachments in DefaultEmailService, I would simplify the code.

Anyway, it is a great library, thanks for building it!
Best regards

Glad to hear that you solved.

Anyway, this is an open source repository. You can contribute by opening a pull request with the proposed changes. They would be welcome.

Best
R

I'll submit a pull request to encode attachment names. Regarding the refactoring, I think it would require some API changes.