Search in sources :

Example 16 with EmailMessage

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

the class MailMessageTest method testHTMLMailMessage.

@Test
public void testHTMLMailMessage() throws MessagingException, IOException {
    SessionConfig mailConfig = TestMailConfigs.standardConfig();
    String subject = "HTML Message from Seam Mail - " + java.util.UUID.randomUUID().toString();
    Person person = new Person(toName, toAddress);
    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(MailTestUtil.getAddressHeader(replyToName, replyToAddress)).to(person).subject(subject).bodyHtml(htmlBody).importance(MessagePriority.HIGH).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(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.assertEquals(e.getMessageId(), MailUtility.headerStripper(mess.getHeader("Message-ID", 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(htmlBody, MailTestUtil.getStringContent(html));
    Assert.assertTrue(attachment1.getContentType().startsWith("image/png;"));
    Assert.assertEquals("seamLogo.png", attachment1.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 17 with EmailMessage

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

the class MailMessageTest method testTextMailMessageLongFields.

@Test
public void testTextMailMessageLongFields() throws MessagingException, IOException {
    SessionConfig mailConfig = TestMailConfigs.standardConfig();
    String subject = "Sometimes it is important to have a really long subject even if nobody is going to read it - " + java.util.UUID.randomUUID().toString();
    String longFromName = "FromSometimesPeopleHaveNamesWhichAreALotLongerThanYouEverExpectedSomeoneToHaveSoItisGoodToTestUpTo100CharactersOrSo YouKnow?";
    String longFromAddress = "sometimesPeopleHaveNamesWhichAreALotLongerThanYouEverExpectedSomeoneToHaveSoItisGoodToTestUpTo100CharactersOrSo@jboss.org";
    String longToName = "ToSometimesPeopleHaveNamesWhichAreALotLongerThanYouEverExpectedSomeoneToHaveSoItisGoodToTestUpTo100CharactersOrSo YouKnow?";
    String longToAddress = "toSometimesPeopleHaveNamesWhichAreALotLongerThanYouEverExpectedSomeoneToHaveSoItisGoodToTestUpTo100CharactersOrSo.seamerson@seam-mail.test";
    String longCcName = "CCSometimesPeopleHaveNamesWhichAreALotLongerThanYouEverExpectedSomeoneToHaveSoItisGoodToTestUpTo100CharactersOrSo YouKnow? Hatty";
    String longCcAddress = "cCSometimesPeopleHaveNamesWhichAreALotLongerThanYouEverExpectedSomeoneToHaveSoItisGoodToTestUpTo100CharactersOrSo.hatty@jboss.org";
    EmailMessage e;
    Wiser wiser = new Wiser(mailConfig.getServerPort());
    wiser.setHostname(mailConfig.getServerHost());
    try {
        wiser.start();
        e = new MailMessageImpl(mailConfig).from(MailTestUtil.getAddressHeader(longFromName, longFromAddress)).to(MailTestUtil.getAddressHeader(longToName, longToAddress)).cc(MailTestUtil.getAddressHeader(longCcName, longCcAddress)).subject(subject).bodyText(textBody).importance(MessagePriority.HIGH).send();
    } finally {
        stop(wiser);
    }
    Assert.assertTrue("Didn't receive the expected amount of messages. Expected 2 got " + wiser.getMessages().size(), wiser.getMessages().size() == 2);
    MimeMessage mess = wiser.getMessages().get(0).getMimeMessage();
    Assert.assertEquals(MailTestUtil.getAddressHeader(longFromName, longFromAddress), mess.getHeader("From", null));
    Assert.assertEquals(MailTestUtil.getAddressHeader(longToName, longToAddress), mess.getHeader("To", null));
    Assert.assertEquals(MailTestUtil.getAddressHeader(longCcName, longCcAddress), mess.getHeader("CC", 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();
    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 18 with EmailMessage

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

the class VelocityMailMessageTest method testSMTPSessionAuthentication.

@Test
public void testSMTPSessionAuthentication() 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();
    Wiser wiser = new Wiser(mailConfig.getServerPort());
    wiser.setHostname(mailConfig.getServerHost());
    wiser.getServer().setAuthenticationHandlerFactory(new EasyAuthenticationHandlerFactory(new SMTPAuthenticator("test", "test12!")));
    try {
        wiser.start();
        new MailMessageImpl(mailConfig).from(fromAddress).to(person.getEmail()).subject(subject).put("version", "Seam 3").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("Subject has been modified", subject, MimeUtility.unfold(mess.getHeader("Subject", null)));
    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) EmailMessage(com.outjected.email.api.EmailMessage) SMTPAuthenticator(com.outjected.email.util.SMTPAuthenticator) URLAttachment(com.outjected.email.impl.attachments.URLAttachment) Wiser(org.subethamail.wiser.Wiser) MimeMessage(javax.mail.internet.MimeMessage) EasyAuthenticationHandlerFactory(org.subethamail.smtp.auth.EasyAuthenticationHandlerFactory) SessionConfig(com.outjected.email.api.SessionConfig) Test(org.junit.Test)

Example 19 with EmailMessage

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

the class XMLTests method simple.

@Test
public void simple() throws AddressException, JAXBException, IOException {
    EmailMessage msg = new EmailMessage();
    msg.setMessageId(UUID.randomUUID().toString() + "@test.org");
    msg.setImportance(MessagePriority.HIGH);
    msg.getFromAddresses().add(new InternetAddress("from@test.org", "Mr. From"));
    msg.getToAddresses().add(new InternetAddress("to@test.org"));
    msg.getCcAddresses().add(new InternetAddress("cc@test.org"));
    msg.getBccAddresses().add(new InternetAddress("bcc@test.org"));
    msg.setSubject("subject");
    msg.setTextBody("text body");
    msg.setHtmlBody("html body");
    msg.addAttachment(new BaseAttachment("myfile.txt", "text/plain", ContentDisposition.ATTACHMENT, Resources.toByteArray(Resources.getResource("template.text.velocity"))));
    msg.addAttachment(new BaseAttachment("myfile2.txt", "text/plain", ContentDisposition.ATTACHMENT, Resources.toByteArray(Resources.getResource("template.text.velocity"))));
    msg.setEnvelopeFrom(new InternetAddress("env-from@test.org"));
    msg.getReplyToAddresses().add(new InternetAddress("reply-to@test.org"));
    msg.getHeaders().add(new Header("Sender", "sender@test.org"));
    msg.getHeaders().add(new Header("X-Sender", "xsender@test.org"));
    String xml = XMLUtil.marshal(msg);
    EmailMessage umsg = XMLUtil.unmarshal(EmailMessage.class, xml);
    Assert.assertTrue(msg.getType().equals(umsg.getType()));
    Assert.assertTrue(msg.getCharset().equals(umsg.getCharset()));
    Assert.assertTrue(msg.getImportance().equals(umsg.getImportance()));
    Assert.assertTrue(msg.getToAddresses().get(0).equals(umsg.getToAddresses().get(0)));
    Assert.assertTrue(msg.getFromAddresses().get(0).equals(umsg.getFromAddresses().get(0)));
    Assert.assertTrue(msg.getCcAddresses().get(0).equals(umsg.getCcAddresses().get(0)));
    Assert.assertTrue(msg.getBccAddresses().get(0).equals(umsg.getBccAddresses().get(0)));
    Assert.assertTrue(msg.getSubject().equals(umsg.getSubject()));
    Assert.assertTrue(msg.getTextBody().equals(umsg.getTextBody()));
    Assert.assertTrue(msg.getHtmlBody().equals(umsg.getHtmlBody()));
    Assert.assertTrue(msg.getMessageId().equals(umsg.getMessageId()));
    Assert.assertTrue(msg.getAttachments().get(0).getFileName().equals(umsg.getAttachments().get(0).getFileName()));
}
Also used : BaseAttachment(com.outjected.email.impl.attachments.BaseAttachment) EmailMessage(com.outjected.email.api.EmailMessage) InternetAddress(javax.mail.internet.InternetAddress) Header(com.outjected.email.api.Header) 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