Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- fixed JavaMailEmailDispatcher to handle text and html bodies properly

- Added a test for JavaMailEmailDispatcher
  • Loading branch information...
commit 40c5a48fccdce3db3d7ce0412f166a6cfc3bf6d0 1 parent 56adbb7
hueboness authored
View
65 sandbox/template/src/java/org/springmodules/email/Email.java
@@ -16,12 +16,13 @@
package org.springmodules.email;
+import java.util.ArrayList;
import java.util.List;
import java.util.Set;
-import java.util.ArrayList;
+import java.util.HashSet;
-import org.springframework.core.io.Resource;
import javax.mail.internet.InternetAddress;
+import org.springframework.core.io.Resource;
/**
* Represents an email. This separate abstraction of an email message is required as a {@link javax.mail.internet.MimeMessage}
@@ -51,10 +52,10 @@
private String textBody;
// Set<Attachment>
- private Set attachments;
+ private Set attachments = new HashSet();
// Set<Attachment>
- private Set inlineAttachments;
+ private Set inlineAttachments = new HashSet();
/**
* Sets the address from which this email is sent.
@@ -104,6 +105,16 @@ public void setTo(InternetAddress[] to) {
}
/**
+ * Sets the address to which this email is sent.
+ *
+ * @param to The address to which this email is sent.
+ */
+ public void setTo(InternetAddress to) {
+ this.to = new ArrayList();
+ addTo(to);
+ }
+
+ /**
* Adds the given address to the address list to which this email is sent.
*
* @param address Another address to which this email should be sent.
@@ -124,6 +135,15 @@ public void addTo(InternetAddress[] addresses) {
}
/**
+ * Sets the given address as the address to which this email is sent.
+ *
+ * @param address The address to which this email should be sent.
+ */
+ public void setTo(String address) {
+ setTo(EmailUtils.createAddress(address));
+ }
+
+ /**
* Adds the given address to the address list to which this email is sent.
*
* @param address Additional address to which this email should be sent.
@@ -163,6 +183,25 @@ public void setCc(InternetAddress[] cc) {
}
/**
+ * Sets the address to which this email is cc'ed.
+ *
+ * @param cc The address to which this email is cc'ed.
+ */
+ public void setCc(InternetAddress cc) {
+ this.cc = new ArrayList();
+ addCc(cc);
+ }
+
+ /**
+ * Sets the address to which this email is cc'ed.
+ *
+ * @param cc The address to which this email is cc'ed.
+ */
+ public void setCc(String cc) {
+ setCc(EmailUtils.createAddress(cc));
+ }
+
+ /**
* Adds the given address to the address list to which this email will be cc'ed.
*
* @param address Additional address to which this email should be cc'ed.
@@ -222,6 +261,24 @@ public void setBcc(InternetAddress[] bcc) {
}
/**
+ * Sets the address to which this email is bcc'ed.
+ *
+ * @param bcc The address to which this email is bcc'ed.
+ */
+ public void setBcc(InternetAddress bcc) {
+ setBcc(new InternetAddress[] { bcc });
+ }
+
+ /**
+ * Sets the address to which this email is bcc'ed.
+ *
+ * @param bcc The address to which this email is bcc'ed.
+ */
+ public void setBcc(String bcc) {
+ setBcc(EmailUtils.createAddress(bcc));
+ }
+
+ /**
* Adds the given address to the address list to which this email is bcc'ed.
*
* @param address Additional address to which this email should be bcc'ed.
View
7 sandbox/template/src/java/org/springmodules/email/JavaMailEmailDispatcher.java
@@ -61,7 +61,12 @@ public void prepare(MimeMessage mimeMessage) throws Exception {
message.setCc(email.getCc());
message.setFrom(email.getFrom());
message.setSubject(email.getSubject());
- message.setText(email.getTextBody(), email.getHtmlBody());
+ if (email.getTextBody() != null) {
+ message.setText(email.getTextBody(), false);
+ }
+ if (email.getHtmlBody() != null) {
+ message.setText(email.getHtmlBody(), true);
+ }
message.setReplyTo(email.getReplyTo());
for (Iterator iter = email.getAttachments().iterator(); iter.hasNext();) {
Attachment attachment = (Attachment) iter.next();
View
82 sandbox/template/src/test/org/springmodules/email/JavaMailEmailDispatcherTests.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2002-2007 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springmodules.email;
+
+import java.util.Properties;
+
+import javax.mail.Address;
+import javax.mail.Session;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import junit.framework.TestCase;
+import org.springframework.mail.javamail.MimeMessagePreparator;
+
+/**
+ * @author Uri Boness
+ */
+public class JavaMailEmailDispatcherTests extends TestCase {
+
+ private JavaMailEmailDispatcher dispatcher;
+
+ protected void setUp() throws Exception {
+ dispatcher = new JavaMailEmailDispatcher();
+ }
+
+ public void testGenerateMimeMessagePreparator() throws Exception {
+
+ Email email = new Email();
+ email.setFrom("From", "from@bla.com");
+ email.setReplyTo("Reply To", "replyto@bla.com");
+ email.setTo("to@bla.com");
+ email.setCc("cc@bla.com");
+ email.setBcc("bcc@bla.com");
+ email.setPriority(EmailPriority.HIGH);
+ email.setSubject("subject");
+ email.setTextBody("text");
+
+ MimeMessagePreparator preparator = dispatcher.generateMimeMessagePreparator(email);
+ MimeMessage mimeMessage = createMimeMessage();
+ preparator.prepare(mimeMessage);
+
+ assertEquals(1, mimeMessage.getFrom().length);
+ InternetAddress address = (InternetAddress)mimeMessage.getFrom()[0];
+ assertEquals("From", address.getPersonal());
+ assertEquals("from@bla.com", address.getAddress());
+
+ Address[] addresses = mimeMessage.getRecipients(MimeMessage.RecipientType.TO);
+ assertEquals(1, addresses.length);
+ address = (InternetAddress)addresses[0];
+ assertEquals("to@bla.com", address.getAddress());
+
+ addresses = mimeMessage.getRecipients(MimeMessage.RecipientType.CC);
+ assertEquals(1, addresses.length);
+ address = (InternetAddress)addresses[0];
+ assertEquals("cc@bla.com", address.getAddress());
+
+ addresses = mimeMessage.getRecipients(MimeMessage.RecipientType.BCC);
+ assertEquals(1, addresses.length);
+ address = (InternetAddress)addresses[0];
+ assertEquals("bcc@bla.com", address.getAddress());
+
+ assertEquals("2", mimeMessage.getHeader("X-Priority")[0]);
+ assertEquals("subject", mimeMessage.getSubject());
+ }
+
+ protected MimeMessage createMimeMessage() {
+ Session session = Session.getInstance(new Properties());
+ return new MimeMessage(session);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.