use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.
the class ImapServerTest method testRetrieveMultipart.
@Test
public void testRetrieveMultipart() throws Exception {
assertThat(greenMail.getImap()).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.getImap())) {
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 ImapServerTest method testImapsReceive.
@Test
public void testImapsReceive() throws Throwable {
assertThat(greenMail.getImaps()).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.getImaps())) {
Message[] messages = retriever.getMessages(to);
assertThat(messages.length).isEqualTo(1);
assertThat(messages[0].getSubject()).isEqualTo(subject);
assertThat(((String) messages[0].getContent()).trim()).isEqualTo(body);
}
}
use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.
the class ImapServerTest method testRetrieveSimpleWithNonDefaultPassword.
@Test
public void testRetrieveSimpleWithNonDefaultPassword() throws Exception {
assertThat(greenMail.getImap()).isNotNull();
final String to = "test@localhost";
final String password = "donotharmanddontrecipricateharm";
greenMail.setUser(to, password);
final String subject = GreenMailUtil.random();
final String body = GreenMailUtil.random();
GreenMailUtil.sendTextEmailTest(to, "from@localhost", subject, body);
greenMail.waitForIncomingEmail(5000, 1);
try (Retriever retriever = new Retriever(greenMail.getImap())) {
assertThatThrownBy(() -> retriever.getMessages(to, "wrongpassword")).isInstanceOf(RuntimeException.class).hasCauseInstanceOf(AuthenticationFailedException.class);
Message[] messages = retriever.getMessages(to, password);
assertThat(messages.length).isEqualTo(1);
assertThat(messages[0].getSubject()).isEqualTo(subject);
assertThat(((String) messages[0].getContent()).trim()).isEqualTo(body);
}
}
use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.
the class ImapServerTest method testRetrieveSimple.
/**
* Tests simple send and retrieve, including umlauts.
*
* @throws Exception on error.
*/
@Test
public void testRetrieveSimple() throws Exception {
assertThat(greenMail.getImap()).isNotNull();
final String subject = GreenMailUtil.random() + UMLAUTS;
final String body = GreenMailUtil.random() + "\r\n" + " öäü \u00c4 \u00e4" + "\r\n" + GreenMailUtil.random();
final String to = "test@localhost";
MimeMessage mimeMessage = new MimeMessage(greenMail.getSmtp().createSession());
mimeMessage.setSentDate(new Date());
mimeMessage.setFrom("from@localhost");
mimeMessage.setRecipients(Message.RecipientType.TO, to);
// Need to explicitly set encoding
mimeMessage.setSubject(subject, "UTF-8");
mimeMessage.setText(body, "UTF-8");
Transport.send(mimeMessage);
greenMail.waitForIncomingEmail(5000, 1);
try (Retriever retriever = new Retriever(greenMail.getImap())) {
Message[] messages = retriever.getMessages(to);
assertThat(messages.length).isEqualTo(1);
assertThat(messages[0].getSubject()).isEqualTo(subject);
assertThat(((String) messages[0].getContent()).trim()).isEqualTo(body);
}
}
use of com.icegreen.greenmail.util.Retriever in project greenmail by greenmail-mail-test.
the class SendReceiveWithDifferentEncodingsTest method testSendingAndRetrievingMaintainsEncoding.
private void testSendingAndRetrievingMaintainsEncoding(Charset charset) throws MessagingException, IOException {
Session session = GreenMailUtil.getSession(ServerSetupTest.SMTP, new Properties());
MimeMessage mimeMessage = new MimeMessage(session, mailDataInputStream(charset));
GreenMailUtil.sendMimeMessage(mimeMessage);
String sentMailText = new String(getBytes(mimeMessage), charset);
assertThat(sentMailText).contains("Schön");
greenMail.waitForIncomingEmail(1000, 1);
Retriever retriever = new Retriever(greenMail.getPop3());
MimeMessage receivedMessage = (MimeMessage) retriever.getMessages("bar@example.com")[0];
// Verify that the Message's raw data is in the correct encoding
String receivedPureMessage = new String(getBytes(receivedMessage), charset);
assertThat(receivedPureMessage).contains("Schön");
// Verify that the Message's 'getContent' method correctly determines the charset when returning the content.
// Note that here, we retrieve a String without explicitly providing the encoding.
String content = (String) receivedMessage.getContent();
assertThat(content).contains("Schön");
}
Aggregations