コード例 #1
0
  public static void sendEmailWithException(
      String address, String subject, String content, Map<String, String> headers)
      throws MailNotSentException {
    try {
      String method =
          ParameterService.getParameterValue(RapidMiner.PROPERTY_RAPIDMINER_TOOLS_MAIL_METHOD);
      int methodIndex = -1;
      if (method != null) {
        try {
          methodIndex = Integer.parseInt(method);
        } catch (NumberFormatException e) {
          methodIndex = -1;
          for (int i = 0; i < RapidMiner.PROPERTY_RAPIDMINER_TOOLS_MAIL_METHOD_VALUES.length; i++) {
            if (RapidMiner.PROPERTY_RAPIDMINER_TOOLS_MAIL_METHOD_VALUES[i].equals(method)) {
              methodIndex = i;
              break;
            }
          }
        }
      }
      if (methodIndex == -1) {
        methodIndex = RapidMiner.PROPERTY_RAPIDMINER_TOOLS_MAIL_METHOD_SMTP;
      }

      MailSender mailSender = null;
      switch (methodIndex) {
        case RapidMiner.PROPERTY_RAPIDMINER_TOOLS_MAIL_METHOD_SMTP:
          mailSender = new MailSenderSMTP();
          break;
        case RapidMiner.PROPERTY_RAPIDMINER_TOOLS_MAIL_METHOD_SENDMAIL:
          mailSender = new MailSenderSendmail();
          break;
        default:
          // LogService.getGlobal().log("Illegal send mail method: " + method + ".",
          // LogService.ERROR);
          LogService.getRoot()
              .log(
                  Level.SEVERE,
                  "com.rapidminer.tools.MailUtilities.illegal_send_mail_method",
                  method);
          throw new MailNotSentException(
              "Illegal send mail method", "illegal_send_mail_method", method);
      }

      if (mailSender != null) {
        mailSender.sendEmail(address, subject, content, headers);
        // LogService.getRoot().info("Sent mail to "+address+" with subject "+subject);
        LogService.getRoot()
            .log(
                Level.INFO,
                "com.rapidminer.tools.MailUtilities.sent_mail_to_adress_with_subject",
                new Object[] {address, subject});
      }
    } catch (Exception e) {
      // LogService.getGlobal().log("Cannot send mail to " + address + ": " + e,
      // LogService.ERROR);
      LogService.getRoot()
          .log(
              Level.SEVERE,
              "com.rapidminer.tools.MailUtilities.sending_mail_to_address_error",
              new Object[] {address, e});
      throw new MailNotSentException(
          "Cannot send mail", "sending_mail_to_address_error", e, new Object[] {address, e});
    }
  }