Example #1
0
  public static boolean sendEmail(String from, String to, String subject, String message) {
    // create a new authenticator for the SMTP server
    EmailUtilsAuthenticator authenticator = new EmailUtilsAuthenticator();

    // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.setProperty(MAIL_SMTP_HOST, Configuration.getSmtpHost());
    // setup the authentication
    properties.setProperty(MAIL_SMTP_AUTH, "true");

    // Get the Session object with the authenticator.
    Session session = Session.getInstance(properties, authenticator);

    try {
      MimeMessage mime = new MimeMessage(session);
      mime.setFrom(new InternetAddress(from));
      mime.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

      mime.setSubject(subject);
      mime.setText(message);

      // Send it
      Transport.send(mime);

      return true;

    } catch (MessagingException mex) {
      logger.error("sendEmail(String, String, String, String) - exception ", mex);
      return false;
    }
  }
Example #2
0
  /**
   * Daily mail company digest, get all the users subscribed and send them emails abou the
   * comapnies.
   */
  @Schedule(hour = "0")
  public void dailyMailCompanyDigest() {

    if (logger.isDebugEnabled()) {
      logger.debug("dailyMailCompanyDigest - start");
    }

    // get all users with associated companies (fetch get all the lazy connections)
    Query query =
        entityManager.createQuery("SELECT u FROM User u join fetch u.subscribedCompanies");

    @SuppressWarnings("unchecked")
    List<User> userList = query.getResultList();

    if (true == userList.isEmpty()) {
      logger.info("dailyMailCompanyDigest - no users to process");
    } else {
      // set the email subject for all the users
      String subject =
          String.format(
              "Daily Company Digest %1$te/%1$tm/%1$tY at %1$tH:%1$tM:%1$tS%n", new Date());
      for (User user : userList) {
        if (StringUtils.IsNullOrWhiteSpace(user.getEmail())) {
          logger.info(
              String.format("dailyMailCompanyDigest user %s has no email", user.getUsername()));
        } else {
          String messageCompanies = buildSubjectCompanies(user.getSubscribedCompanies());
          boolean status =
              EmailUtils.sendEmail(
                  Configuration.getEmailFromAddress(), user.getEmail(), subject, messageCompanies);

          if (!status) {
            logger.info(
                String.format(
                    "dailyMailCompanyDigest error sending email user %s", user.getUsername()));
          }
        }
      }
    }

    if (logger.isDebugEnabled()) {
      logger.debug("dailyMailCompanyDigest - end");
    }
  }