@Test(expected = SendFailedException.class) public void testTextMailMessageSendFailed() { String uuid = java.util.UUID.randomUUID().toString(); String subject = "Text Message from $version Mail - " + uuid; String version = "Seam 3"; mailConfig.setServerHost("localHost"); mailConfig.setServerPort(8977); // Port is two off so this should fail Wiser wiser = new Wiser(mailConfig.getServerPort() + 2); try { wiser.start(); person.setName(toName); person.setEmail(toAddress); mailMessage .get() .from(fromAddress, fromName) .replyTo(replyToAddress) .to(toAddress, toName) .subject(subject) .bodyText(new RenderTemplate(templateCompiler.get(), textTemplatePath)) .put("person", person) .put("version", version) .importance(MessagePriority.HIGH) .send(session.get()); } finally { stop(wiser); } }
@Test public void testSMTPSessionAuthentication() throws MessagingException, MalformedURLException { String subject = "HTML+Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString(); mailConfig.setServerHost("localHost"); mailConfig.setServerPort(8978); Wiser wiser = new Wiser(mailConfig.getServerPort()); wiser .getServer() .setAuthenticationHandlerFactory( new EasyAuthenticationHandlerFactory(new SMTPAuthenticator("test", "test12!"))); try { wiser.start(); person.setName(toName); person.setEmail(toAddress); mailMessage .get() .from(fromAddress) .to(person.getEmail()) .subject(subject) .put("person", person) .put("version", "Seam 3") .bodyHtmlTextAlt( new FreeMarkerTemplate( resourceProvider.loadResourceStream("template.html.freemarker")), new FreeMarkerTemplate( resourceProvider.loadResourceStream("template.text.freemarker"))) .importance(MessagePriority.LOW) .deliveryReceipt(fromAddress) .readReceipt("seam.test") .addAttachment( "template.html.freemarker", "text/html", ContentDisposition.ATTACHMENT, resourceProvider.loadResourceStream("template.html.freemarker")) .addAttachment( new URLAttachment( "http://design.jboss.org/seam/logo/final/seam_mail_85px.png", "seamLogo.png", ContentDisposition.INLINE)) .send(gmailSession); } 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))); }
@Gmail @Produces public Session getMailSession() { log.debug("Producing Gmail Session"); MailConfig mailConfig = new MailConfig(); mailConfig.setServerHost("localhost"); mailConfig.setServerPort(8978); mailConfig.setUsername("test"); mailConfig.setPassword("test12!"); mailConfig.setAuth(true); return MailUtility.buildMailSession(mailConfig); }
@Test public void testHTMLTextAltMailMessage() throws MessagingException, IOException { String subject = "HTML+Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString(); String version = "Seam 3"; EmailMessage emailMessage; mailConfig.setServerHost("localHost"); mailConfig.setServerPort(8977); Wiser wiser = new Wiser(mailConfig.getServerPort()); try { wiser.start(); person.setName(toName); person.setEmail(toAddress); emailMessage = mailMessage .get() .from(fromAddress, fromName) .to(person.getEmail(), person.getName()) .subject(subject) .put("person", person) .put("version", version) .bodyHtmlTextAlt( new RenderTemplate(templateCompiler.get(), htmlTemplatePath), new RenderTemplate(templateCompiler.get(), textTemplatePath)) .importance(MessagePriority.LOW) .deliveryReceipt(fromAddress) .readReceipt("seam.test") .addAttachment( htmlTemplatePath, "text/html", ContentDisposition.ATTACHMENT, resourceProvider.loadResourceStream(htmlTemplatePath)) .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(htmlTemplatePath, attachment.getFileName()); Assert.assertTrue(inlineAttachment.getContentType().startsWith("image/png;")); Assert.assertEquals("seamLogo.png", inlineAttachment.getFileName()); }
@Test public void testTextMailMessage() throws MessagingException, IOException { String uuid = java.util.UUID.randomUUID().toString(); String subject = "Text Message from @{version} Mail - " + uuid; String version = "Seam 3"; String mergedSubject = "Text Message from " + version + " Mail - " + uuid; mailConfig.setServerHost("localHost"); mailConfig.setServerPort(8977); Wiser wiser = new Wiser(mailConfig.getServerPort()); try { wiser.start(); person.setName(toName); person.setEmail(toAddress); mailMessage .get() .from(fromAddress, fromName) .replyTo(replyToAddress) .to(toAddress, toName) .subject(new RenderTemplate(templateCompiler.get(), new StringTemplateResource(subject))) .bodyText(new RenderTemplate(templateCompiler.get(), textTemplatePath)) .put("person", person) .put("version", version) .importance(MessagePriority.HIGH) .send(session.get()); } 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", mergedSubject, 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(text.getContentType().startsWith("text/plain; charset=UTF-8")); Assert.assertEquals( expectedTextBody(person.getName(), version), MailTestUtil.getStringContent(text)); }