Search in sources :

Example 1 with EmailMessage

use of com.outjected.email.api.EmailMessage in project simple-email by codylerum.

the class MailMessageTest method testHTMLTextAltMailMessage.

@Test
public void testHTMLTextAltMailMessage() throws MessagingException, IOException {
    SessionConfig mailConfig = TestMailConfigs.standardConfig();
    String subject = "HTML+Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString();
    Person person = new Person(toName, toAddress);
    Wiser wiser = new Wiser(mailConfig.getServerPort());
    wiser.setHostname(mailConfig.getServerHost());
    try {
        wiser.start();
        new MailMessageImpl(mailConfig).from(MailTestUtil.getAddressHeader(fromName, fromAddress)).to(person).subject(subject).bodyHtmlTextAlt(htmlBody, textBody).importance(MessagePriority.LOW).deliveryReceipt(fromAddress).readReceipt(fromAddress).addAttachment("template.text.velocity", "text/plain", ContentDisposition.ATTACHMENT, Resources.asByteSource(Resources.getResource("template.text.velocity")).read()).addAttachment(new URLAttachment("http://design.jboss.org/seam/logo/final/seam_mail_85px.png", "seamLogo.png", ContentDisposition.INLINE)).send();
    } 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(toName, toAddress), mess.getHeader("To", null));
    Assert.assertEquals("Subject has been modified", subject, MimeUtility.unfold(mess.getHeader("Subject", null)));
    Assert.assertEquals(MessagePriority.LOW.getPriority(), mess.getHeader("Priority", null));
    Assert.assertEquals(MessagePriority.LOW.getX_priority(), mess.getHeader("X-Priority", null));
    Assert.assertEquals(MessagePriority.LOW.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();
    MimeMultipart alternative = (MimeMultipart) related.getBodyPart(0).getContent();
    BodyPart attachment = mixed.getBodyPart(1);
    BodyPart inlineAttachment = related.getBodyPart(1);
    BodyPart textAlt = alternative.getBodyPart(0);
    BodyPart html = alternative.getBodyPart(1);
    Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed"));
    Assert.assertEquals(2, mixed.getCount());
    Assert.assertTrue(related.getContentType().startsWith("multipart/related"));
    Assert.assertEquals(2, related.getCount());
    Assert.assertTrue(html.getContentType().startsWith("text/html"));
    Assert.assertEquals(htmlBody, MailTestUtil.getStringContent(html));
    Assert.assertTrue(textAlt.getContentType().startsWith("text/plain"));
    Assert.assertEquals(textBody, MailTestUtil.getStringContent(textAlt));
    Assert.assertTrue(attachment.getContentType().startsWith("text/plain"));
    Assert.assertEquals("template.text.velocity", attachment.getFileName());
    Assert.assertTrue(inlineAttachment.getContentType().startsWith("image/png;"));
    Assert.assertEquals("seamLogo.png", inlineAttachment.getFileName());
    EmailMessage convertedMessage = MessageConverter.convert(mess);
    Assert.assertEquals(convertedMessage.getSubject(), subject);
}
Also used : MailMessageImpl(com.outjected.email.impl.MailMessageImpl) BodyPart(javax.mail.BodyPart) EmailMessage(com.outjected.email.api.EmailMessage) URLAttachment(com.outjected.email.impl.attachments.URLAttachment) Wiser(org.subethamail.wiser.Wiser) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SessionConfig(com.outjected.email.api.SessionConfig) Test(org.junit.Test)

Example 2 with EmailMessage

use of com.outjected.email.api.EmailMessage in project simple-email by codylerum.

the class MailMessageTest method testTextMailMessage.

@Test
public void testTextMailMessage() throws MessagingException, IOException {
    SessionConfig mailConfig = TestMailConfigs.standardConfig();
    String subject = "Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString();
    String messageId = "1234@seam.test.com";
    EmailMessage e;
    Wiser wiser = new Wiser(mailConfig.getServerPort());
    wiser.setHostname(mailConfig.getServerHost());
    try {
        wiser.start();
        e = new MailMessageImpl(mailConfig).from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress).to(MailTestUtil.getAddressHeader(toName, toAddress)).subject(subject).bodyText(textBody).importance(MessagePriority.HIGH).messageId(messageId).envelopeFrom(ENVELOPE_FROM_ADDRESS).send();
    } 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", 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"));
    Assert.assertEquals(messageId, MailUtility.headerStripper(mess.getHeader("Message-ID", null)));
    MimeMultipart mixed = (MimeMultipart) mess.getContent();
    BodyPart text = mixed.getBodyPart(0);
    Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed"));
    Assert.assertEquals(1, mixed.getCount());
    Assert.assertTrue("Incorrect Charset: " + e.getCharset(), text.getContentType().startsWith("text/plain; charset=" + e.getCharset()));
    Assert.assertEquals(textBody, MailTestUtil.getStringContent(text));
    EmailMessage convertedMessage = MessageConverter.convert(mess);
    Assert.assertEquals(convertedMessage.getSubject(), subject);
}
Also used : MailMessageImpl(com.outjected.email.impl.MailMessageImpl) BodyPart(javax.mail.BodyPart) EmailMessage(com.outjected.email.api.EmailMessage) Wiser(org.subethamail.wiser.Wiser) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SessionConfig(com.outjected.email.api.SessionConfig) Test(org.junit.Test)

Example 3 with EmailMessage

use of com.outjected.email.api.EmailMessage in project simple-email by codylerum.

the class MailMessageTest method testTextMailMessageUsingDefaultSession.

@Test
public void testTextMailMessageUsingDefaultSession() throws MessagingException, IOException {
    SessionConfig mailConfig = TestMailConfigs.standardConfig();
    Person person = new Person(toName, toAddress);
    String subject = "Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString();
    String messageId = "1234@seam.test.com";
    EmailMessage e;
    Wiser wiser = new Wiser(mailConfig.getServerPort());
    wiser.setHostname(mailConfig.getServerHost());
    try {
        wiser.start();
        e = new MailMessageImpl(mailConfig).from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress).to(person).subject(subject).bodyText(textBody).importance(MessagePriority.HIGH).messageId(messageId).send();
    } 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", 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"));
    Assert.assertEquals(messageId, MailUtility.headerStripper(mess.getHeader("Message-ID", null)));
    MimeMultipart mixed = (MimeMultipart) mess.getContent();
    BodyPart text = mixed.getBodyPart(0);
    Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed"));
    Assert.assertEquals(1, mixed.getCount());
    Assert.assertTrue("Incorrect Charset: " + e.getCharset(), text.getContentType().startsWith("text/plain; charset=" + e.getCharset()));
    Assert.assertEquals(textBody, MailTestUtil.getStringContent(text));
    EmailMessage convertedMessage = MessageConverter.convert(mess);
    Assert.assertEquals(convertedMessage.getSubject(), subject);
}
Also used : MailMessageImpl(com.outjected.email.impl.MailMessageImpl) BodyPart(javax.mail.BodyPart) EmailMessage(com.outjected.email.api.EmailMessage) Wiser(org.subethamail.wiser.Wiser) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SessionConfig(com.outjected.email.api.SessionConfig) Test(org.junit.Test)

Example 4 with EmailMessage

use of com.outjected.email.api.EmailMessage in project simple-email by codylerum.

the class VelocityMailMessageTest method testVelocityHTMLTextAltMailMessage.

@Test
public void testVelocityHTMLTextAltMailMessage() throws MessagingException, IOException {
    SessionConfig mailConfig = TestMailConfigs.standardConfig();
    Person person = new Person(toName, toAddress);
    String subject = "HTML+Text 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();
        emailMessage = new MailMessageImpl(mailConfig).from(MailTestUtil.getAddressHeader(fromName, fromAddress)).to(MailTestUtil.getAddressHeader(person.getName(), person.getEmail())).subject(subject).put("version", version).put("person", person).bodyHtmlTextAlt(new VelocityTemplate(Resources.asCharSource(Resources.getResource("template.html.velocity"), Charsets.UTF_8).read()), new VelocityTemplate(Resources.asCharSource(Resources.getResource("template.text.velocity"), Charsets.UTF_8).read())).importance(MessagePriority.LOW).deliveryReceipt(fromAddress).readReceipt(fromAddress).addAttachment("template.html.velocity", "text/html", ContentDisposition.ATTACHMENT, Resources.asByteSource(Resources.getResource("template.html.velocity")).read()).addAttachment(new URLAttachment("http://design.jboss.org/seam/logo/final/seam_mail_85px.png", "seamLogo.png", ContentDisposition.INLINE)).send();
    } 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(toName, toAddress), mess.getHeader("To", null));
    Assert.assertEquals("Subject has been modified", subject, MimeUtility.unfold(mess.getHeader("Subject", null)));
    Assert.assertEquals(MessagePriority.LOW.getPriority(), mess.getHeader("Priority", null));
    Assert.assertEquals(MessagePriority.LOW.getX_priority(), mess.getHeader("X-Priority", null));
    Assert.assertEquals(MessagePriority.LOW.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();
    MimeMultipart alternative = (MimeMultipart) related.getBodyPart(0).getContent();
    BodyPart attachment = mixed.getBodyPart(1);
    BodyPart inlineAttachment = related.getBodyPart(1);
    BodyPart textAlt = alternative.getBodyPart(0);
    BodyPart html = alternative.getBodyPart(1);
    Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed"));
    Assert.assertEquals(2, 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(textAlt.getContentType().startsWith("text/plain"));
    Assert.assertEquals(expectedTextBody(person.getName(), version), MailTestUtil.getStringContent(textAlt));
    Assert.assertTrue(attachment.getContentType().startsWith("text/html"));
    Assert.assertEquals("template.html.velocity", attachment.getFileName());
    Assert.assertTrue(inlineAttachment.getContentType().startsWith("image/png;"));
    Assert.assertEquals("seamLogo.png", inlineAttachment.getFileName());
    EmailMessage convertedMessage = MessageConverter.convert(mess);
    Assert.assertEquals(convertedMessage.getSubject(), subject);
}
Also used : VelocityTemplate(com.outjected.email.impl.templating.velocity.VelocityTemplate) MailMessageImpl(com.outjected.email.impl.MailMessageImpl) BodyPart(javax.mail.BodyPart) EmailMessage(com.outjected.email.api.EmailMessage) URLAttachment(com.outjected.email.impl.attachments.URLAttachment) Wiser(org.subethamail.wiser.Wiser) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SessionConfig(com.outjected.email.api.SessionConfig) Test(org.junit.Test)

Example 5 with EmailMessage

use of com.outjected.email.api.EmailMessage in project simple-email by codylerum.

the class VelocityMailMessageTest method testTextMailMessageSpecialCharacters.

@Test
public void testTextMailMessageSpecialCharacters() throws MessagingException, IOException {
    SessionConfig mailConfig = TestMailConfigs.standardConfig();
    Person person = new Person(toName, toAddress);
    EmailMessage e;
    String uuid = java.util.UUID.randomUUID().toString();
    String subjectTemplate = "Special Char ü from $version Mail - " + uuid;
    String version = "Seam 3";
    String subject = "Special Char ü from " + version + " Mail - " + uuid;
    String specialTextBody = "This is a Text Body with a special character - ü - $version";
    String mergedSpecialTextBody = "This is a Text Body with a special character - ü - " + version;
    String messageId = "1234@seam.test.com";
    Wiser wiser = new Wiser(mailConfig.getServerPort());
    wiser.setHostname(mailConfig.getServerHost());
    try {
        wiser.start();
        person.setName(toName);
        person.setEmail(toAddress);
        e = new MailMessageImpl(mailConfig).from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress).to(MailTestUtil.getAddressHeader(toName, toAddress)).subject(new VelocityTemplate(subjectTemplate)).bodyText(new VelocityTemplate(specialTextBody)).importance(MessagePriority.HIGH).messageId(messageId).put("version", version).send();
    } 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("Subject has been modified", subject, MimeUtility.decodeText(MimeUtility.unfold(mess.getHeader("Subject", null))));
    MimeMultipart mixed = (MimeMultipart) mess.getContent();
    BodyPart text = mixed.getBodyPart(0);
    Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed"));
    Assert.assertEquals(1, mixed.getCount());
    Assert.assertTrue("Incorrect Charset: " + e.getCharset(), text.getContentType().startsWith("text/plain; charset=" + e.getCharset()));
    Assert.assertEquals(mergedSpecialTextBody, MimeUtility.decodeText(MailTestUtil.getStringContent(text)));
    EmailMessage convertedMessage = MessageConverter.convert(mess);
    Assert.assertEquals(convertedMessage.getSubject(), subject);
}
Also used : VelocityTemplate(com.outjected.email.impl.templating.velocity.VelocityTemplate) MailMessageImpl(com.outjected.email.impl.MailMessageImpl) BodyPart(javax.mail.BodyPart) EmailMessage(com.outjected.email.api.EmailMessage) Wiser(org.subethamail.wiser.Wiser) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SessionConfig(com.outjected.email.api.SessionConfig) Test(org.junit.Test)

Aggregations

EmailMessage (com.outjected.email.api.EmailMessage)19 Test (org.junit.Test)18 MailMessageImpl (com.outjected.email.impl.MailMessageImpl)17 MimeMessage (javax.mail.internet.MimeMessage)17 Wiser (org.subethamail.wiser.Wiser)17 SessionConfig (com.outjected.email.api.SessionConfig)16 BodyPart (javax.mail.BodyPart)15 MimeMultipart (javax.mail.internet.MimeMultipart)15 URLAttachment (com.outjected.email.impl.attachments.URLAttachment)8 FreeMarkerTemplate (com.outjected.email.impl.templating.freemarker.FreeMarkerTemplate)5 VelocityTemplate (com.outjected.email.impl.templating.velocity.VelocityTemplate)5 SMTPAuthenticator (com.outjected.email.util.SMTPAuthenticator)2 EasyAuthenticationHandlerFactory (org.subethamail.smtp.auth.EasyAuthenticationHandlerFactory)2 Header (com.outjected.email.api.Header)1 MailException (com.outjected.email.api.MailException)1 SimpleMailConfig (com.outjected.email.impl.SimpleMailConfig)1 BaseAttachment (com.outjected.email.impl.attachments.BaseAttachment)1 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 InternetAddress (javax.mail.internet.InternetAddress)1