ozimov/spring-boot-email-tools

Fail to send templated emails without attachments

maciejlach opened this issue · 3 comments

Latest spring-boot-email-tools release (0.3.7) fails to send template base emails, without attachments:

java.lang.NullPointerException
	at it.ozimov.springboot.templating.mail.service.EmailServiceImpl.send(EmailServiceImpl.java:107)

Code to reproduce:

@ActiveProfiles("service-test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ServicesTestsConfiguration.class)
@TestPropertySource(locations = "classpath:application-test.properties")
public class MailTest {

    @Autowired
    public EmailService emailService;

    @Value("${mail.from}")
    private String emailFrom;

    @Value("${mail.to}")
    private String emailTo;

    private final Charset charset = Charset.forName("UTF-8");

    @Test
    public void testMail() throws AddressException, CannotSendEmailException {
         final Email email = EmailImpl.builder().from(new InternetAddress(emailFrom)).to(Lists.newArrayList(new InternetAddress(emailTo)))
                .subject("subject").body("").encoding(charset).build();

        final Map<String, Object> modelObject = Maps.newHashMap();
        modelObject.put("instance", "FooBar");

        emailService.send(email, "email_test.ftl", modelObject);
    }
}

Workaround - use empty list of attachments:

final Email email = EmailImpl.builder().from(new InternetAddress(emailFrom)).to(Lists.newArrayList(new InternetAddress(emailTo)))
                .subject("subject").body("").encoding(charset).attachments(Lists.newArrayList()).build();

@maciejlach Hi! Thanks for reporting the issue. I'll try to reproduce it and if the bug will be confirmed I'll prepare a fix.

@maciejlach Confirmed. And the issue started from the convenience methods made in the unit tests.

public static Email getSimpleMail(InternetAddress from, EmailAttachmentImpl ... emailAttachments) throws UnsupportedEncodingException {

which had

 if(nonNull(emailAttachments)){
            builder.attachments(Arrays.asList(emailAttachments));
}

I improved the integration tests and fixed the issue by having a default method in the Email interface with an empty immutable collection.

I will release the fix soon.

@maciejlach release 0.3.8 made.

Thanks again for highlighting the issue.