Skip to content

Commit d2c6593

Browse files
author
lorenzo
committed
bugfix: HTML email did not resolve inline resources (e.g images) correctly.
1 parent bcddcbb commit d2c6593

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

releases.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"version": "1.0.4",
44
"date": "//TODO",
5-
"release-notes": "<ul><li>bugfix: SMTP session builder did not work correctly for SSL connections.</li></ul>"
5+
"release-notes": "<ul><li>bugfix: SMTP session builder did not work correctly for SSL connections.</li><li>bugfix: HTML email did not resolve inline images correctly.</li></ul>"
66
},
77
{
88
"version": "1.0.2",

src/main/java/com/reedelk/mail/internal/smtp/type/MailWithHtml.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
import com.reedelk.runtime.api.message.Message;
77
import com.reedelk.runtime.api.message.content.MimeType;
88
import com.reedelk.runtime.api.message.content.Pair;
9+
import org.apache.commons.mail.DataSourceResolver;
910
import org.apache.commons.mail.EmailException;
1011
import org.apache.commons.mail.ImageHtmlEmail;
1112

13+
import javax.activation.DataSource;
14+
import javax.activation.URLDataSource;
15+
import java.io.IOException;
16+
import java.net.URL;
17+
1218
public class MailWithHtml extends AbstractMailType {
1319

1420
public MailWithHtml(SMTPMailSend component, ConverterService converterService) {
@@ -27,7 +33,22 @@ public MailTypeStrategyResult create(FlowContext context, Message message) throw
2733
Pair<String, String> charsetAndBody = buildCharsetAndMailBody(context, message);
2834
email.setCharset(charsetAndBody.left());
2935
email.setHtmlMsg(charsetAndBody.right());
36+
email.setDataSourceResolver(new AbsoluteURLDataSourceResolver()); // resolve image sources inline.
3037

3138
return MailTypeStrategyResult.create(email, charsetAndBody.right(), MimeType.TEXT_HTML);
3239
}
40+
41+
// Resolves img src URLs when Email contains image URLs
42+
private static class AbsoluteURLDataSourceResolver implements DataSourceResolver {
43+
44+
@Override
45+
public DataSource resolve(String resourceLocation) throws IOException {
46+
return new URLDataSource(new URL(resourceLocation));
47+
}
48+
49+
@Override
50+
public DataSource resolve(String resourceLocation, boolean isLenient) throws IOException {
51+
return new URLDataSource(new URL(resourceLocation));
52+
}
53+
}
3354
}

0 commit comments

Comments
 (0)