Skip to content

Commit

Permalink
mail multiple attacment support adding and version migration from 0.4…
Browse files Browse the repository at this point in the history
….5.3 to 0.4.5.4
  • Loading branch information
hasanmumin committed Jan 5, 2016
1 parent a905d39 commit b5d45ae
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 43 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>io.robe</groupId>
<artifactId>robe-parent</artifactId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
<packaging>pom</packaging>
<name>Robe Project</name>
<url>www.robe.io</url>
Expand Down Expand Up @@ -94,7 +94,7 @@

<properties>
<dropwizard.version>0.7.1</dropwizard.version>
<project.version>0.4.5.1</project.version>
<project.version>0.4.5.4</project.version>
<mysql.version>5.1.26</mysql.version>
<beanutils.version>1.9.0</beanutils.version>
<commons.version>1.9</commons.version>
Expand Down
2 changes: 1 addition & 1 deletion robe-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.robe</groupId>
<artifactId>robe-parent</artifactId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
6 changes: 3 additions & 3 deletions robe-assets/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion robe-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion robe-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion robe-convert/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion robe-crud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion robe-guice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion robe-hibernate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion robe-mail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
51 changes: 32 additions & 19 deletions robe-mail/src/main/java/io/robe/mail/MailItem.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package io.robe.mail;

import javax.activation.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* MailItem is for holding mail entries. It supports standard mail attributes with one attachment and an {@link io.robe.mail.MailEvent} for executing before and after
* MailItem is for holding mail entries. It supports standard mail attributes with one attachments and an {@link io.robe.mail.MailEvent} for executing before and after
*/
public class MailItem {
/**
Expand All @@ -16,7 +13,7 @@ public class MailItem {
private String id = System.currentTimeMillis() + "";
private String title;
private String body;
private DataSource attachment;
private List<DataSource> attachments = new ArrayList<>();
private String sender;
private List<String> receivers;
private MailEvent event;
Expand All @@ -25,18 +22,34 @@ public class MailItem {
public MailItem() {
}

public MailItem(String title, String body, DataSource attachment, String sender, String... receivers) {
public MailItem(String title, String body, DataSource attachments, String sender, String... receivers) {
this.title = title;
this.body = body;
this.attachment = attachment;
this.attachments = Collections.singletonList(attachments);
this.sender = sender;
this.receivers = Arrays.asList(receivers);
}

public MailItem(String title, String body, DataSource attachment, String sender, List<String> receivers) {
public MailItem(String title, String body, DataSource attachments, String sender, List<String> receivers) {
this.title = title;
this.body = body;
this.attachment = attachment;
this.attachments = Collections.singletonList(attachments);
this.sender = sender;
this.receivers = receivers;
}

public MailItem(String title, String body, List<DataSource> attachments, String sender, String... receivers) {
this.title = title;
this.body = body;
this.attachments = attachments;
this.sender = sender;
this.receivers = Arrays.asList(receivers);
}

public MailItem(String title, String body, List<DataSource> attachments, String sender, List<String> receivers) {
this.title = title;
this.body = body;
this.attachments = attachments;
this.sender = sender;
this.receivers = receivers;
}
Expand Down Expand Up @@ -66,12 +79,12 @@ public void setBody(String body) {
this.body = body;
}

public DataSource getAttachment() {
return attachment;
public List<DataSource> getAttachments() {
return attachments;
}

public void setAttachment(DataSource attachment) {
this.attachment = attachment;
public void setAttachments(List<DataSource> attachments) {
this.attachments = attachments;
}

public String getSender() {
Expand All @@ -86,14 +99,14 @@ public List<String> getReceivers() {
return receivers;
}

public void setReceivers(List<String> receivers) {
this.receivers = receivers;
}

public void setReceivers(String... receivers) {
this.receivers = Arrays.asList(receivers);
}

public void setReceivers(List<String> receivers) {
this.receivers = receivers;
}

public MailEvent getEvent() {
return event;
}
Expand All @@ -116,7 +129,7 @@ public String toString() {
"id='" + id + '\'' +
", title='" + title + '\'' +
", body='" + body + '\'' +
", attachment=" + attachment +
", attachments=" + attachments +
", sender='" + sender + '\'' +
", receivers=" + receivers +
", event=" + event +
Expand Down
16 changes: 9 additions & 7 deletions robe-mail/src/main/java/io/robe/mail/MailSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.slf4j.LoggerFactory;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
Expand Down Expand Up @@ -74,11 +75,6 @@ public void sendMessage(MailItem item) throws MessagingException {
InternetAddress from = new InternetAddress(item.getSender());
msg.setFrom(from);

MimeBodyPart attachFilePart = new MimeBodyPart();
if (item.getAttachment() != null) {
attachFilePart.setDataHandler(new DataHandler(item.getAttachment()));
attachFilePart.setFileName(item.getAttachment().getName());
}

InternetAddress[] to = new InternetAddress[item.getReceivers().size()];
for (int i = 0; i < item.getReceivers().size(); i++) {
Expand All @@ -92,8 +88,14 @@ public void sendMessage(MailItem item) throws MessagingException {
body.setContent(item.getBody(), "text/html; charset=UTF-8");
Multipart content = new MimeMultipart();
content.addBodyPart(body);
if (item.getAttachment() != null) {
content.addBodyPart(attachFilePart);

if (item.getAttachments() != null && item.getAttachments().size() > 0) {
for (DataSource attachment : item.getAttachments()) {
BodyPart itemBodyPart = new MimeBodyPart();
itemBodyPart.setDataHandler(new DataHandler(attachment));
itemBodyPart.setFileName(attachment.getName());
content.addBodyPart(itemBodyPart);
}
}

msg.setContent(content);
Expand Down
68 changes: 68 additions & 0 deletions robe-mail/src/test/java/io/robe/mail/MailSenderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.robe.mail;

import org.junit.Before;
import org.junit.Test;

import javax.activation.FileDataSource;
import javax.mail.AuthenticationFailedException;
import javax.mail.MessagingException;
import java.util.HashMap;

/**
* Created by hasanmumin on 05/01/16.
*/
public class MailSenderTest {
private MailSender mailSender;

@Before
public void initialize() {

/**
mail.smtp.username: username@robe.io
mail.smtp.password: xxXXXXxx
mail.smtp.host: smtp.gmail.com
mail.smtp.port: 587
mail.smtp.auth: true
mail.smtp.starttls.enable: true
*/

HashMap<String, Object> properties = new HashMap<>();
properties.put("mail.smtp.username", "YOURGMAILACCOUNT");
properties.put("mail.smtp.password", "YOURPASSWORD");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", 587);
properties.put("mail.smtp.auth", true);
properties.put("mail.smtp.starttls.enable", true);

MailConfiguration mailConfiguration = new MailConfiguration();
mailConfiguration.setUsernameKey("mail.smtp.username");
mailConfiguration.setPasswordKey("mail.smtp.password");
mailConfiguration.setProperties(properties);

mailSender = new MailSender(mailConfiguration);

}

@Test
public void sendMail() {

MailItem mailItem = new MailItem();
mailItem.setTitle("Title");
mailItem.setBody("body");
mailItem.setReceivers("receiver1", "receiver2");
mailItem.getAttachments().add(new FileDataSource("file1path"));
mailItem.getAttachments().add(new FileDataSource("file2path"));

try {
mailSender.sendMessage(mailItem);
} catch (MessagingException e) {
e.printStackTrace();
if (e instanceof AuthenticationFailedException) {
// TODO ignore this because Username and Password not accepted.
assert true;
}

}

}
}
2 changes: 1 addition & 1 deletion robe-quartz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.3</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
6 changes: 3 additions & 3 deletions robe-servlet/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>robe-parent</artifactId>
<groupId>io.robe</groupId>
<version>0.4.5.2</version>
<version>0.4.5.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down

0 comments on commit b5d45ae

Please sign in to comment.