Exemplo n.º 1
0
 static HtmlEmail removeRecipient(HtmlEmail email, String emailToRemove) throws EmailException {
   List<String> to = Utils.noGenericTypeToStringType(email.getToAddresses()),
       cc = Utils.noGenericTypeToStringType(email.getCcAddresses()),
       bcc = Utils.noGenericTypeToStringType(email.getBccAddresses());
   if (to.contains(emailToRemove)) {
     to.remove(emailToRemove);
     email.setTo(to);
   } else if (cc.contains(emailToRemove)) {
     cc.remove(emailToRemove);
     email.setCc(cc);
   } else if (bcc.contains(emailToRemove)) {
     bcc.remove(emailToRemove);
     email.setBcc(bcc);
   }
   return email;
 }
Exemplo n.º 2
0
 @SuppressWarnings("unchecked")
 static String[] composeReadableRecipientList(HtmlEmail email) {
   String[] RRL = {"To: ", "Cc: ", "Bcc: "};
   Iterator<String> toIT = email.getToAddresses().iterator(),
       ccIT = email.getCcAddresses().iterator(),
       bccIT = email.getBccAddresses().iterator();
   while (toIT.hasNext()) {
     RRL[0] += toIT.next() + ", ";
   }
   while (ccIT.hasNext()) {
     RRL[1] += ccIT.next() + ", ";
   }
   while (bccIT.hasNext()) {
     RRL[2] += bccIT.next() + ", ";
   }
   return RRL;
 }
Exemplo n.º 3
0
  public static void sendResetPwdMail(String appCode, ODocument user) throws Exception {
    final String errorString = "Cannot send mail to reset the password: "******" invalid user object");

    // initialization
    String siteUrl = Application.NETWORK_HTTP_URL.getValueAsString();
    int sitePort = Application.NETWORK_HTTP_PORT.getValueAsInteger();
    if (StringUtils.isEmpty(siteUrl))
      throw new PasswordRecoveryException(errorString + " invalid site url (is empty)");

    String textEmail = PasswordRecovery.EMAIL_TEMPLATE_TEXT.getValueAsString();
    String htmlEmail = PasswordRecovery.EMAIL_TEMPLATE_HTML.getValueAsString();
    if (StringUtils.isEmpty(htmlEmail)) htmlEmail = textEmail;
    if (StringUtils.isEmpty(htmlEmail))
      throw new PasswordRecoveryException(errorString + " text to send is not configured");

    boolean useSSL = PasswordRecovery.NETWORK_SMTP_SSL.getValueAsBoolean();
    boolean useTLS = PasswordRecovery.NETWORK_SMTP_TLS.getValueAsBoolean();
    String smtpHost = PasswordRecovery.NETWORK_SMTP_HOST.getValueAsString();
    int smtpPort = PasswordRecovery.NETWORK_SMTP_PORT.getValueAsInteger();
    if (StringUtils.isEmpty(smtpHost))
      throw new PasswordRecoveryException(errorString + " SMTP host is not configured");

    String username_smtp = null;
    String password_smtp = null;
    if (PasswordRecovery.NETWORK_SMTP_AUTHENTICATION.getValueAsBoolean()) {
      username_smtp = PasswordRecovery.NETWORK_SMTP_USER.getValueAsString();
      password_smtp = PasswordRecovery.NETWORK_SMTP_PASSWORD.getValueAsString();
      if (StringUtils.isEmpty(username_smtp))
        throw new PasswordRecoveryException(errorString + " SMTP username is not configured");
    }
    String emailFrom = PasswordRecovery.EMAIL_FROM.getValueAsString();
    String emailSubject = PasswordRecovery.EMAIL_SUBJECT.getValueAsString();
    if (StringUtils.isEmpty(emailFrom))
      throw new PasswordRecoveryException(errorString + " sender email is not configured");

    try {
      String userEmail =
          ((ODocument) user.field(UserDao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER))
              .field("email")
              .toString();

      String username = (String) ((ODocument) user.field("user")).field("name");

      // Random
      String sRandom = appCode + "%%%%" + username + "%%%%" + UUID.randomUUID();
      String sBase64Random = new String(Base64.encodeBase64(sRandom.getBytes()));

      // Save on DB
      ResetPwdDao.getInstance().create(new Date(), sBase64Random, user);

      // Send mail
      HtmlEmail email = null;

      URL resetUrl =
          new URL(
              Application.NETWORK_HTTP_SSL.getValueAsBoolean() ? "https" : "http",
              siteUrl,
              sitePort,
              "/user/password/reset/" + sBase64Random);

      // HTML Email Text
      ST htmlMailTemplate = new ST(htmlEmail, '$', '$');
      htmlMailTemplate.add("link", resetUrl);
      htmlMailTemplate.add("user_name", username);

      // Plain text Email Text
      ST textMailTemplate = new ST(textEmail, '$', '$');
      textMailTemplate.add("link", resetUrl);
      textMailTemplate.add("user_name", username);

      email = new HtmlEmail();

      email.setHtmlMsg(htmlMailTemplate.render());
      email.setTextMsg(textMailTemplate.render());

      // Email Configuration
      email.setSSL(useSSL);
      email.setSSLOnConnect(useSSL);
      email.setTLS(useTLS);
      email.setStartTLSEnabled(useTLS);
      email.setStartTLSRequired(useTLS);
      email.setSSLCheckServerIdentity(false);
      email.setSslSmtpPort(String.valueOf(smtpPort));
      email.setHostName(smtpHost);
      email.setSmtpPort(smtpPort);

      if (PasswordRecovery.NETWORK_SMTP_AUTHENTICATION.getValueAsBoolean()) {
        email.setAuthenticator(new DefaultAuthenticator(username_smtp, password_smtp));
      }
      email.setFrom(emailFrom);
      email.addTo(userEmail);

      email.setSubject(emailSubject);

      if (Logger.isDebugEnabled()) {
        StringBuilder logEmail =
            new StringBuilder()
                .append("HostName: ")
                .append(email.getHostName())
                .append("\n")
                .append("SmtpPort: ")
                .append(email.getSmtpPort())
                .append("\n")
                .append("SslSmtpPort: ")
                .append(email.getSslSmtpPort())
                .append("\n")
                .append("SSL: ")
                .append(email.isSSL())
                .append("\n")
                .append("TLS: ")
                .append(email.isTLS())
                .append("\n")
                .append("SSLCheckServerIdentity: ")
                .append(email.isSSLCheckServerIdentity())
                .append("\n")
                .append("SSLOnConnect: ")
                .append(email.isSSLOnConnect())
                .append("\n")
                .append("StartTLSEnabled: ")
                .append(email.isStartTLSEnabled())
                .append("\n")
                .append("StartTLSRequired: ")
                .append(email.isStartTLSRequired())
                .append("\n")
                .append("SubType: ")
                .append(email.getSubType())
                .append("\n")
                .append("SocketConnectionTimeout: ")
                .append(email.getSocketConnectionTimeout())
                .append("\n")
                .append("SocketTimeout: ")
                .append(email.getSocketTimeout())
                .append("\n")
                .append("FromAddress: ")
                .append(email.getFromAddress())
                .append("\n")
                .append("ReplyTo: ")
                .append(email.getReplyToAddresses())
                .append("\n")
                .append("BCC: ")
                .append(email.getBccAddresses())
                .append("\n")
                .append("CC: ")
                .append(email.getCcAddresses())
                .append("\n")
                .append("Subject: ")
                .append(email.getSubject())
                .append("\n")
                .append("Message: ")
                .append(email.toString())
                .append("\n")
                .append("SentDate: ")
                .append(email.getSentDate())
                .append("\n");
        Logger.debug("Password Recovery is ready to send: \n" + logEmail.toString());
      }
      email.send();

    } catch (EmailException authEx) {
      Logger.error("ERROR SENDING MAIL:" + ExceptionUtils.getStackTrace(authEx));
      throw new PasswordRecoveryException(
          errorString
              + " Could not reach the mail server. Please contact the server administrator");
    } catch (Exception e) {
      Logger.error("ERROR SENDING MAIL:" + ExceptionUtils.getStackTrace(e));
      throw new Exception(errorString, e);
    }
  }