diff --git a/api/src/main/java/org/jboss/seam/mail/api/MailMessage.java b/api/src/main/java/org/jboss/seam/mail/api/MailMessage.java index 7d7ebb0..a0f30be 100644 --- a/api/src/main/java/org/jboss/seam/mail/api/MailMessage.java +++ b/api/src/main/java/org/jboss/seam/mail/api/MailMessage.java @@ -32,6 +32,7 @@ import org.jboss.seam.mail.core.MailConfig; import org.jboss.seam.mail.core.MailTransporter; import org.jboss.seam.mail.core.SendFailedException; +import org.jboss.seam.mail.core.TemplatingException; import org.jboss.seam.mail.core.enumerations.ContentDisposition; import org.jboss.seam.mail.core.enumerations.ContentType; import org.jboss.seam.mail.core.enumerations.MessagePriority; @@ -331,6 +332,13 @@ public MailMessage addAttachment(String fileName, String mimeType, ContentDispos */ public MailMessage bodyHtmlTextAlt(String html, String text); + /** + * Set the charset of the message. Otherwise defaults to Charset.defaultCharset() + * + * @param contentType + */ + public MailMessage charset(String charset); + /** * Set the Content Type of the message * diff --git a/api/src/main/java/org/jboss/seam/mail/core/EmailMessage.java b/api/src/main/java/org/jboss/seam/mail/core/EmailMessage.java index 3d966a3..6eaa82f 100644 --- a/api/src/main/java/org/jboss/seam/mail/core/EmailMessage.java +++ b/api/src/main/java/org/jboss/seam/mail/core/EmailMessage.java @@ -17,6 +17,7 @@ package org.jboss.seam.mail.core; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -28,12 +29,12 @@ import org.jboss.seam.mail.core.enumerations.MessagePriority; /** - * Stores infomation about an EmailMessage while it is being build and after - * sending - * + * Stores infomation about an EmailMessage while it is being build and after sending + * * @author Cody Lerum */ public class EmailMessage { + private String charset = Charset.defaultCharset().name(); private ContentType rootContentType = ContentType.MIXED; private EmailMessageType type = EmailMessageType.STANDARD; private String messageId; @@ -56,9 +57,27 @@ public class EmailMessage { private MessagePriority importance = MessagePriority.NORMAL; + /** + * Get the charset used to encode the EmailMessage + * + * @return charset of the EmailMessage + */ + public String getCharset() { + return charset; + } + + /** + * Override the default charset of the JVM + * + * @param charset + */ + public void setCharset(String charset) { + this.charset = charset; + } + /** * Get the Root Mime ContentType of the EmailMessage - * + * * @return Root Mime ContentType of the EmailMessage */ public ContentType getRootContentType() { @@ -67,7 +86,7 @@ public ContentType getRootContentType() { /** * Set the Root Mime ContentType of the EmailMessage - * + * * @param rootContentType SubType to set */ public void setRootContentType(ContentType rootContentType) { @@ -76,7 +95,7 @@ public void setRootContentType(ContentType rootContentType) { /** * Get the current EmailMessageType of the EmailMessage - * + * * @return EmailMessageType of this EmailMessage */ public EmailMessageType getType() { @@ -85,7 +104,7 @@ public EmailMessageType getType() { /** * Sets the EmailMessageType of the EmailMessage - * + * * @param type EmailMessageType to set on the EmailMessage */ public void setType(EmailMessageType type) { @@ -94,7 +113,7 @@ public void setType(EmailMessageType type) { /** * Gets the Message-ID of the EmailMeassage. This nulled after sending. - * + * * @return Message-ID of the EmailMeassage */ public String getMessageId() { @@ -103,7 +122,7 @@ public String getMessageId() { /** * Sets the Message-ID for the EmailMeassage. Should be in RFC822 format - * + * * @param messageId Globally unique Message-ID example 1234.5678@test.com */ public void setMessageId(String messageId) { @@ -112,7 +131,7 @@ public void setMessageId(String messageId) { /** * Gets the Message-ID after the EmailMeassage has been sent. - * + * * @return Message-ID which was set on the last send. */ public String getLastMessageId() { @@ -121,7 +140,7 @@ public String getLastMessageId() { /** * Sents the Message-ID after the EmailMeassage has been sent. - * + * * @param lastMessageId Message-ID to be set. */ public void setLastMessageId(String lastMessageId) { @@ -130,20 +149,20 @@ public void setLastMessageId(String lastMessageId) { /** * Get the Collection of FROM addresses on the EmailMeassage - * + * * @return Collection of InternetAddresses addresses */ public List getFromAddresses() { return fromAddresses; } - + public void setFromAddresses(List fromAddresses) { this.fromAddresses = fromAddresses; } /** * Adds a single InternetAddress to the FROM addresses on the EmailMessage - * + * * @param fromAddress */ public void addFromAddress(InternetAddress fromAddress) { @@ -151,9 +170,8 @@ public void addFromAddress(InternetAddress fromAddress) { } /** - * Adds a Collection of InternetAddress to the FROM addresses on the - * EmailMessage - * + * Adds a Collection of InternetAddress to the FROM addresses on the EmailMessage + * * @param fromAddresses */ public void addFromAddresses(Collection fromAddresses) { @@ -162,21 +180,20 @@ public void addFromAddresses(Collection fromAddresses) { /** * Get the Collection of REPLY-TO addresses on the EmailMeassage - * + * * @return Collection of InternetAddresses addresses */ public List getReplyToAddresses() { return replyToAddresses; } - + public void setReplyToAddresses(List replyToAddresses) { this.replyToAddresses = replyToAddresses; } /** - * Adds a single InternetAddress to the REPLY-TO addresses on the - * EmailMessage - * + * Adds a single InternetAddress to the REPLY-TO addresses on the EmailMessage + * * @param replyToAddress InternetAddress to set */ public void addReplyToAddress(InternetAddress replyToAddress) { @@ -185,7 +202,7 @@ public void addReplyToAddress(InternetAddress replyToAddress) { /** * Adds a Collection of InternetAddress to the REPLY-TO addresses on the - * + * * @param replyToAddresses Collection of InternetAddress to add */ public void addReplyToAddresses(Collection replyToAddresses) { @@ -194,7 +211,7 @@ public void addReplyToAddresses(Collection replyToAddresses) { /** * Get the Collection of TO addresses on the EmailMeassage - * + * * @return Collection of InternetAddresses addresses */ public List getToAddresses() { @@ -203,7 +220,7 @@ public List getToAddresses() { /** * Adds a single InternetAddress to the TO addresses on the EmailMessage - * + * * @param toAddress InternetAddress to set */ public void addToAddress(InternetAddress toAddress) { @@ -212,20 +229,20 @@ public void addToAddress(InternetAddress toAddress) { /** * Adds a Collection of InternetAddress to the TO addresses on the - * + * * @param toAddresses Collection of InternetAddress to add */ public void addToAddresses(Collection toAddresses) { this.toAddresses.addAll(toAddresses); } - + public void setToAddresses(List toAddresses) { this.toAddresses = toAddresses; } /** * Remove an InternetAddress from the TO addressses - * + * * @param toAddress * @return true if address was removed. false if it did not exist. */ @@ -235,20 +252,20 @@ public boolean removeToAddress(InternetAddress toAddress) { /** * Get the Collection of CC addresses on the EmailMeassage - * + * * @return Collection of InternetAddresses addresses */ public List getCcAddresses() { return ccAddresses; } - + public void setCcAddresses(List ccAddresses) { this.ccAddresses = ccAddresses; } /** * Adds a single InternetAddress to the CC addresses on the EmailMessage - * + * * @param ccAddress InternetAddress to set */ public void addCcAddress(InternetAddress ccAddress) { @@ -257,7 +274,7 @@ public void addCcAddress(InternetAddress ccAddress) { /** * Adds a Collection of InternetAddress to the CC addresses on the - * + * * @param ccAddresses Collection of InternetAddress to add */ public void addCcAddresses(Collection ccAddresses) { @@ -266,7 +283,7 @@ public void addCcAddresses(Collection ccAddresses) { /** * Remove an InternetAddress from the CC addressses - * + * * @param ccAddress * @return true if address was removed. false if it did not exist. */ @@ -276,20 +293,20 @@ public boolean removeCcAddress(InternetAddress ccAddress) { /** * Get the Collection of BCC addresses on the EmailMeassage - * + * * @return Collection of InternetAddresses addresses */ public List getBccAddresses() { return bccAddresses; } - + public void setBccAddresses(List bccAddresses) { this.bccAddresses = bccAddresses; } /** * Adds a single InternetAddress to the BCC addresses on the EmailMessage - * + * * @param bccAddress InternetAddress to set */ public void addBccAddress(InternetAddress bccAddress) { @@ -298,7 +315,7 @@ public void addBccAddress(InternetAddress bccAddress) { /** * Adds a Collection of InternetAddress to the BCC addresses on the - * + * * @param bccAddresses Collection of InternetAddress to add */ public void addBccAddresses(Collection bccAddresses) { @@ -307,7 +324,7 @@ public void addBccAddresses(Collection bccAddresses) { /** * Remove an InternetAddress from the BCC addressses - * + * * @param bccAddress * @return true if address was removed. false if it did not exist. */ @@ -317,20 +334,20 @@ public boolean removeBccAddress(InternetAddress bccAddress) { /** * Get a Collection of additional headers added to the EmailMessage - * + * * @return Collection of Header */ public List
getHeaders() { return headers; } - + public void setHeaders(List
headers) { this.headers = headers; } /** * Add a single Header to the EmailMessage - * + * * @param header Header to set */ public void addHeader(Header header) { @@ -339,7 +356,7 @@ public void addHeader(Header header) { /** * Add a Collection of Header to the EmailMessage - * + * * @param headers Collection of Header to add to EmailMessage */ public void addHeaders(Collection
headers) { @@ -348,7 +365,7 @@ public void addHeaders(Collection
headers) { /** * Get the Subject of the EmailMessage - * + * * @return The Subject */ public String getSubject() { @@ -357,7 +374,7 @@ public String getSubject() { /** * Sets the Subject on the EmailMessage - * + * * @param subject Subject to be set */ public void setSubject(String subject) { @@ -366,7 +383,7 @@ public void setSubject(String subject) { /** * Get the Text Body of the EmailMessage - * + * * @return The EmailMessage Text Body. */ public String getTextBody() { @@ -375,7 +392,7 @@ public String getTextBody() { /** * Set the Text Body of the EmailMessage - * + * * @param textBody Text Body to be set */ public void setTextBody(String textBody) { @@ -384,7 +401,7 @@ public void setTextBody(String textBody) { /** * Get the HTML Body of the EmailMessage - * + * * @return The EmailMessage HTML Body. */ public String getHtmlBody() { @@ -393,7 +410,7 @@ public String getHtmlBody() { /** * Set the HTML Body of the EmailMessage - * + * * @param htmlBody HTML Body to be set */ public void setHtmlBody(String htmlBody) { @@ -402,20 +419,20 @@ public void setHtmlBody(String htmlBody) { /** * Get the collection of InternetAddress which are Delivery Reciept addresses - * + * * @return Collection of InternetAddress */ public List getDeliveryReceiptAddresses() { return deliveryReceiptAddresses; } - + public void setDeliveryReceiptAddresses(List deliveryReceiptAddresses) { this.deliveryReceiptAddresses = deliveryReceiptAddresses; } /** * Adds a InternetAddress as a Delivery Receipt address - * + * * @param address InternetAddress to be added */ public void addDeliveryReceiptAddress(InternetAddress address) { @@ -424,7 +441,7 @@ public void addDeliveryReceiptAddress(InternetAddress address) { /** * Adds a Collection of InternetAddress as a Delivery Receipt address - * + * * @param deliveryReceiptAddresses Collection of InternetAddress to be added */ public void addDeliveryReceiptAddresses(Collection deliveryReceiptAddresses) { @@ -433,20 +450,20 @@ public void addDeliveryReceiptAddresses(Collection deliveryRece /** * Get the collection of InternetAddress which are Read Receipt addresses - * + * * @return Collection of InternetAddress */ public List getReadReceiptAddresses() { return readReceiptAddresses; } - + public void setReadReceiptAddresses(List readReceiptAddresses) { this.readReceiptAddresses = readReceiptAddresses; } /** * Adds a InternetAddress as a Read Receipt address - * + * * @param address InternetAddress to be added */ public void addReadReceiptAddress(InternetAddress address) { @@ -455,7 +472,7 @@ public void addReadReceiptAddress(InternetAddress address) { /** * Adds a Collection of InternetAddress as a Read Receipt address - * + * * @param readReceiptAddresses Collection of InternetAddress to be added */ public void addReadReceiptAddresses(Collection readReceiptAddresses) { @@ -463,9 +480,8 @@ public void addReadReceiptAddresses(Collection readReceiptAddre } /** - * Get the Current Importance of the EmailMessage. Default is normal. No - * Header added - * + * Get the Current Importance of the EmailMessage. Default is normal. No Header added + * * @return MessagePriority of EmailMessage */ public MessagePriority getImportance() { @@ -474,7 +490,7 @@ public MessagePriority getImportance() { /** * Sets the MessagePriority of the EmailMessage - * + * * @param importance MessagePriority to be set. */ public void setImportance(MessagePriority importance) { @@ -483,7 +499,7 @@ public void setImportance(MessagePriority importance) { /** * Adds an EmailAttachment to the EmailMessage - * + * * @param attachment EmailAttachment to be added */ public void addAttachment(EmailAttachment attachment) { @@ -492,7 +508,7 @@ public void addAttachment(EmailAttachment attachment) { /** * Adds a Collection of EmailAttachment to the EmailMessage - * + * * @param attachments Collection of EmailAttachment */ public void addAttachments(Collection attachments) { @@ -503,14 +519,14 @@ public void addAttachments(Collection attachments) { /** * Gets a Collection representing all the Attachments on the EmailMessage - * + * * @return Collection of EmailAttachment */ public List getAttachments() { return attachments; } - + public void setAttachments(List attachments) { this.attachments = attachments; - } + } } diff --git a/impl/src/main/java/org/jboss/seam/mail/core/BaseMailMessage.java b/impl/src/main/java/org/jboss/seam/mail/core/BaseMailMessage.java index 9aa3486..6788c2a 100644 --- a/impl/src/main/java/org/jboss/seam/mail/core/BaseMailMessage.java +++ b/impl/src/main/java/org/jboss/seam/mail/core/BaseMailMessage.java @@ -46,23 +46,20 @@ */ public class BaseMailMessage { private RootMimeMessage rootMimeMessage; - private String charset = "UTF-8"; + private String charset; private ContentType rootContentType; private Map attachments = new HashMap(); private MimeMultipart rootMultipart; private MimeMultipart relatedMultipart = new MimeMultipart(ContentType.RELATED.getValue()); private Session session; - public BaseMailMessage(Session session, ContentType rootContentType) { + public BaseMailMessage(Session session, String charset, ContentType rootContentType) { this.session = session; this.rootContentType = rootContentType; + this.charset = charset; initialize(); } - public BaseMailMessage(Session session) { - this(session, ContentType.MIXED); - } - private void initialize() { rootMimeMessage = new RootMimeMessage(session); rootMultipart = new MimeMultipart(rootContentType.getValue()); @@ -145,7 +142,7 @@ public void setReplyTo(Collection emailAddresses) { } public void setSubject(String value) { - setSubject(value, "UTF-8"); + setSubject(value, charset); } private void setSubject(String value, String charset) { diff --git a/impl/src/main/java/org/jboss/seam/mail/core/MailMessageImpl.java b/impl/src/main/java/org/jboss/seam/mail/core/MailMessageImpl.java index c68e7a3..b5e47e5 100644 --- a/impl/src/main/java/org/jboss/seam/mail/core/MailMessageImpl.java +++ b/impl/src/main/java/org/jboss/seam/mail/core/MailMessageImpl.java @@ -291,6 +291,11 @@ public MailMessage bodyHtmlTextAlt(TemplateProvider htmlBody, TemplateProvider t return this; } + public MailMessage charset(String charset) { + emailMessage.setCharset(charset); + return this; + } + public MailMessage contentType(ContentType contentType) { emailMessage.setRootContentType(contentType); return this; diff --git a/impl/src/main/java/org/jboss/seam/mail/util/MailUtility.java b/impl/src/main/java/org/jboss/seam/mail/util/MailUtility.java index bcaf5ab..fd4c788 100644 --- a/impl/src/main/java/org/jboss/seam/mail/util/MailUtility.java +++ b/impl/src/main/java/org/jboss/seam/mail/util/MailUtility.java @@ -176,7 +176,7 @@ public static String headerStripper(String header) { } public static MimeMessage createMimeMessage(EmailMessage e, Session session) { - BaseMailMessage b = new BaseMailMessage(session, e.getRootContentType()); + BaseMailMessage b = new BaseMailMessage(session, e.getCharset(), e.getRootContentType()); if (!Strings.isNullOrBlank(e.getMessageId())) { b.setMessageID(e.getMessageId()); @@ -213,8 +213,8 @@ public static MimeMessage createMimeMessage(EmailMessage e, Session session) { } else { throw new SendFailedException("Unsupported Message Type: " + e.getType()); } - - MimeMessage msg = b.getFinalizedMessage(); + + MimeMessage msg = b.getFinalizedMessage(); return msg; } @@ -226,7 +226,7 @@ public static void send(EmailMessage e, Session session) throws SendFailedExcept } catch (MessagingException e1) { throw new SendFailedException("Send Failed", e1); } - + try { e.setMessageId(null); e.setLastMessageId(MailUtility.headerStripper(msg.getMessageID())); diff --git a/testsuite/src/test/java/org/jboss/seam/mail/FreeMarkerMailMessageTest.java b/testsuite/src/test/java/org/jboss/seam/mail/FreeMarkerMailMessageTest.java index 6c012ce..9ac1da8 100644 --- a/testsuite/src/test/java/org/jboss/seam/mail/FreeMarkerMailMessageTest.java +++ b/testsuite/src/test/java/org/jboss/seam/mail/FreeMarkerMailMessageTest.java @@ -59,8 +59,7 @@ public class FreeMarkerMailMessageTest { @Deployment(name = "freemarker") public static Archive createTestArchive() { - return Deployments.baseFreeMarkerDeployment() - .addAsResource("template.text.freemarker", "template.text.freemarker") + return Deployments.baseFreeMarkerDeployment().addAsResource("template.text.freemarker", "template.text.freemarker") .addAsResource("template.html.freemarker", "template.html.freemarker") .addPackages(true, FreeMarkerMailMessageTest.class.getPackage()); } @@ -93,6 +92,9 @@ public static Archive createTestArchive() { @Test public void testFreeMarkerTextMailMessage() throws MessagingException, IOException { + + EmailMessage e; + String uuid = java.util.UUID.randomUUID().toString(); String subject = "Text Message from ${version} Mail - " + uuid; String version = "Seam 3"; @@ -103,25 +105,19 @@ public void testFreeMarkerTextMailMessage() throws MessagingException, IOExcepti try { wiser.start(); - person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) - .replyTo(replyToAddress) - .to(MailTestUtil.getAddressHeader(toName, toAddress)) - .subject(new FreeMarkerTemplate(subject)) + e = mailMessage.get().from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress) + .to(MailTestUtil.getAddressHeader(toName, toAddress)).subject(new FreeMarkerTemplate(subject)) .bodyText(new FreeMarkerTemplate(resourceProvider.loadResourceStream("template.text.freemarker"))) - .put("person", person) - .put("version", version) - .importance(MessagePriority.HIGH) - .send(session.get()); + .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); + 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(); @@ -140,21 +136,23 @@ public void testFreeMarkerTextMailMessage() throws MessagingException, IOExcepti Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed")); Assert.assertEquals(1, mixed.getCount()); - Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8")); + Assert.assertTrue("Incorrect Charset: " + e.getCharset(), + text.getContentType().startsWith("text/plain; charset=" + e.getCharset())); Assert.assertEquals(expectedTextBody(person.getName(), version), MailTestUtil.getStringContent(text)); } - + @Test public void testTextMailMessageSpecialCharacters() throws MessagingException, IOException { - + + EmailMessage e; + String uuid = java.util.UUID.randomUUID().toString(); String subject = "Special Char ü from ${version} Mail - " + uuid; String version = "Seam 3"; - String mergedSubject = "Special Char ü from " + version + " Mail - " + uuid; + String mergedSubject = "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()); @@ -165,16 +163,10 @@ public void testTextMailMessageSpecialCharacters() throws MessagingException, IO person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) - .replyTo(replyToAddress) - .to(MailTestUtil.getAddressHeader(toName, toAddress)) - .subject(new FreeMarkerTemplate(subject)) - .bodyText(new FreeMarkerTemplate(specialTextBody)) - .importance(MessagePriority.HIGH) - .messageId(messageId) - .put("version", version) - .send(session.get()); + e = mailMessage.get().from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress) + .to(MailTestUtil.getAddressHeader(toName, toAddress)).subject(new FreeMarkerTemplate(subject)) + .bodyText(new FreeMarkerTemplate(specialTextBody)).importance(MessagePriority.HIGH).messageId(messageId) + .put("version", version).send(session.get()); } finally { stop(wiser); } @@ -183,12 +175,13 @@ public void testTextMailMessageSpecialCharacters() throws MessagingException, IO .getMessages().size() == 1); MimeMessage mess = wiser.getMessages().get(0).getMimeMessage(); - + System.out.println(subject); System.out.println(MimeUtility.decodeText(MimeUtility.unfold(mess.getHeader("Subject", null)))); System.out.println(mergedSubject); - - Assert.assertEquals("Subject has been modified", mergedSubject, MimeUtility.decodeText(MimeUtility.unfold(mess.getHeader("Subject", null)))); + + Assert.assertEquals("Subject has been modified", mergedSubject, + MimeUtility.decodeText(MimeUtility.unfold(mess.getHeader("Subject", null)))); MimeMultipart mixed = (MimeMultipart) mess.getContent(); BodyPart text = mixed.getBodyPart(0); @@ -196,7 +189,8 @@ public void testTextMailMessageSpecialCharacters() throws MessagingException, IO Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed")); Assert.assertEquals(1, mixed.getCount()); - Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8")); + Assert.assertTrue("Incorrect Charset: " + e.getCharset(), + text.getContentType().startsWith("text/plain; charset=" + e.getCharset())); Assert.assertEquals(mergedSpecialTextBody, MimeUtility.decodeText(MailTestUtil.getStringContent(text))); } @@ -205,17 +199,17 @@ public void testFreeMarkerHTMLMailMessage() throws MessagingException, IOExcepti String subject = "HTML 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(); - person.setName(toName); person.setEmail(toAddress); - emailMessage = mailMessage.get() + emailMessage = mailMessage + .get() .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) .replyTo(MailTestUtil.getAddressHeader(replyToName, replyToAddress)) .to(person) @@ -224,13 +218,15 @@ public void testFreeMarkerHTMLMailMessage() throws MessagingException, IOExcepti .put("person", person) .put("version", version) .importance(MessagePriority.HIGH) - .addAttachment(new URLAttachment("http://design.jboss.org/seam/logo/final/seam_mail_85px.png", "seamLogo.png", ContentDisposition.INLINE)) - .send(session.get()); + .addAttachment( + new URLAttachment("http://design.jboss.org/seam/logo/final/seam_mail_85px.png", "seamLogo.png", + ContentDisposition.INLINE)).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); + 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(); @@ -255,8 +251,8 @@ public void testFreeMarkerHTMLMailMessage() throws MessagingException, IOExcepti 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.assertEquals(expectedHtmlBody(emailMessage, person.getName(), person.getEmail(), version), + MailTestUtil.getStringContent(html)); Assert.assertTrue(attachment1.getContentType().startsWith("image/png;")); Assert.assertEquals("seamLogo.png", attachment1.getFileName()); @@ -267,7 +263,7 @@ public void testFreeMarkerHTMLTextAltMailMessage() throws MessagingException, IO 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 { @@ -276,26 +272,29 @@ public void testFreeMarkerHTMLTextAltMailMessage() throws MessagingException, IO person.setName(toName); person.setEmail(toAddress); - emailMessage = mailMessage.get() + emailMessage = mailMessage + .get() .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) .to(MailTestUtil.getAddressHeader(person.getName(), person.getEmail())) .subject(subject) .put("person", person) .put("version", version) - .bodyHtmlTextAlt( - new FreeMarkerTemplate(resourceProvider.loadResourceStream("template.html.freemarker")), + .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(); + .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(); } finally { stop(wiser); } - Assert.assertTrue("Didn't receive the expected amount of messages. Expected 1 got " + wiser.getMessages().size(), wiser.getMessages().size() == 1); + 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(); @@ -323,7 +322,8 @@ public void testFreeMarkerHTMLTextAltMailMessage() throws MessagingException, IO 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.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)); @@ -337,39 +337,42 @@ public void testFreeMarkerHTMLTextAltMailMessage() throws MessagingException, IO @Test public void testSMTPSessionAuthentication() throws MessagingException, MalformedURLException { - String subject = "HTML+Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString(); + 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!"))); + wiser.getServer().setAuthenticationHandlerFactory( + new EasyAuthenticationHandlerFactory(new SMTPAuthenticator("test", "test12!"))); try { wiser.start(); - person.setName(toName); person.setEmail(toAddress); - mailMessage.get() + mailMessage + .get() .from(fromAddress) .to(person.getEmail()) .subject(subject) .put("person", person) .put("version", "Seam 3") - .bodyHtmlTextAlt( - new FreeMarkerTemplate(resourceProvider.loadResourceStream("template.html.freemarker")), + .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); + .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); + 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(); @@ -381,27 +384,19 @@ public void testFreeMarkerTextMailMessageSendFailed() { String uuid = java.util.UUID.randomUUID().toString(); String subject = "Text Message from $version Mail - " + uuid; String version = "Seam 3"; - - //Port is two off so this should fail + + // Port is two off so this should fail Wiser wiser = new Wiser(mailConfig.getServerPort() + 2); - wiser.setHostname(mailConfig.getServerHost()); + wiser.setHostname(mailConfig.getServerHost()); try { wiser.start(); - person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from(fromAddress) - .replyTo(replyToAddress) - .to(toAddress) - .subject(new FreeMarkerTemplate(subject)) + mailMessage.get().from(fromAddress).replyTo(replyToAddress).to(toAddress).subject(new FreeMarkerTemplate(subject)) .bodyText(new FreeMarkerTemplate(resourceProvider.loadResourceStream("template.text.freemarker"))) - .put("person", person) - .put("version", version) - .importance(MessagePriority.HIGH) - .send(session.get()); + .put("person", person).put("version", version).importance(MessagePriority.HIGH).send(session.get()); } finally { stop(wiser); } @@ -419,7 +414,6 @@ protected void stop(Wiser wiser) { } } - private static String expectedHtmlBody(EmailMessage emailMessage, String name, String email, String version) { StringBuilder sb = new StringBuilder(); @@ -427,7 +421,9 @@ private static String expectedHtmlBody(EmailMessage emailMessage, String name, S sb.append("" + "\r\n"); sb.append("

Dear " + name + ",

" + "\r\n"); sb.append("

This is an example HTML email sent by " + version + " and FreeMarker.

" + "\r\n"); - sb.append("

" + "\r\n"); + sb.append("

" + "\r\n"); sb.append("

It has an alternative text body for mail readers that don't support html.

" + "\r\n"); sb.append("" + "\r\n"); sb.append(""); @@ -440,7 +436,8 @@ private static String expectedTextBody(String name, String version) { sb.append("Hello " + name + ",\r\n"); sb.append("\r\n"); - sb.append("This is the alternative text body for mail readers that don't support html. This was sent with " + version + " and FreeMarker."); + sb.append("This is the alternative text body for mail readers that don't support html. This was sent with " + version + + " and FreeMarker."); return sb.toString(); } diff --git a/testsuite/src/test/java/org/jboss/seam/mail/MailMessageTest.java b/testsuite/src/test/java/org/jboss/seam/mail/MailMessageTest.java index 8c0b441..6912e9a 100644 --- a/testsuite/src/test/java/org/jboss/seam/mail/MailMessageTest.java +++ b/testsuite/src/test/java/org/jboss/seam/mail/MailMessageTest.java @@ -55,8 +55,7 @@ public class MailMessageTest { @Deployment(name = "mailMessage") public static Archive createTestArchive() { - return Deployments.baseDeployment() - .addAsResource("template.text.velocity") + return Deployments.baseDeployment().addAsResource("template.text.velocity") .addPackages(true, MailMessageTest.class.getPackage()); } @@ -89,11 +88,12 @@ public static Archive createTestArchive() { @Test public void testTextMailMessage() throws MessagingException, IOException { - - String subject = "Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString(); + 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 { @@ -102,15 +102,9 @@ public void testTextMailMessage() throws MessagingException, IOException { person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) - .replyTo(replyToAddress) - .to(MailTestUtil.getAddressHeader(toName, toAddress)) - .subject(subject) - .bodyText(textBody) - .importance(MessagePriority.HIGH) - .messageId(messageId) - .send(session.get()); + e = mailMessage.get().from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress) + .to(MailTestUtil.getAddressHeader(toName, toAddress)).subject(subject).bodyText(textBody) + .importance(MessagePriority.HIGH).messageId(messageId).send(session.get()); } finally { stop(wiser); } @@ -136,16 +130,19 @@ public void testTextMailMessage() throws MessagingException, IOException { Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed")); Assert.assertEquals(1, mixed.getCount()); - Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8")); + Assert.assertTrue("Incorrect Charset: " + e.getCharset(), + text.getContentType().startsWith("text/plain; charset=" + e.getCharset())); Assert.assertEquals(textBody, MailTestUtil.getStringContent(text)); } - + @Test public void testTextMailMessageSpecialCharacters() throws MessagingException, IOException { - + String subject = "Sometimes subjects have speical characters like ü - " + java.util.UUID.randomUUID().toString(); String specialTextBody = "This is a Text Body with a special character - ü"; + EmailMessage e; + String messageId = "1234@seam.test.com"; Wiser wiser = new Wiser(mailConfig.getServerPort()); @@ -156,15 +153,9 @@ public void testTextMailMessageSpecialCharacters() throws MessagingException, IO person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) - .replyTo(replyToAddress) - .to(MailTestUtil.getAddressHeader(toName, toAddress)) - .subject(subject) - .bodyText(specialTextBody) - .importance(MessagePriority.HIGH) - .messageId(messageId) - .send(session.get()); + e = mailMessage.get().from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress) + .to(MailTestUtil.getAddressHeader(toName, toAddress)).subject(subject).bodyText(specialTextBody) + .importance(MessagePriority.HIGH).messageId(messageId).send(session.get()); } finally { stop(wiser); } @@ -173,8 +164,9 @@ public void testTextMailMessageSpecialCharacters() throws MessagingException, IO .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)))); + + 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); @@ -182,15 +174,16 @@ public void testTextMailMessageSpecialCharacters() throws MessagingException, IO Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed")); Assert.assertEquals(1, mixed.getCount()); - Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8")); + Assert.assertTrue("Incorrect Charset: " + e.getCharset(), + text.getContentType().startsWith("text/plain; charset=" + e.getCharset())); Assert.assertEquals(specialTextBody, MimeUtility.decodeText(MailTestUtil.getStringContent(text))); } @Test public void testHTMLMailMessage() throws MessagingException, IOException { String subject = "HTML Message from Seam Mail - " + java.util.UUID.randomUUID().toString(); - - EmailMessage emailMessage; + + EmailMessage e; Wiser wiser = new Wiser(mailConfig.getServerPort()); wiser.setHostname(mailConfig.getServerHost()); @@ -200,7 +193,7 @@ public void testHTMLMailMessage() throws MessagingException, IOException { person.setName(toName); person.setEmail(toAddress); - emailMessage = mailMessage + e = mailMessage .get() .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) .replyTo(MailTestUtil.getAddressHeader(replyToName, replyToAddress)) @@ -227,7 +220,7 @@ public void testHTMLMailMessage() throws MessagingException, IOException { 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(emailMessage.getLastMessageId(), MailUtility.headerStripper(mess.getHeader("Message-ID", null))); + Assert.assertEquals(e.getLastMessageId(), MailUtility.headerStripper(mess.getHeader("Message-ID", null))); Assert.assertTrue(mess.getHeader("Content-Type", null).startsWith("multipart/mixed")); MimeMultipart mixed = (MimeMultipart) mess.getContent(); @@ -331,6 +324,8 @@ public void testTextMailMessageLongFields() throws MessagingException, IOExcepti String longCcName = "CCSometimesPeopleHaveNamesWhichAreALotLongerThanYouEverExpectedSomeoneToHaveSoItisGoodToTestUpTo100CharactersOrSo YouKnow? Hatty"; String longCcAddress = "cCSometimesPeopleHaveNamesWhichAreALotLongerThanYouEverExpectedSomeoneToHaveSoItisGoodToTestUpTo100CharactersOrSo.hatty@jboss.org"; + EmailMessage e; + Wiser wiser = new Wiser(mailConfig.getServerPort()); wiser.setHostname(mailConfig.getServerHost()); try { @@ -339,14 +334,10 @@ public void testTextMailMessageLongFields() throws MessagingException, IOExcepti person.setName(longToName); person.setEmail(longToAddress); - mailMessage.get() - .from(MailTestUtil.getAddressHeader(longFromName, longFromAddress)) - .to(MailTestUtil.getAddressHeader(longToName, longToAddress)) - .cc(MailTestUtil.getAddressHeader(longCcName, longCcAddress)) - .subject(subject) - .bodyText(textBody) - .importance(MessagePriority.HIGH) - .send(session.get()); + e = mailMessage.get().from(MailTestUtil.getAddressHeader(longFromName, longFromAddress)) + .to(MailTestUtil.getAddressHeader(longToName, longToAddress)) + .cc(MailTestUtil.getAddressHeader(longCcName, longCcAddress)).subject(subject).bodyText(textBody) + .importance(MessagePriority.HIGH).send(session.get()); } finally { stop(wiser); } @@ -371,35 +362,29 @@ public void testTextMailMessageLongFields() throws MessagingException, IOExcepti Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed")); Assert.assertEquals(1, mixed.getCount()); - Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8")); + Assert.assertTrue("Incorrect Charset: " + e.getCharset(), + text.getContentType().startsWith("text/plain; charset=" + e.getCharset())); Assert.assertEquals(textBody, MailTestUtil.getStringContent(text)); } @Test(expected = SendFailedException.class) public void testTextMailMessageSendFailed() { String subject = "Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString(); - String messageId = "1234@seam.test.com"; // Port is one off so this should fail Wiser wiser = new Wiser(mailConfig.getServerPort() + 1); wiser.setHostname(mailConfig.getServerHost()); - + try { wiser.start(); person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) - .replyTo(replyToAddress) - .to(toAddress) - .subject(subject) - .bodyText(textBody) - .importance(MessagePriority.HIGH) - .messageId(messageId) - .send(session.get()); + mailMessage.get().from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress).to(toAddress) + .subject(subject).bodyText(textBody).importance(MessagePriority.HIGH).messageId(messageId) + .send(session.get()); } finally { stop(wiser); } @@ -414,21 +399,16 @@ public void testTextMailMessageInvalidAddress() throws SendFailedException { // Port is one off so this should fail Wiser wiser = new Wiser(mailConfig.getServerPort() + 1); wiser.setHostname(mailConfig.getServerHost()); - + try { wiser.start(); person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from("seam seamerson@test.com", fromName) - .replyTo(replyToAddress).to(toAddress, toName) - .subject(subject) - .bodyText(textBody) - .importance(MessagePriority.HIGH) - .messageId(messageId) - .send(session.get()); + mailMessage.get().from("seam seamerson@test.com", fromName).replyTo(replyToAddress).to(toAddress, toName) + .subject(subject).bodyText(textBody).importance(MessagePriority.HIGH).messageId(messageId) + .send(session.get()); } finally { stop(wiser); } @@ -437,9 +417,10 @@ public void testTextMailMessageInvalidAddress() throws SendFailedException { @Test public void testTextMailMessageUsingPerson() throws MessagingException, IOException { 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 { @@ -448,14 +429,9 @@ public void testTextMailMessageUsingPerson() throws MessagingException, IOExcept person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) - .replyTo(replyToAddress) - .to(person).subject(subject) - .bodyText(textBody) - .importance(MessagePriority.HIGH) - .messageId(messageId) - .send(session.get()); + e = mailMessage.get().from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress).to(person) + .subject(subject).bodyText(textBody).importance(MessagePriority.HIGH).messageId(messageId) + .send(session.get()); } finally { stop(wiser); } @@ -481,16 +457,19 @@ public void testTextMailMessageUsingPerson() throws MessagingException, IOExcept Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed")); Assert.assertEquals(1, mixed.getCount()); - Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8")); + Assert.assertTrue("Incorrect Charset: " + e.getCharset(), + text.getContentType().startsWith("text/plain; charset=" + e.getCharset())); Assert.assertEquals(textBody, MailTestUtil.getStringContent(text)); } @Test public void testTextMailMessageUsingDefaultSession() throws MessagingException, IOException { - String subject = "Text Message from Seam Mail - " + java.util.UUID.randomUUID().toString(); + 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 { @@ -499,14 +478,8 @@ public void testTextMailMessageUsingDefaultSession() throws MessagingException, person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) - .replyTo(replyToAddress) - .to(person).subject(subject) - .bodyText(textBody) - .importance(MessagePriority.HIGH) - .messageId(messageId) - .send(); + e = mailMessage.get().from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress).to(person) + .subject(subject).bodyText(textBody).importance(MessagePriority.HIGH).messageId(messageId).send(); } finally { stop(wiser); } @@ -532,7 +505,8 @@ public void testTextMailMessageUsingDefaultSession() throws MessagingException, Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed")); Assert.assertEquals(1, mixed.getCount()); - Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8")); + Assert.assertTrue("Incorrect Charset: " + e.getCharset(), + text.getContentType().startsWith("text/plain; charset=" + e.getCharset())); Assert.assertEquals(textBody, MailTestUtil.getStringContent(text)); } diff --git a/testsuite/src/test/java/org/jboss/seam/mail/VelocityMailMessageTest.java b/testsuite/src/test/java/org/jboss/seam/mail/VelocityMailMessageTest.java index d0cd348..f94a90c 100644 --- a/testsuite/src/test/java/org/jboss/seam/mail/VelocityMailMessageTest.java +++ b/testsuite/src/test/java/org/jboss/seam/mail/VelocityMailMessageTest.java @@ -61,10 +61,8 @@ public class VelocityMailMessageTest { @Deployment(name = "velocity") public static Archive createTestArchive() { - return Deployments.baseVelocityDeployment() - .addAsResource("template.text.velocity") - .addAsResource("template.html.velocity") - .addAsWebResource("seam-mail-logo.png") + return Deployments.baseVelocityDeployment().addAsResource("template.text.velocity") + .addAsResource("template.html.velocity").addAsWebResource("seam-mail-logo.png") .addPackages(true, VelocityMailMessageTest.class.getPackage()); } @@ -99,6 +97,9 @@ public static Archive createTestArchive() { @Test public void testVelocityTextMailMessage() throws MessagingException, IOException { + + EmailMessage e; + String uuid = java.util.UUID.randomUUID().toString(); String subject = "Text Message from $version Mail - " + uuid; String version = "Seam 3"; @@ -112,7 +113,7 @@ public void testVelocityTextMailMessage() throws MessagingException, IOException person.setName(toName); person.setEmail(toAddress); - mailMessage + e = mailMessage .get() .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) .replyTo(replyToAddress) @@ -146,21 +147,23 @@ public void testVelocityTextMailMessage() throws MessagingException, IOException Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed")); Assert.assertEquals(1, mixed.getCount()); - Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8")); + Assert.assertTrue("Incorrect Charset: " + e.getCharset(), + text.getContentType().startsWith("text/plain; charset=" + e.getCharset())); Assert.assertEquals(expectedTextBody(person.getName(), version), MailTestUtil.getStringContent(text)); } - + @Test public void testTextMailMessageSpecialCharacters() throws MessagingException, IOException { - + + EmailMessage e; + String uuid = java.util.UUID.randomUUID().toString(); String subject = "Special Char ü from $version Mail - " + uuid; String version = "Seam 3"; - String mergedSubject = "Special Char ü from " + version + " Mail - " + uuid; + String mergedSubject = "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()); @@ -171,16 +174,10 @@ public void testTextMailMessageSpecialCharacters() throws MessagingException, IO person.setName(toName); person.setEmail(toAddress); - mailMessage.get() - .from(MailTestUtil.getAddressHeader(fromName, fromAddress)) - .replyTo(replyToAddress) - .to(MailTestUtil.getAddressHeader(toName, toAddress)) - .subject(new VelocityTemplate(subject)) - .bodyText(new VelocityTemplate(specialTextBody)) - .importance(MessagePriority.HIGH) - .messageId(messageId) - .put("version", version) - .send(session.get()); + e = mailMessage.get().from(MailTestUtil.getAddressHeader(fromName, fromAddress)).replyTo(replyToAddress) + .to(MailTestUtil.getAddressHeader(toName, toAddress)).subject(new VelocityTemplate(subject)) + .bodyText(new VelocityTemplate(specialTextBody)).importance(MessagePriority.HIGH).messageId(messageId) + .put("version", version).send(session.get()); } finally { stop(wiser); } @@ -189,12 +186,13 @@ public void testTextMailMessageSpecialCharacters() throws MessagingException, IO .getMessages().size() == 1); MimeMessage mess = wiser.getMessages().get(0).getMimeMessage(); - + System.out.println(subject); System.out.println(MimeUtility.decodeText(MimeUtility.unfold(mess.getHeader("Subject", null)))); System.out.println(mergedSubject); - - Assert.assertEquals("Subject has been modified", mergedSubject, MimeUtility.decodeText(MimeUtility.unfold(mess.getHeader("Subject", null)))); + + Assert.assertEquals("Subject has been modified", mergedSubject, + MimeUtility.decodeText(MimeUtility.unfold(mess.getHeader("Subject", null)))); MimeMultipart mixed = (MimeMultipart) mess.getContent(); BodyPart text = mixed.getBodyPart(0); @@ -202,7 +200,8 @@ public void testTextMailMessageSpecialCharacters() throws MessagingException, IO Assert.assertTrue(mixed.getContentType().startsWith("multipart/mixed")); Assert.assertEquals(1, mixed.getCount()); - Assert.assertTrue(text.getContentType().startsWith("text/plain; charset=UTF-8")); + Assert.assertTrue("Incorrect Charset: " + e.getCharset(), + text.getContentType().startsWith("text/plain; charset=" + e.getCharset())); Assert.assertEquals(mergedSpecialTextBody, MimeUtility.decodeText(MailTestUtil.getStringContent(text))); } @@ -356,7 +355,7 @@ public void testSMTPSessionAuthentication() throws MessagingException, Malformed mailConfig.setServerHost("localhost"); mailConfig.setServerPort(8978); - + Wiser wiser = new Wiser(mailConfig.getServerPort()); wiser.setHostname(mailConfig.getServerHost()); wiser.getServer().setAuthenticationHandlerFactory(