Exemplo n.º 1
0
  public void test_assertAttachmentFiles() throws Exception {
    File[] attachments =
        new File[] {
          new File(getClass().getResource("attachment1.xls").toURI()),
          new File(getClass().getResource("attachment2.xls").toURI()),
        };

    sendMail("darth.Vader", "luke.Skywalker", "scoop", "Je suis ton pere", attachments);

    List<MailMessage> list = mailFixture.getReceivedMessages();
    assertEquals(1, list.size());
    MailMessage message = list.get(0);
    assertEquals("darth.Vader", message.getFrom());
    assertEquals("luke.Skywalker", message.getTo());
    assertEquals("scoop", message.getSubject());
    assertEquals("Je suis ton pere", message.getBody());

    File multipart1 = message.getMultipart(1, createMultipartFilePath(1));
    File multipart2 = message.getMultipart(2, createMultipartFilePath(2));

    ExcelUtil.compare(
        new File(getClass().getResource("attachment1.xls").toURI()), multipart1, null, null);
    ExcelUtil.compare(
        new File(getClass().getResource("attachment2.xls").toURI()), multipart2, null, null);
  }
Exemplo n.º 2
0
  public void test_getReceivedMessages() throws Exception {
    sendMail("darth.Vader", "luke.Skywalker", "scoop", "Je suis ton pere");

    List<MailMessage> list = mailFixture.getReceivedMessages();
    assertEquals(1, list.size());
    MailMessage message = list.get(0);
    assertEquals("darth.Vader", message.getFrom());
    assertEquals("luke.Skywalker", message.getTo());
    assertEquals("scoop", message.getSubject());
    assertEquals("Je suis ton pere", message.getBody());
  }
Exemplo n.º 3
0
  @Override
  public void send(MailMessage message) {
    if (StringUtils.isEmpty(host)) {
      String errorText = "SMTP Host can't be null!";
      logger.error(errorText);
      throw new IllegalArgumentException(errorText);
    }

    if (message == null) {
      String errorText = "EmailMessage can't be null!";
      logger.error(errorText);
      throw new IllegalArgumentException(errorText);
    }

    Session session;

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);

    if (useTls) {
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.port", tlsPort);

      // Note - not really tested.
    } else if (useSsl) {
      props.put("mail.smtp.socketFactory.port", sslPort);
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.port", sslPort);
    }

    if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
      props.put("mail.smtp.auth", "true");
      session =
          Session.getInstance(
              props,
              new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username, password);
                }
              });

    } else {
      session = Session.getInstance(props);
    }

    try {
      Message mimeMessage = new MimeMessage(session);

      mimeMessage.setFrom(new InternetAddress(message.getFrom()));
      mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(message.getTo()));
      mimeMessage.setSubject(message.getSubject());

      if (!StringUtils.isEmpty(message.getReplyTo())) {
        mimeMessage.setReplyTo(InternetAddress.parse(message.getReplyTo()));
      }

      // Body, plain vs. html
      MimeMultipart multiPartContent = new MimeMultipart();

      if (!StringUtils.isEmpty(message.getBodyPlain())) {
        MimeBodyPart plain = new MimeBodyPart();
        plain.setText(message.getBodyPlain());
        multiPartContent.addBodyPart(plain);
      }

      if (!StringUtils.isEmpty(message.getBodyHtml())) {
        MimeBodyPart html = new MimeBodyPart();
        html.setContent(message.getBodyHtml(), "text/html");
        multiPartContent.addBodyPart(html);
        multiPartContent.setSubType("alternative");
      }

      mimeMessage.setContent(multiPartContent);

      Transport.send(mimeMessage);
      logger.info("Sent email to [{}] with subject [{}].", message.getTo(), message.getSubject());

    } catch (MessagingException me) {
      logger.warn("Failed to send: [{}]", me.getMessage());
      me.printStackTrace();
      throw new RuntimeException(me);
    }
  }