From 4cfc36439285a36811d525341cde3785f539efdd Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 5 Jan 2018 12:21:56 +0300 Subject: email: multipart --- .../src/main/java/com/juick/api/EmailManager.java | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'juick-api/src/main') diff --git a/juick-api/src/main/java/com/juick/api/EmailManager.java b/juick-api/src/main/java/com/juick/api/EmailManager.java index a01eac78..2f4065d0 100644 --- a/juick-api/src/main/java/com/juick/api/EmailManager.java +++ b/juick-api/src/main/java/com/juick/api/EmailManager.java @@ -14,10 +14,13 @@ import org.springframework.stereotype.Component; import javax.inject.Inject; import javax.mail.MessagingException; +import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; import java.util.Properties; import static com.juick.formatters.PlainTextFormatter.formatPost; @@ -74,10 +77,22 @@ public class EmailManager implements ApplicationListener { message.setFrom(new InternetAddress("juick@juick.com")); message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email)); message.setSubject(subject); - message.setText(String.format("%s\n%s\n\n---\nYou are receiving this because you are subscribed to this user " + - "or discussion. Configure notifications at https://juick.com/settings?hash=%s", + String plainText = String.format("%s\n\n--\nYou are receiving this because you are subscribed to this user " + + ", discussion or tag. Reply to this email directly or view it on Juick: %s.", + StringUtils.isNotEmpty(msg.getText()) ? formatPost(msg) : "", formatUrl(msg)); + MimeBodyPart textBodyPart = new MimeBodyPart(); + textBodyPart.setContent(plainText, "text/plain"); + String htmlText = String.format("%s

--
You are receiving this because you are subscribed to this user " + + ", discussion or tag. Reply to this email directly or view it on Juick." + + "
Configure or disable notifications", StringUtils.isNotEmpty(msg.getText()) ? formatPost(msg) : "", formatUrl(msg), - userService.getHashByUID(userService.getUserByEmail(email).getUid()))); + userService.getHashByUID(userService.getUserByEmail(email).getUid())); + MimeBodyPart htmlBodyPart = new MimeBodyPart(); + htmlBodyPart.setContent(htmlText, "text/html"); + Multipart multipart = new MimeMultipart("alternative"); + multipart.addBodyPart(textBodyPart); + multipart.addBodyPart(htmlBodyPart); + message.setContent(multipart); message.setHeader("List-Unsubscribe", String.format("https://juick.com/settings?hash=%s", userService.getHashByUID(userService.getUserByEmail(email).getUid()))); message.saveChanges(); transport.connect(); -- cgit v1.2.3