Skip to content

Commit

Permalink
#169 first changes for enhanced mailer
Browse files Browse the repository at this point in the history
  • Loading branch information
svenkubiak committed May 20, 2016
1 parent b8c250a commit 0cf253a
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.mangoo.exceptions;

import org.apache.commons.mail.EmailException;

/**
*
* @author svenkubiak
Expand All @@ -10,7 +8,7 @@
public class MangooMailerException extends Exception {
private static final long serialVersionUID = 8991735383215886040L;

public MangooMailerException(EmailException e) {
public MangooMailerException(Exception e) {
super(e);
}
}
101 changes: 91 additions & 10 deletions mangooio-core/src/main/java/io/mangoo/mail/Mail.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,32 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

import io.mangoo.core.Application;
import io.mangoo.enums.Default;
import io.mangoo.exceptions.MangooMailerException;
import io.mangoo.exceptions.MangooTemplateEngineException;
import io.mangoo.templating.TemplateEngine;

/**
*
* @author svenkubiak
*
*/
public class Mail {
private Map<String, Object> content = new HashMap<String, Object>();
private List<File> files = new ArrayList<File>();
private List<String> recipients = new ArrayList<String>();
private List<String> cc = new ArrayList<String>();
private List<String> bcc = new ArrayList<String>();
private Map<String, Object> content = new HashMap<>();
private List<File> files = new ArrayList<>();
private List<String> recipients = new ArrayList<>();
private List<String> cc = new ArrayList<>();
private List<String> bcc = new ArrayList<>();
private String template;
private String subject;
private String from;
private boolean html;
Expand All @@ -33,31 +41,50 @@ public static Mail newMail(){
}

public Mail withRecipient(String recipient) {
Objects.requireNonNull(recipient, "recipient can not be null");

this.recipients.add(recipient);
return this;
}

public Mail withCC(String recipient) {
Objects.requireNonNull(recipient, "cc recipient can not be null");

this.cc.add(recipient);
return this;
}

public Mail withSubject(String subject) {
Objects.requireNonNull(subject, "sobject can not be null");

this.subject = subject;
return this;
}

public Mail withTemplate(String template) {
Objects.requireNonNull(template, "template can not be null");

this.template = template;
return this;
}

public Mail withBCC(String recipient) {
Objects.requireNonNull(template, "bcc recipient can not be null");

this.bcc.add(recipient);
return this;
}

public Mail withFrom(String from) {
Objects.requireNonNull(from, "from can not be null");

this.from = from;
return this;
}

public Mail withAttachment(File file) {
Objects.requireNonNull(file, "file can not be null");

this.attachment = true;
this.files.add(file);
return this;
Expand All @@ -69,6 +96,9 @@ public Mail isHTML() {
}

public Mail withContent(String key, Object value) {
Objects.requireNonNull(key, "key can not be null");
Objects.requireNonNull(value, "value can not be null");

content.put(key, value);
return this;
}
Expand All @@ -88,6 +118,7 @@ private void sendSimpleEmail() throws MangooMailerException {
Email email = new SimpleEmail();
email.setFrom(this.from);
email.setSubject(this.subject);
email.setMsg(render());

for (String recipient : this.recipients) {
email.addTo(recipient);
Expand All @@ -100,16 +131,66 @@ private void sendSimpleEmail() throws MangooMailerException {
for (String bcc : this.bcc) {
email.addBcc(bcc);
}
} catch (EmailException e) {
} catch (EmailException | MangooTemplateEngineException e) {
throw new MangooMailerException(e);
}
}

private void sendMultipartEmail() {
// TODO Auto-generated method stub
private void sendMultipartEmail() throws MangooMailerException {
try {
MultiPartEmail multiPartEmail = new MultiPartEmail();
multiPartEmail.setFrom(this.from);
multiPartEmail.setSubject(this.subject);
multiPartEmail.setMsg(render());

for (String recipient : this.recipients) {
multiPartEmail.addTo(recipient);
}

for (String cc : this.cc) {
multiPartEmail.addCc(cc);
}

for (String bcc : this.bcc) {
multiPartEmail.addBcc(bcc);
}

for (File file : this.files) {
multiPartEmail.attach(file);
}
} catch (EmailException | MangooTemplateEngineException e) {
throw new MangooMailerException(e);
}
}

private void sendHtmlEmail() throws MangooMailerException {
try {
HtmlEmail htmlEmail = new HtmlEmail();
htmlEmail.setFrom(this.from);
htmlEmail.setSubject(this.subject);
htmlEmail.setHtmlMsg(render());

for (String recipient : this.recipients) {
htmlEmail.addTo(recipient);
}

for (String cc : this.cc) {
htmlEmail.addCc(cc);
}

for (String bcc : this.bcc) {
htmlEmail.addBcc(bcc);
}

for (File file : this.files) {
htmlEmail.attach(file);
}
} catch (EmailException | MangooTemplateEngineException e) {
throw new MangooMailerException(e);
}
}

private void sendHtmlEmail() {
// TODO Auto-generated method stub
private String render() throws MangooTemplateEngineException {
return Application.getInstance(TemplateEngine.class).render(Default.TEMPLATES_FOLDER.toString(), this.template, this.content);
}
}
129 changes: 67 additions & 62 deletions mangooio-integration-test/src/test/java/io/mangoo/mail/MailTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package io.mangoo.mail;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
import org.junit.AfterClass;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.junit.BeforeClass;
import org.junit.Test;

import com.google.common.io.Resources;
import com.icegreen.greenmail.util.GreenMail;

import io.mangoo.core.Application;
import io.mangoo.exceptions.MangooMailerException;

/**
*
* @author svenkubiak
*
*/
public class MailTest {
private static Mailer mailer;
private static GreenMail greenMail;
Expand All @@ -28,65 +36,62 @@ public static void init() {
fakeSMTP.start();
greenMail = fakeSMTP.getGreenMail();
}

@Test
public void textTest() throws Exception {
assertEquals(0, greenMail.getReceviedMessagesForDomain("text.com").length);

final Email email = new SimpleEmail();
email.setFrom("user@test.com");
email.setSubject("plainTextTest");
email.setMsg("This is a test plan text message");
email.addTo("foo@text.com");

mailer.send(email);

assertEquals(1, greenMail.getReceviedMessagesForDomain("text.com").length);
public void SimpleEmailTest() throws MangooMailerException, MessagingException, IOException {
//given
assertThat(greenMail.getReceviedMessagesForDomain("winterfell.com").length, equalTo(0));

//when
Mail.newMail()
.withFrom("Jon Snow <jon.snow@winterfell.com>")
.withRecipient("sansa.stark@winterfell.com")
.withCC("bram.stark@winterfell.com")
.withBCC("nightswatch@castkeblack.org")
.withSubject("Lord of light")
.withTemplate("/emails/simple.ftl")
.withContent("king", "geofrey")
.send();

//then
MimeMessage[] receviedMessagesForDomain = greenMail.getReceviedMessagesForDomain("winterfell.com");
MimeMessage mimeMessage = receviedMessagesForDomain[0];
assertEquals(receviedMessagesForDomain.length, equalTo(1));
assertEquals(mimeMessage.getFrom()[0], equalTo("Jon Snow <jon.snow@winterfell.com>"));
}

@Test
public void multipartTest() throws Exception {
assertEquals(0, greenMail.getReceviedMessagesForDomain("multipart.com").length);

final EmailAttachment attachment = new EmailAttachment();
attachment.setPath(Resources.getResource("attachment.txt").getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("John");

final MultiPartEmail email = new MultiPartEmail();
email.addTo("jdoe@multipart.com", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");

email.attach(attachment);

mailer.send(email);

assertEquals(1, greenMail.getReceviedMessagesForDomain("multipart.com").length);
public void HtmlEmailTest() throws MangooMailerException {
//when
Mail.newMail()
.withFrom("Jon Snow <jon.snow@winterfell.com>")
.withRecipient("sansa.stark@winterfell.com")
.withCC("bram.stark@winterfell.com")
.withBCC("nightswatch@castkeblack.org")
.withSubject("Lord of light")
.withTemplate("/emails/html.ftl")
.withContent("king", "kong")
.send();

//then
}

@Test
public void htmlTest() throws Exception {
assertEquals(0, greenMail.getReceviedMessagesForDomain("html.org").length);

final HtmlEmail email = new HtmlEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@html.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test email with inline image");

email.setHtmlMsg("<html>The apache logo - </html>");
email.setTextMsg("Your email client does not support HTML messages");

mailer.send(email);

assertEquals(1, greenMail.getReceviedMessagesForDomain("html.org").length);
}

@AfterClass
public static void shutdown() {
fakeSMTP.stop();
public void MultiPartEmailTest() throws MangooMailerException {
//given
File file = new File(UUID.randomUUID().toString());

//when
Mail.newMail()
.withFrom("Jon Snow <jon.snow@winterfell.com>")
.withRecipient("sansa.stark@winterfell.com")
.withCC("bram.stark@winterfell.com")
.withBCC("nightswatch@castkeblack.org")
.withSubject("Lord of light")
.withTemplate("/emails/multipart.ftl")
.withContent("name", "raven")
.withContent("king", "none")
.withAttachment(file)
.send();
}
}
}
Loading

0 comments on commit 0cf253a

Please sign in to comment.