示例#1
0
 public static void email(
     CallingContext context, String templateName, Map<String, ? extends Object> templateValues) {
   final SMTPConfig config = getSMTPConfig();
   Session session = getSession(config);
   EmailTemplate template = getEmailTemplate(context, templateName);
   try {
     // Instantiate a new MimeMessage and fill it with the required information.
     Message msg = new MimeMessage(session);
     msg.setFrom(new InternetAddress(config.getFrom()));
     String to = renderTemplate(template.getEmailTo(), templateValues);
     if (StringUtils.isBlank(to)) {
       throw RaptureExceptionFactory.create("No emailTo field");
     }
     InternetAddress[] address = {new InternetAddress(to)};
     msg.setRecipients(Message.RecipientType.TO, address);
     msg.setSubject(renderTemplate(template.getSubject(), templateValues));
     msg.setSentDate(new Date());
     msg.setContent(
         renderTemplate(template.getMsgBody(), templateValues), "text/html; charset=utf-8");
     // Hand the message to the default transport service for delivery.
     Transport.send(msg);
   } catch (MessagingException e) {
     log.error("Failed to send email", e);
   }
 }
示例#2
0
 public static Session getSession(final SMTPConfig config) {
   // Create some properties and get the default Session.
   Properties props = System.getProperties();
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.starttls.enable", "true");
   props.put("mail.smtp.starttls.required", "true");
   props.put("mail.smtp.host", config.getHost());
   props.put("mail.smtp.port", config.getPort());
   Session session =
       Session.getInstance(
           props,
           new javax.mail.Authenticator() {
             protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(config.getUsername(), config.getPassword());
             }
           });
   return session;
 }