private Session getSession() { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", EMAIL_CONFIG.getHost()); props.put("mail.smtp.port", EMAIL_CONFIG.getPort()); return Session.getDefaultInstance( props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( EMAIL_CONFIG.getUsername(), EMAIL_CONFIG.getPassword()); } }); }
@Override public void alert(RaptureAlertEvent event) { String from = EMAIL_CONFIG.getFrom(); String to = emailTemplate.getEmailTo(); String subject = event.parseTemplate(emailTemplate.getSubject()); String msgBody = event.parseTemplate(emailTemplate.getMsgBody()); try { Message message = new MimeMessage(getSession()); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setText(msgBody); Transport.send(message); } catch (MessagingException e) { logger.error("Failed to send email", e); } }