@Test
  public void testSendMessageWithAttachment() throws Exception {
    final String ATTACHMENT_NAME = "boring-attachment.txt";

    // mock smtp server
    Wiser wiser = new Wiser();
    int port = 2525 + (int) (Math.random() * 100);
    mailSender.setPort(port);
    wiser.setPort(port);
    wiser.start();

    Date dte = new Date();
    String emailSubject = "grepster testSendMessageWithAttachment: " + dte;
    String emailBody = "Body of the grepster testSendMessageWithAttachment message sent at: " + dte;

    ClassPathResource cpResource = new ClassPathResource("/test-attachment.txt");
    // a null from should work
    mailEngine.sendMessage(
        new String[] {"*****@*****.**"}, null, cpResource, emailBody, emailSubject, ATTACHMENT_NAME);

    mailEngine.sendMessage(
        new String[] {"*****@*****.**"},
        mailMessage.getFrom(),
        cpResource,
        emailBody,
        emailSubject,
        ATTACHMENT_NAME);

    wiser.stop();
    // one without and one with from
    assertTrue(wiser.getMessages().size() == 2);

    WiserMessage wm = wiser.getMessages().get(0);
    MimeMessage mm = wm.getMimeMessage();

    Object o = wm.getMimeMessage().getContent();
    assertTrue(o instanceof MimeMultipart);
    MimeMultipart multi = (MimeMultipart) o;
    int numOfParts = multi.getCount();

    boolean hasTheAttachment = false;
    for (int i = 0; i < numOfParts; i++) {
      BodyPart bp = multi.getBodyPart(i);
      String disp = bp.getDisposition();
      if (disp == null) { // the body of the email
        Object innerContent = bp.getContent();
        MimeMultipart innerMulti = (MimeMultipart) innerContent;
        assertEquals(emailBody, innerMulti.getBodyPart(0).getContent());
      } else if (disp.equals(Part.ATTACHMENT)) { // the attachment to the email
        hasTheAttachment = true;
        assertEquals(ATTACHMENT_NAME, bp.getFileName());
      } else {
        fail("Did not expect to be able to get here.");
      }
    }
    assertTrue(hasTheAttachment);
    assertEquals(emailSubject, mm.getSubject());
  }
Beispiel #2
0
  /**
   * Extracts an email from the queue and consumes the email.
   *
   * @return Text of the email
   * @throws MessagingException
   * @throws IOException
   */
  protected String popEmail() throws MessagingException, IOException {
    List<WiserMessage> wiserMessageList = wiser.getMessages();
    if (wiserMessageList.isEmpty()) {
      return null;
    }
    WiserMessage wiserMessage = wiserMessageList.get(wiserMessageList.size() - 1);
    wiserMessageList.remove(wiserMessageList.size() - 1);
    MimeMessage message = wiserMessage.getMimeMessage();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    message.writeTo(os);
    String body = os.toString();

    return body;
  }
  public void testSend() throws Exception {
    // mock smtp server
    Wiser wiser = new Wiser();
    // set the port to a random value so there's no conflicts between tests
    int port = 2525 + (int) (Math.random() * 100);
    mailSender.setPort(port);
    wiser.setPort(port);
    wiser.start();

    Date dte = new Date();
    this.mailMessage.setTo("*****@*****.**");
    String emailSubject = "grepster testSend: " + dte;
    String emailBody = "Body of the grepster testSend message sent at: " + dte;
    this.mailMessage.setSubject(emailSubject);
    this.mailMessage.setText(emailBody);
    this.mailEngine.send(this.mailMessage);

    wiser.stop();
    assertTrue(wiser.getMessages().size() == 1);
    WiserMessage wm = wiser.getMessages().get(0);
    assertEquals(emailSubject, wm.getMimeMessage().getSubject());
    assertEquals(emailBody, wm.getMimeMessage().getContent());
  }