public static void main(String[] args) {
   String resourceFile = "com/baobaotao/event/beans.xml";
   ApplicationContext ctx = new ClassPathXmlApplicationContext(resourceFile);
   MailSender mailSender = ctx.getBean(MailSender.class);
   mailSender.sendMail("test mail.");
   System.out.println("done.");
 }
  /**
   * Send the email.
   *
   * @throws MojoExecutionException if the mail could not be sent
   */
  protected void sendMessage() throws MojoExecutionException {
    File templateFile = new File(templateOutputDirectory, template);
    String email = "";
    final MailSender ms = getActualMailSender();
    final String fromName = ms.getName();
    final String fromAddress = ms.getEmail();
    if (fromAddress == null || fromAddress.equals("")) {
      throw new MojoExecutionException(
          "Invalid mail sender: name and email is mandatory (" + ms + ").");
    }
    getLog()
        .info(
            "Using this sender for email announcement: " + fromAddress + " < " + fromName + " > ");
    try {
      MailMessage mailMsg = new MailMessage();
      mailMsg.setSubject(getSubject());
      mailMsg.setContent(IOUtil.toString(readAnnouncement(templateFile)));
      mailMsg.setContentType(this.mailContentType);
      mailMsg.setFrom(fromAddress, fromName);

      final Iterator it = getToAddresses().iterator();
      while (it.hasNext()) {
        email = it.next().toString();
        getLog().info("Sending mail to " + email + "...");
        mailMsg.addTo(email, "");
      }

      if (getCcAddresses() != null) {
        final Iterator it2 = getCcAddresses().iterator();
        while (it2.hasNext()) {
          email = it2.next().toString();
          getLog().info("Sending cc mail to " + email + "...");
          mailMsg.addCc(email, "");
        }
      }

      if (getBccAddresses() != null) {
        final Iterator it3 = getBccAddresses().iterator();
        while (it3.hasNext()) {
          email = it3.next().toString();
          getLog().info("Sending bcc mail to " + email + "...");
          mailMsg.addBcc(email, "");
        }
      }

      mailer.send(mailMsg);
      getLog().info("Sent...");
    } catch (IOException ioe) {
      throw new MojoExecutionException("Failed to send email.", ioe);
    } catch (MailSenderException e) {
      throw new MojoExecutionException("Failed to send email < " + email + " >", e);
    }
  }
 @Test
 public void basic() {
   Map<String, Object> model = new HashMap<String, Object>();
   model.put("key-1", "val-1");
   model.put("key-2", "val-2");
   mailSender.send("*****@*****.**", "*****@*****.**", "test", "testCode", model);
 }
  /**
   * Returns the identify of the mail sender according to the plugin's configuration:
   *
   * <ul>
   *   <li>if the <tt>mailSender</tt> parameter is set, it is returned
   *   <li>if no <tt>fromDeveloperId</tt> is set, the first developer in the list is returned
   *   <li>if a <tt>fromDeveloperId</tt> is set, the developer with that id is returned
   *   <li>if the developers list is empty or if the specified id does not exist, an exception is
   *       thrown
   * </ul>
   *
   * @return the mail sender to use
   * @throws MojoExecutionException if the mail sender could not be retrieved
   */
  protected MailSender getActualMailSender() throws MojoExecutionException {
    if (senderString != null) {
      try {
        InternetAddress ia = new InternetAddress(senderString, true);
        return new MailSender(ia.getPersonal(), ia.getAddress());
      } catch (AddressException e) {
        throw new MojoExecutionException("Invalid value for change.sender: ", e);
      }
    }
    if (mailSender != null && mailSender.getEmail() != null) {
      return mailSender;
    } else if (from == null || from.isEmpty()) {
      throw new MojoExecutionException(
          "The <developers> section in your pom should not be empty. Add a <developer> entry or set the "
              + "mailSender parameter.");
    } else if (fromDeveloperId == null) {
      final Developer dev = (Developer) from.get(0);
      return new MailSender(dev.getName(), dev.getEmail());
    } else {
      final Iterator it = from.iterator();
      while (it.hasNext()) {
        Developer developer = (Developer) it.next();

        if (fromDeveloperId.equals(developer.getId())) {
          return new MailSender(developer.getName(), developer.getEmail());
        }
      }
      throw new MojoExecutionException(
          "Missing developer with id '"
              + fromDeveloperId
              + "' in the <developers> section in your pom.");
    }
  }
Exemple #5
0
  public static void main(String[] args) {
    // Gmail送信
    MailSender oMail = new MailSender("smtp.gmail.com", 465, "*****@*****.**", "password");

    //        oMail.addToAddress("*****@*****.**","送信先");
    oMail.addToAddress("*****@*****.**");
    //        oMail.addCCAddress("*****@*****.**");
    //        oMail.addBCCAddress("*****@*****.**","BCC");
    oMail.setFromAddress("noreply", "noreply");
    oMail.setSubject("件名テスト");
    oMail.setBody("本文テスト");
    //        oMail.addAttachment("filepath");

    try {
      oMail.sendMail();
    } catch (Exception e) {
      System.out.println(e.toString());
      e.printStackTrace();
    }

    System.exit(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});
    }
  }