use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.
the class GreenMailUtilTest method testGetEmptyBodyAndHeader.
@Test
public void testGetEmptyBodyAndHeader() throws Exception {
GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
try {
greenMail.start();
String subject = GreenMailUtil.random();
// Provokes https://github.com/greenmail-mail-test/greenmail/issues/151
String body = "";
String to = "test@localhost";
final byte[] gifAttachment = { 0, 1, 2 };
GreenMailUtil.sendAttachmentEmail(to, "from@localhost", subject, body, gifAttachment, "image/gif", "testimage_filename", "testimage_description", greenMail.getSmtp().getServerSetup());
greenMail.waitForIncomingEmail(5000, 1);
try (Retriever retriever = new Retriever(greenMail.getImap())) {
MimeMultipart mp = (MimeMultipart) retriever.getMessages(to)[0].getContent();
BodyPart bp;
bp = mp.getBodyPart(0);
assertThat(body).isEqualTo(GreenMailUtil.getBody(bp).trim());
assertThat("Content-Type: text/plain; charset=us-ascii\r\n" + "Content-Transfer-Encoding: 7bit").isEqualTo(GreenMailUtil.getHeaders(bp).trim());
bp = mp.getBodyPart(1);
assertThat("AAEC").isEqualTo(GreenMailUtil.getBody(bp).trim());
assertThat("Content-Type: image/gif; name=testimage_filename\r\n" + "Content-Transfer-Encoding: base64\r\n" + "Content-Disposition: attachment; filename=testimage_filename\r\n" + "Content-Description: testimage_description").isEqualTo(GreenMailUtil.getHeaders(bp).trim());
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GreenMailUtil.copyStream(bp.getInputStream(), bout);
assertThat(bout.toByteArray()).isEqualTo(gifAttachment);
}
} finally {
greenMail.stop();
}
}
use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.
the class Pop3ServerTest method testPop3sReceive.
@Test
public void testPop3sReceive() throws Throwable {
assertThat(greenMail.getPop3s()).isNotNull();
final String subject = GreenMailUtil.random();
final String body = GreenMailUtil.random();
String to = "test@localhost";
GreenMailUtil.sendTextEmailSecureTest(to, "from@localhost", subject, body);
greenMail.waitForIncomingEmail(5000, 1);
try (Retriever retriever = new Retriever(greenMail.getPop3s())) {
Message[] messages = retriever.getMessages(to);
assertThat(messages.length).isEqualTo(1);
assertThat(messages[0].getSubject()).isEqualTo(subject);
assertThat(GreenMailUtil.getBody(messages[0]).trim()).isEqualTo(body);
}
}
use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.
the class Pop3ServerTest method testRetrieveMultipart.
@Test
public void testRetrieveMultipart() throws Exception {
assertThat(greenMail.getPop3()).isNotNull();
String subject = GreenMailUtil.random();
String body = GreenMailUtil.random();
String to = "test@localhost";
GreenMailUtil.sendAttachmentEmail(to, "from@localhost", subject, body, new byte[] { 0, 1, 2 }, "image/gif", "testimage_filename", "testimage_description", ServerSetupTest.SMTP);
greenMail.waitForIncomingEmail(5000, 1);
try (Retriever retriever = new Retriever(greenMail.getPop3())) {
Message[] messages = retriever.getMessages(to);
Object o = messages[0].getContent();
assertThat(o instanceof MimeMultipart).isTrue();
MimeMultipart mp = (MimeMultipart) o;
assertThat(mp.getCount()).isEqualTo(2);
BodyPart bp;
bp = mp.getBodyPart(0);
assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo(body);
bp = mp.getBodyPart(1);
assertThat(GreenMailUtil.getBody(bp).trim()).isEqualTo("AAEC");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GreenMailUtil.copyStream(bp.getInputStream(), bout);
byte[] gif = bout.toByteArray();
for (int i = 0; i < gif.length; i++) {
assertThat((int) gif[i]).isEqualTo(i);
}
}
}
use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.
the class SenderRecipientTest method retrieveAndCheck.
/**
* Retrieve message from retriever and check the sender and receivers
*
* @param server Server to read from
* @param login Account to retrieve
*/
private void retrieveAndCheck(AbstractServer server, String login) throws MessagingException {
try (Retriever retriever = new Retriever(server)) {
Message[] messages = retriever.getMessages(login);
assertThat(messages.length).isEqualTo(1);
Message message = messages[0];
assertThat(toInetAddr(message.getRecipients(Message.RecipientType.TO))).isEqualTo(TO_ADDRESSES);
assertThat(toInetAddr(message.getRecipients(Message.RecipientType.CC))).isEqualTo(CC_ADDRESSES);
// BCC addresses are not contained in the message since other receivers are not allowed to know the list of
// BCC recipients
assertThat(toInetAddr(message.getRecipients(Message.RecipientType.BCC))).isNull();
}
}
use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.
the class GreenMailConfigurationTestBase method testUsersAccessible.
/**
* Check if both user accounts can be accessed
*/
public static void testUsersAccessible(GreenMailOperations greenMail) {
// Checks if the user that is registered in the config is actually accessible
try (Retriever retriever = new Retriever(greenMail.getImap())) {
final Message[] messages = retriever.getMessages("user@localhost", "password");
// if getMessage is successful this means that the user account has been created
assertEquals(messages.length, 0);
// Now check second user. this one has a different user id
final Message[] messages2 = retriever.getMessages("secondUserLogin", "password2");
assertEquals(messages2.length, 0);
}
}
Aggregations