package com.juick.api; import com.juick.Message; import com.juick.server.component.MessageEvent; import com.juick.server.util.HttpBadRequestException; import com.juick.service.EmailService; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import javax.inject.Inject; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; import static com.juick.formatters.PlainTextFormatter.formatPost; import static com.juick.formatters.PlainTextFormatter.formatUrl; @Component public class EmailManager implements ApplicationListener { private static final Logger logger = LoggerFactory.getLogger(EmailManager.class); @Inject EmailService emailService; @Inject SubscriptionService subscriptionService; @Inject MessagesService messagesService; @Override public void onApplicationEvent(MessageEvent event) { Message msg = event.getMessage(); if (msg.getRid() == 0) { String subject = String.format("New message from %s", msg.getUser().getName()); subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg.getMid()) .forEach(user -> emailService.getEmails(user.getUid(), true) .forEach(email -> emailNotify(email, subject, msg))); } else { Message originalMessage = messagesService.getMessage(msg.getMid()); String subject = String.format("New reply to %s", originalMessage.getUser().getName()); subscriptionService.getUsersSubscribedToComments(msg.getMid(), msg.getUser().getUid()) .forEach(user -> emailService.getEmails(user.getUid(), true) .forEach(email -> emailNotify(email, subject, msg))); } } void emailNotify(String email, String subject, Message msg) { Properties prop = System.getProperties(); prop.put("mail.smtp.starttls.enable", "true"); Session session = Session.getDefaultInstance(prop); try { Transport transport = session.getTransport("smtp"); MimeMessage message = new MimeMessage(session) { protected void updateMessageID() throws MessagingException { setHeader("Message-ID", String.format("%d.%d@juick.com", msg.getMid(), msg.getRid())); if (msg.getRid() > 0) { if (msg.getReplyto() > 0) { Message replyto = messagesService.getReply(msg.getMid(), msg.getReplyto()); setHeader("In-Reply-To", String.format("%d.%d@juick.com", replyto.getMid(), replyto.getRid())); } else { Message original = messagesService.getMessage(msg.getMid()); setHeader("In-Reply-To", String.format("%d.%d@juick.com", original.getMid(), original.getRid())); } } } }; 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---\nYou are receiving this because you are subscribed to this user " + "or discussion. Configure notifications at https://juick.com/settings", formatPost(msg), formatUrl(msg))); message.saveChanges(); transport.connect(); transport.sendMessage(message, message.getAllRecipients()); } catch (MessagingException ex) { logger.error("mail exception", ex); } } }