예제 #1
0
  @Test
  public void testFreeMarkerTextMailMessage() throws MessagingException, IOException {
    String uuid = java.util.UUID.randomUUID().toString();
    String subject = "Text Message from ${version} Mail - " + uuid;
    String version = "Seam 3";
    String mergedSubject = "Text Message from " + version + " Mail - " + uuid;

    Wiser wiser = new Wiser(mailConfig.getServerPort());
    wiser.setHostname(mailConfig.getServerHost());
    try {
      wiser.start();

      person.setName(toName);
      person.setEmail(toAddress);

      mailMessage
          .get()
          .from(MailTestUtil.getAddressHeader(fromName, fromAddress))
          .replyTo(replyToAddress)
          .to(MailTestUtil.getAddressHeader(toName, toAddress))
          .subject(new FreeMarkerTemplate(subject))
          .bodyText(
              new FreeMarkerTemplate(
                  resourceProvider.loadResourceStream("template.text.freemarker")))
          .put("person", person)
          .put("version", version)
          .importance(MessagePriority.HIGH)
          .send(session.get());
    } finally {
      stop(wiser);
    }

    Assert.assertTrue(
        "Didn't receive the expected amount of messages. Expected 1 got "
            + wiser.getMessages().size(),
        wiser.getMessages().size() == 1);

    MimeMessage mess = wiser.getMessages().get(0).getMimeMessage();

    Assert.assertEquals(
        MailTestUtil.getAddressHeader(fromName, fromAddress), mess.getHeader("From", null));
    Assert.assertEquals(
        MailTestUtil.getAddressHeader(replyToAddress), mess.getHeader("Reply-To", null));
    Assert.assertEquals(
        MailTestUtil.getAddressHeader(toName, toAddress), mess.getHeader("To", null));
    Assert.assertEquals(
        "Subject has been modified",
        mergedSubject,
        MimeUtility.unfold(mess.getHeader("Subject", null)));
    Assert.assertEquals(MessagePriority.HIGH.getPriority(), mess.getHeader("Priority", null));
    Assert.assertEquals(MessagePriority.HIGH.getX_priority(), mess.getHeader("X-Priority", null));
    Assert.assertEquals(MessagePriority.HIGH.getImportance(), mess.getHeader("Importance", null));
    Assert.assertTrue(mess.getHeader("Content-Type", null).startsWith("multipart/mixed"));

    MimeMultipart mixed = (MimeMultipart) mess.getContent();
    BodyPart text = mixed.getBodyPart(0);

    Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed"));
    Assert.assertEquals(1, mixed.getCount());

    Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8"));
    Assert.assertEquals(
        expectedTextBody(person.getName(), version), MailTestUtil.getStringContent(text));
  }
예제 #2
0
  @Test
  public void testFreeMarkerHTMLMailMessage() throws MessagingException, IOException {
    String subject = "HTML Message from Seam Mail - " + java.util.UUID.randomUUID().toString();
    String version = "Seam 3";
    EmailMessage emailMessage;

    Wiser wiser = new Wiser(mailConfig.getServerPort());
    wiser.setHostname(mailConfig.getServerHost());
    try {
      wiser.start();

      person.setName(toName);
      person.setEmail(toAddress);

      emailMessage =
          mailMessage
              .get()
              .from(MailTestUtil.getAddressHeader(fromName, fromAddress))
              .replyTo(MailTestUtil.getAddressHeader(replyToName, replyToAddress))
              .to(person)
              .subject(subject)
              .bodyHtml(
                  new FreeMarkerTemplate(
                      resourceProvider.loadResourceStream("template.html.freemarker")))
              .put("person", person)
              .put("version", version)
              .importance(MessagePriority.HIGH)
              .addAttachment(
                  new URLAttachment(
                      "http://design.jboss.org/seam/logo/final/seam_mail_85px.png",
                      "seamLogo.png",
                      ContentDisposition.INLINE))
              .send(session.get());
    } finally {
      stop(wiser);
    }

    Assert.assertTrue(
        "Didn't receive the expected amount of messages. Expected 1 got "
            + wiser.getMessages().size(),
        wiser.getMessages().size() == 1);

    MimeMessage mess = wiser.getMessages().get(0).getMimeMessage();

    Assert.assertEquals(
        MailTestUtil.getAddressHeader(fromName, fromAddress), mess.getHeader("From", null));
    Assert.assertEquals(
        MailTestUtil.getAddressHeader(replyToName, replyToAddress),
        mess.getHeader("Reply-To", null));
    Assert.assertEquals(
        MailTestUtil.getAddressHeader(toName, toAddress), mess.getHeader("To", null));
    Assert.assertEquals(
        "Subject has been modified", subject, MimeUtility.unfold(mess.getHeader("Subject", null)));
    Assert.assertEquals(MessagePriority.HIGH.getPriority(), mess.getHeader("Priority", null));
    Assert.assertEquals(MessagePriority.HIGH.getX_priority(), mess.getHeader("X-Priority", null));
    Assert.assertEquals(MessagePriority.HIGH.getImportance(), mess.getHeader("Importance", null));
    Assert.assertTrue(mess.getHeader("Content-Type", null).startsWith("multipart/mixed"));

    MimeMultipart mixed = (MimeMultipart) mess.getContent();
    MimeMultipart related = (MimeMultipart) mixed.getBodyPart(0).getContent();
    BodyPart html = related.getBodyPart(0);
    BodyPart attachment1 = related.getBodyPart(1);

    Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed"));
    Assert.assertEquals(1, mixed.getCount());

    Assert.assertTrue(related.getContentType().startsWith("multipart/related"));
    Assert.assertEquals(2, related.getCount());

    Assert.assertTrue(html.getContentType().startsWith("text/html"));
    Assert.assertEquals(
        expectedHtmlBody(emailMessage, person.getName(), person.getEmail(), version),
        MailTestUtil.getStringContent(html));

    Assert.assertTrue(attachment1.getContentType().startsWith("image/png;"));
    Assert.assertEquals("seamLogo.png", attachment1.getFileName());
  }