示例#1
0
  public void send(MailMessage mail) throws MailSenderException {
    verify(mail);

    try {
      SimpleMailMessage message = new SimpleMailMessage(getSmtpHost(), getSmtpPort());

      message.from(mail.getFrom().getRfc2822Address());

      for (Iterator iter = mail.getToAddresses().iterator(); iter.hasNext(); ) {
        message.to(((MailMessage.Address) iter.next()).getRfc2822Address());
      }

      for (Iterator iter = mail.getCcAddresses().iterator(); iter.hasNext(); ) {
        message.cc(((MailMessage.Address) iter.next()).getRfc2822Address());
      }

      for (Iterator iter = mail.getBccAddresses().iterator(); iter.hasNext(); ) {
        message.bcc(((MailMessage.Address) iter.next()).getRfc2822Address());
      }

      message.setSubject(mail.getSubject());

      for (Iterator it = mail.getHeaders().entrySet().iterator(); it.hasNext(); ) {
        Map.Entry entry = (Map.Entry) it.next();

        Object value = entry.getValue();
        if (value instanceof List) {
          for (Iterator values = ((List) value).iterator(); values.hasNext(); ) {
            String content = (String) values.next();
            message.setHeader(entry.getKey().toString(), StringUtils.clean(content));
          }
        } else {
          String content = (String) value;
          message.setHeader(entry.getKey().toString(), StringUtils.clean(content));
        }
      }

      if (mail.getSendDate() != null) {
        message.setHeader("Date", DateFormatUtils.getDateHeader(mail.getSendDate()));
      } else {
        message.setHeader("Date", DateFormatUtils.getDateHeader(new Date()));
      }

      message.getPrintStream().print(mail.getContent());

      message.sendAndClose();
    } catch (IOException ex) {
      throw new MailSenderException("Error while sending mail.", ex);
    }
  }
  /**
   * 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);
    }
  }